Centos7 minimal install tuning shell script


case:

[09:56:57 root@bj-aly-wordpress-251-93 shell_script]#cat /data/shell_script/reset_centos7_case.sh 
#!/bin/bash
#
#*************************************************************************************
#Author:                                                      wanghaomiao
#QQ:                                                          1074035975
#Date:                                                        2022-03-31
#FileName                                                     reset_centos7.sh
#URL:                                                         wanghaomiao.com.cn
#Description:                                                 The test script
#Copyreght (C):                                               2022 All rightsreserved
#*************************************************************************************
COLOR='\e[1;31m'
COLOR_END='\e[0m'
NETWORK_NAME=`ip addr | sed -nr '/^2/s#^.* ([a-z]+[0-9]+).*#\1#p'`
NETWORK_DIR="/etc/sysconfig/network-scripts/ifcfg-$NETWORK_NAME"

set_sshd(){
sed -i.bak -e 's/#UseDNS no/UseDNS no/' -e 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
systemctl restart sshd
if [ $? -eq 0 ]; then
    echo -e $COLOR ssh service setup completed! $COLOR_END
else
    echo -e "$COLOR SSH service setting failed, please try again,exit! $COLOR_END"
    exit
fi
}

set_vimrc(){
echo -ne "$COLOR"
echo -e "Please enter relevant information to set vimrc file"
read -p "pleace enter your UserName:" UserName
read -p "pleace enter your QQ:" QQ
read -p "pleace enter your UserURL:" UserURL
echo -ne "$COLOR_END"

cat >.vimrc <<EOF
set ts=4
set expandtab
set ignorecase
autocmd BufNewFile *.sh exec ":call SetTitile()"
func SetTitile()
    if expand("%:e") == 'sh'
    call setline(1,"#!/bin/bash")
    call setline(2,"#")
    call setline(3,"#*************************************************************************************")
    call setline(4,"#Author:                                                       $UserName")
    call setline(5,"#QQ:                                                           $QQ")
    call setline(6,"#Date:                                                         ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName                                                      ".expand("%"))
    call setline(8,"#URL:                                                          $UserURL")
    call setline(9,"#Description:                                                  The test script")
    call setline(10,"#Copyreght (C):                                               ".strftime("%Y")." All rightsreserved")
    call setline(11,"#*************************************************************************************")
    call setline(12,"")
    endif
endfunc
autocmd BufNewFile * normal G
EOF
if [ $? -eq 0 ]; then
    echo -e "$COLOR .vimrc setup completed! $COLOR_END"
else
    echo -e "$COLOR .vimrc setting failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_selinux_firewalld(){
sed -ri.bak 's/(SELINUX=).*/\1disabled/' /etc/selinux/config
systemctl disable --now firewalld
if [ $? -eq 0 ];then
    echo -e "$COLOR SElinux and firewalld are turend off! $COLOR_END"
else
    echo -e "$COLOR SELinux and firewall shutdown failed. Please try again,exit! $COLOR_END"
    exit
fi
}

set_hostname(){
read -p "Please enter the host name to set: " HostName
hostnamectl set-hostname $HostName
if [ $? -eq 0 ];then
    echo -e "$COLOR Hostname setting completed,pleace re login! $COLOR_END"
else
    echo -e "$COLOR Hostname setting failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_date(){
read -p "Please enter the time zone to set: " ZONE
timedatectl set-timezone $ZONE
if [ $? -eq 0 ];then
    echo -e "$COLOR Time zone $ZONE settings completed! $COLOR_END"
else
    echo -e "$COLOR Time zone $ZONE setting failed, please try again,exit! $COLOR_END"
    exit
fi
}

set_yum(){
yum repolist
yum -y install wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.buckup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
if [ $? -eq 0 ];then
    echo -e "$COLOR Alibaba cloud Yum source settings completed! $COLOR_END"
else
    echo -e "$COLOR Alibaba colod Yum source settings failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_software_install(){
yum -y install gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages redhat-lsb-core postfix mailx bash-completion man-pages
if [ $? -eq 0 ];then
    echo -e "$COLOR software installation completed! $COLOR_END"
else
    echo -e "$SOLOR sofrware installation failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_eth0(){
mv /etc/sysconfig/network-scripts/ifcfg-$NETWORK_NAME $NETWORK_DIR
sed -ri.bak 's/(GRUB_CMDLINE_LINUX=.*quiet).*/\1 net.ifnames=0 biosdevname=0"/' /etc/default/grub
eval sed -ri.bak 's/${NETWORK_NAME}/eth0/g' $NETWORK_DIR
grub2-mkconfig -o /boot/grub2/grub.cfg
if [ $? -eq 0 ];then
    echo -e "$COLOR The network card name has been changed.about to reboot! $COLOR_END"
    reboot
else
    echo -e "$COLOR The network card name change failed, please try again,exit! $COLOR_END"
    exit
fi
}

