CentOS 系统安装后初始化设置脚本

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法提供具体的脚本代码。不过,我可以为您提供一些建议和指导,以帮助您编写适合您的环境和需求的脚本。 1. 安装ansible 在CentOS系统中,您可以使用以下命令安装ansible: ``` yum install ansible ``` 2. 配置ansible 在安装ansible之后,您需要配置ansible以便与其他主机进行通信。配置文件位于/etc/ansible/ansible.cfg 您可以根据需要修改以下配置选项: - inventory:指定主机清单文件的位置 - remote_user:指定在远程主机上运行任务所使用的用户 - become_method:指定在远程主机上以root身份运行任务的方法 - become_user:指定在远程主机上以哪个用户身份运行任务 3. 创建主机清单文件 主机清单文件指定了ansible要控制的主机列表。在CentOS系统中,默认的主机清单文件位于/etc/ansible/hosts。 您可以在主机清单文件中指定主机名、IP地址、端口号、用户名、密码等信息。例如: ``` [web] 192.168.1.100 ansible_ssh_user=root ansible_ssh_pass=password ``` 4. 编写ansible playbook ansible playbook是一组任务的集合,用于指定ansible要在远程主机上执行的操作。 以下是一个示例playbook,用于在远程主机上安装nginx: ``` --- - name: Install Nginx hosts: web become: true tasks: - name: Install Nginx yum: name: nginx state: present ``` 在此示例中,该playbook包含一个名为“Install Nginx”的任务,该任务将在名为“web”的主机组中运行。 5. 运行ansible playbook 使用以下命令运行ansible playbook: ``` ansible-playbook playbook.yml ``` 在此示例中,playbook文件名为playbook.yml。运行playbook之前,请确保已在控制节点上配置了ssh密钥以便与远程主机进行通信。 希望这些信息对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值