CentOS 系统安装后初始化设置脚本
说明:
- 使用光盘手动安装,选择最小化安装
- 适用于CentOS 6/7/8 系统
- 由于刚装好的系统使用DHCP获取随机ip,因此建议在VM上直接运行,避免ip变化导致断开连接
- 最终获得ip:
- CentOS 6 = 10.0.0.6
- CentOS 7 = 10.0.0.7
- CentOS 8 = 10.0.0.8
- 若在Xshell上执行,应遵循以下要求之一:
- 避免执行第4项【ip与网卡名设置】
- 提前将脚本内容中对应版本的ip修改为当前本机的ip,即可避免掉线
脚本内容
#!/bin/bash
#
#********************************************
#Author: jacklee
#QQ: 1227163339
#Time: 2020-09-29_20:21:50
#FileName: reset_CentOS.sh
#Copyright: 2020 All rights reserved
#Description:
#*********************************************
. /etc/init.d/functions
VS=`uname -r|sed -nr 's/.*el([6-8]).*/\1/p'`
# func1 disable_se_fire
disable_se_fire(){
sed -i.bak 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
cat /etc/selinux/config | grep "^SELINUX=.*" &> /dev/null && action '成功禁用SELinux'
if [ $VS -eq 6 ] ; then
chkconfig iptables off
service iptables stop
service iptables status | grep "not" &> /dev/null && action '成功禁用防火墙'
else
systemctl disable --now firewalld.service
systemctl stop firewalld.service
systemctl status firewalld | grep "dead" &> /dev/null && action '成功禁用防火墙'
fi
}
# func2 yum_jack
yum_jack(){
DIR=/etc/yum.repos.d
[ -e $DIR/backup ] && rm -rf $DIR/backup
mkdir $DIR/backup
mv $DIR/*.repo $DIR/backup
if [ $VS -eq 8 ] ; then
cat > $DIR/jack.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/BaseOS/\$basearch/os/
gpgcheck=0
enabled=1
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/AppStream/\$basearch/os/
gpgcheck=0
enabled=1
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/\$basearch
gpgcheck=0
enabled=1
EOF
else
cat > $DIR/jack.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/centos/\$releasever/os/\$basearch/
gpgcheck=0
enabled=1
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/\$releasever/\$basearch/
https://mirrors.tuna.tsinghua.edu.cn/epel/\$releasever/\$basearch/
gpgcheck=0
enabled=1
EOF
fi
yum makecache && action 'yum配置成功'
}
# func3 install_jack
install_jack(){
yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl* openssh* systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man bash-completion
}
# func4 network_jack
network_jack(){
NET="/etc/sysconfig/network-scripts"
[ -e $NET/ifcfg-ens33 ] && rm -f $NET/ifcfg-ens33
cat > $NET/ifcfg-eth0 << EOF
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
DNS1=180.76.76.76
DNS2=223.6.6.6
PREFIX=24
GATEWAY=10.0.0.2
EOF
if [ $VS -eq 6 ] ; then
cat >> $NET/ifcfg-eth0 << EOF
IPADDR=10.0.0.6
EOF
service network restart
elif [ $VS -eq 7 ] ; then
cat >> $NET/ifcfg-eth0 << EOF
IPADDR=10.0.0.7
EOF
sed -r -i "s/quiet/& net.ifnames=0/" /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
nmcli c reload
nmcli c up eth0
else
cat >> $NET/ifcfg-eth0 << EOF
IPADDR=10.0.0.8
EOF
nmcli c reload
nmcli c up eth0
fi
echo -e "\e[1;32m网络设置已完成,请重启生效!\e[0m"
}
# func5 mail_jack
mail_jack(){
yum -y install postfix mailx
cat >> /etc/mail.rc <<EOF
set from=1227163339@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1227163339@qq.com
set smtp-auth-password=kfppjzkbyfmnijeg
EOF
if [ $VS -eq 6 ] ; then
chkconfig --add postfix
chkconfig postfix on
service postfix start
else
systemctl enable --now postfix
fi
ss -ntl | grep "127.0.0.1:25" &> /dev/null && action '成功启用邮件服务'
}
# func6 personalise
personalise (){
# 彩虹屁:
# 实现连续变色:PS1='\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\$\[\e[0m\]'
cat > /etc/profile.d/env.sh <<EOF
PS1="\[\e[1;\$[RANDOM%7+31]m\][\u@\h \W]\\\\\$\[\e[0m\]"
export EDITOR=vim
EOF
# vim常用设置 (只为root设置)
cat > /root/.vimrc << EOF
set nu
set cul
set ai
set paste
set et
set ts=4
syntax on
EOF
# 新建脚本自动添加头部 (只为root设置)
cat >> /root/.vimrc << EOF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()
if expand ("%:e") == 'sh'
call setline(1, "#!/bin/bash")
call setline(2, "#")
call setline(3, "#********************************************")
call setline(4, "#Author: jacklee")
call setline(5, "#QQ: 1227163339")
call setline(6, "#Time: ".strftime("%F_%T"))
call setline(7, "#FileName: ".expand("%"))
call setline(8, "#Copyright: ".strftime("%Y")." All rights reserved")
call setline(9, "#Description: ")
call setline(10,"#*********************************************")
endif
endfunc
EOF
}
# func7 ntp_jack
ntp_jack(){
if [ $VS -eq 8 ] ; then
yum -y install chrony* &> /dev/null
sed -i.bak 's/^pool 2.centos.*/server ntp.aliyun.com iburst/' /etc/chrony.conf
systemctl enable --now chronyd.service
ss -ntul|grep '323' > /dev/null && action '成功启用时间同步服务'
elif [ $VS -eq 7 ] ; then
yum -y install chrony* &> /dev/null
sed -i.bak 's/^server 0.*/server ntp.aliyun.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 1.*/server ntp1.aliyun.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 2.*/server time1.cloud.tencent.com iburst/' /etc/chrony.conf
sed -i.bak 's/^server 3.*/server time2.cloud.tencent.com iburst/' /etc/chrony.conf
systemctl enable --now chronyd.service
ss -ntul|grep '323' > /dev/null && action '成功启用时间同步服务'
else
yum -y install ntp* &> /dev/null
sed -i.bak 's/^server 0.*/server ntp.aliyun.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 1.*/server ntp1.aliyun.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 2.*/server time1.cloud.tencent.com iburst/' /etc/ntp.conf
sed -i.bak 's/^server 3.*/server time2.cloud.tencent.com iburst/' /etc/ntp.conf
chkconfig --add ntpd
chkconfig ntpd on
service ntpd start
ss -ntul|grep '123' > /dev/null && action '成功启用时间同步服务'
fi
}
###########################################################
while true ; do
echo -en "\e[1;32m"
cat << EOF
▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼
1)【禁用SELinux与防火墙】
2)【配置yum源与EPEL源】
3)【安装常用工具软件】
4)【ip与网卡名设置】
5)【启用邮件服务】
6)【个性化设置】
7)【时间同步】
8)【全执行并重启】
0)【退出】
▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
EOF
echo -e "\e[0m"
read -p "请输入选项(0~8):" MENU
case $MENU in
1)
disable_se_fire
;;
2)
yum_jack
;;
3)
install_jack
;;
4)
network_jack
;;
5)
mail_jack
;;
6)
personalise
;;
7)
ntp_jack
;;
8)
disable_se_fire
yum_jack
install_jack
network_jack
mail_jack
personalise
ntp_jack
echo -e "\e[1;31m5秒后重启!\e[0m"
sleep 1
echo -e "\e[1;31m4秒后重启!\e[0m"
sleep 1
echo -e "\e[1;31m3秒后重启!\e[0m"
sleep 1
echo -e "\e[1;31m2秒后重启!\e[0m"
sleep 1
echo -e "\e[1;31m1秒后重启!\e[0m"
sleep 1
reboot
;;
0)
exit
;;
*)
echo -e "\e[1;31m请输入正确的数字(0~8)!\e[0m"
esac
done