set_ip(){
read -p "please input ip address: " IP
read -p "please input netmask address: " NETMASK
read -p "please input gateway address: " GATEWAY
read -p "Please input DNS address: " DNS1
cat > $NETWORK_DIR << EOF
TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
IPADDR="$IP"
NETMASK="$NETMASK"
GATEWAY="$GATEWAY"
DNS1="$DNS1"
EOF
if [ $? -eq 0 ]; then
    echo -e "$COLOR IP address settings completed,the network service will be restarted soon.SSH users need to reconnect $COLOR_END"
else
    echo -e "$COLOR IP address settings failed,please try again!exit $COLOR_END"
    exit
fi
systemctl restart network
}

set_di(){
sed -ri.bak 's/#set bell-style none/set bell-style none/' /etc/inputrc | grep '#set bell-style none' /etc/inputrc
if [ $? -eq 0 ];then
    echo -e "$COLOR Mainboard alarm turned off,about to reboot! $COLOR_END"
    reboot
else
    echo -e "$COLOR Mainboard alarm closing failed,please try again,exit! $COLOR_END"
    exit
fi
}
#PS3="Please enter the number(1-11):"
#MENU="
#优化sshd服务
#设置.vimrc文件
#关闭Selinux及防火墙
#设置主机名
#时间同步
#更改yum源
#常用软件安装
#设置网卡名称为eth0
#设置IP地址
#关闭主板报警
#退出
#"
echo -ne "$COLOR"
cat <<EOF
1) 优化sshd服务
2) 设置.vimrc文件
3) 关闭Selinux及防火墙
4) 设置主机名
5) 时间同步
6) 更改yum源
7) 常用软件安装
8) 设置网卡名称为eth0
9) 设置IP地址
10) 关闭主板报警
11) 退出
EOF
echo -ne "$COLOR_END"

centos_run(){
read -p "Please enter the number(1-11): " MENU
#select menu in $MENU;do
case $MENU in
1)
    set_sshd
    ;;
2)
    set_vimrc
    ;;
3)
    set_selinux_firewalld
    ;;
4)
    set_hostname
    ;;
5)
    set_date
    ;;
6)
    set_yum
    run
    exit
    ;;
7)
    set_software_install
    run
    exit
    ;;
8)
    set_eth0
    ;;
9)
    set_ip
    ;;    
10)
    set_di
    ;;
11)
#    break
    exit
    ;;
*)
    echo -e "$COLOR Input error, please re-enter! $COLOR_END"
esac
#done
}

centos_run

select:

[09:58:42 root@bj-aly-wordpress-251-93 shell_script]#cat /data/shell_script/reset_centos7_select.sh 
#!/bin/bash
#
#*************************************************************************************
#Author:                                                      wanghaomiao
#QQ:                                                          1074035975
#Date:                                                        2022-03-31
#FileName                                                     reset_centos7.sh
#URL:                                                         wanghaomiao.com.cn
#Description:                                                 The test script
#Copyreght (C):                                               2022 All rightsreserved
#*************************************************************************************
COLOR='\e[1;31m'
COLOR_END='\e[0m'
NETWORK_NAME=`ip addr | sed -nr '/^2/s#^.* ([a-z]+[0-9]+).*#\1#p'`
NETWORK_DIR="/etc/sysconfig/network-scripts/ifcfg-$NETWORK_NAME"

set_sshd(){
sed -i.bak -e 's/#UerDNS no/UseDNS no/' -e 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/' /etc/ssh/sshd_config
systemctl restart sshd
if [ $? -eq 0 ]; then
    echo -e $COLOR ssh service setup completed! $COLOR_END
else
    echo -e "$COLOR SSH service setting failed, please try again,exit! $COLOR_END"
    exit
fi
}

set_vimrc(){
echo -ne "$COLOR"
echo -e "Please enter relevant information to set vimrc file"
read -p "pleace enter your UserName:" UserName
read -p "pleace enter your QQ:" QQ
read -p "pleace enter your UserURL:" UserURL
echo -ne "$COLOR_END"

cat >.vimrc <<EOF
set ts=4
set expandtab
set ignorecase
autocmd BufNewFile *.sh exec ":call SetTitile()"
func SetTitile()
    if expand("%:e") == 'sh'
    call setline(1,"#!/bin/bash")
    call setline(2,"#")
    call setline(3,"#*************************************************************************************")
    call setline(4,"#Author:                                                       $UserName")
    call setline(5,"#QQ:                                                           $QQ")
    call setline(6,"#Date:                                                         ".strftime("%Y-%m-%d"))
    call setline(7,"#FileName                                                      ".expand("%"))
    call setline(8,"#URL:                                                          $UserURL")
    call setline(9,"#Description:                                                  The test script")
    call setline(10,"#Copyreght (C):                                               ".strftime("%Y")." All rightsreserved")
    call setline(11,"#*************************************************************************************")
    call setline(12,"")
    endif
endfunc
autocmd BufNewFile * normal G
EOF
if [ $? -eq 0 ]; then
    echo -e "$COLOR .vimrc setup completed! $COLOR_END"
else
    echo -e "$COLOR .vimrc setting failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_selinux_firewalld(){
sed -ri.bak 's/(SELINUX=).*/\1disabled/' /etc/selinux/config
systemctl disable --now firewalld
if [ $? -eq 0 ];then
    echo -e "$COLOR SElinux and firewalld are turend off! $COLOR_END"
else
    echo -e "$COLOR SELinux and firewall shutdown failed. Please try again,exit! $COLOR_END"
    exit
fi
}

set_hostname(){
read -p "Please enter the host name to set: " HostName
hostnamectl set-hostname $HostName
if [ $? -eq 0 ];then
    echo -e "$COLOR Hostname setting completed,pleace re login! $COLOR_END"
else
    echo -e "$COLOR Hostname setting failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_date(){
read -p "Please enter the time zone to set: " ZONE
timedatectl set-timezone $ZONE
if [ $? -eq 0 ];then
    echo -e "$COLOR Time zone $ZONE settings completed! $COLOR_END"
else
    echo -e "$COLOR Time zone $ZONE setting failed, please try again,exit! $COLOR_END"
    exit
fi
}

set_yum(){
yum repolist
yum -y install wget
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.buckup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
if [ $? -eq 0 ];then
    echo -e "$COLOR Alibaba cloud Yum source settings completed! $COLOR_END"
else
    echo -e "$COLOR Alibaba colod Yum source settings failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_software_install(){
yum -y install gcc make gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages redhat-lsb-core postfix mailx bash-completion man-pages
if [ $? -eq 0 ];then
    echo -e "$COLOR software installation completed! $COLOR_END"
else
    echo -e "$SOLOR sofrware installation failed,please try again,exit! $COLOR_END"
    exit
fi
}

set_eth0(){
mv /etc/sysconfig/network-scripts/ifcfg-$NETWORK_NAME $NETWORK_DIR
sed -ri.bak 's/(GRUB_CMDLINE_LINUX=.*quiet).*/\1 net.ifnames=0 biosdevname=0"/' /etc/default/grub
eval sed -ri.bak 's/${NETWORK_NAME}/eth0/g' $NETWORK_DIR
grub2-mkconfig -o /boot/grub2/grub.cfg
if [ $? -eq 0 ];then
    echo -e "$COLOR The network card name has been changed.about to reboot! $COLOR_END"
    reboot
else
    echo -e "$COLOR The network card name change failed, please try again,exit! $COLOR_END"
    exit
fi
}

set_ip(){
read -p "please input ip address: " IP
read -p "please input netmask address: " NETMASK
read -p "please input gateway address: " GATEWAY
read -p "Please input DNS address: " DNS1
cat > $NETWORK_DIR << EOF
TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
IPADDR="$IP"
NETMASK="$NETMASK"
GATEWAY="$GATEWAY"
DNS1="$DNS1"
EOF
if [ $? -eq 0 ]; then
    echo -e "$COLOR IP address settings completed,the network service will be restarted soon.SSH users need to reconnect $COLOR_END"
else
    echo -e "$COLOR IP address settings failed,please try again!exit $COLOR_END"
    exit
fi
systemctl restart network
}

set_di(){
sed -ri.bak 's/#set bell-style none/set bell-style none/' /etc/inputrc | grep '#set bell-style none' /etc/inputrc
if [ $? -eq 0 ];then
    echo -e "$COLOR Mainboard alarm turned off,about to reboot! $COLOR_END"
    reboot
else
    echo -e "$COLOR Mainboard alarm closing failed,please try again,exit! $COLOR_END"
    exit
fi
}
PS3="Please enter the number(1-11):"
MENU="
优化sshd服务
设置.vimrc文件
关闭Selinux及防火墙
设置主机名
时间同步
更改yum源
常用软件安装
设置网卡名称为eth0
设置IP地址
关闭主板报警
退出
"
run(){
#echo -ne "$COLOR"
#cat <<EOF
#1) 优化sshd服务
#2) 设置.vimrc文件
#3) 关闭Selinux及防火墙
#4) 设置主机名
#5) 时间同步
#6) 更改yum源
#7) 常用软件安装
#8) 设置网卡名称为eth0
#9) 设置IP地址
#10) 关闭主板报警
#11) 退出
#EOF
#echo -ne "$COLOR_END"

#read -p "Please enter the number(1-11): " MENU
select menu in $MENU;do
case $REPLY in
1)
    set_sshd
    ;;
2)
    set_vimrc
    ;;
3)
    set_selinux_firewalld
    ;;
4)
    set_hostname
    ;;
5)
    set_date
    ;;
6)
    set_yum
    run
    exit
    ;;
7)
    set_software_install
    run
    exit
    ;;
8)
    set_eth0
    ;;
9)
    set_ip
    ;;    
10)
    set_di
    ;;
11)
    break
#    exit
    ;;
*)
    echo -e "$COLOR Input error, please re-enter! $COLOR_END"
esac
done
}

run