Centos7系统初始化脚本

时间:2022-02-16 17:29:24

前言:

  因公司业务增加,陆续新增服务器,时不时的来几台,手动地一台台对服务器初始化操作感觉太麻烦。

  于是乎,根据初始化需求整合了一个初始化脚本,实现批量脚本初始化操作。

说明:

  本脚本根据自身需求编写而成,集成了Centos7服务器的基本初始化步骤。

  其中包含如下基础优化内容:

  1)SELinux关闭;

  2)Firewalld关闭;

  3)Bash环境修改;

  4)Openfile系统最大打开文件数配置;

  5)Hostname主机名修改;

  6)History历史记录配置等。

注意:

  A)脚本执行完后将自动重启服务器;

  B)执行脚本前应在/etc/hosts中配置好对应的解析,如 10.10.10.10 kazihuo 内容添加到hosts文件中,执行完脚本后,服务器10.10.10.10将自动将Hostname主机名配置成 “kazihuo” ;

  C)确保存在 /tmp/sysctl.conf 文件,即将已配置好的Kernel内核优化参数文件放置 /tmp 目录下,执行完脚本后,其优化参数将自动配置到服务器中;如无优化文件,即在最后的函数中注释137行 Kernel 即可;

内容:

  脚本内容如下:

[root@kazihuo ~]# cat init.sh

  1 #!/bin/bash
  2 #====================================================
  3 # Author: kazihuo
  4 # Blog: https://www.cnblogs.com/kazihuo
  5 # Create Date: 2019-01-24
  6 # Description: It works for system initalization.
  7 #====================================================
  8 
  9 #State:Plese confirm the files of /etc/hosts and /tmp/sysctl.conf before using the script
 10 
 11 [ -f /etc/init.d/functions ] && source /etc/init.d/functions
 12 
 13 # Defined result function
 14 function Msg(){
 15     if [ $? -eq 0 ];then
 16         action "$1" /bin/true
 17     else 
 18         action "$1" /bin/false
 19     fi
 20 }
 21 
 22 # Defined close selinux function
 23 function Selinux(){
 24     [ -f /etc/selinux/config ] && {
 25     sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
 26     setenforce 0
 27     Msg "Close selinux"
 28     }
 29 }
 30 
 31 # Defined close firewalld function
 32 function Firewalld(){
 33     systemctl stop firewalld.service
 34     systemctl disable firewalld.service  >/dev/null 2>&1
 35     Msg "Close firewalld"
 36 }
 37 
 38 # Defined bashrc function
 39 function Bashrc(){
 40     sed -i 's/\\h \\W/\\h \\w/g' /etc/bashrc
 41     Msg "Bashrc"
 42 }
 43 
 44 # Defined open files function
 45 function Openfile(){
 46     if [ `egrep "^\*" /etc/security/limits.conf|wc -l` -eq 0 ];then
 47         echo '* - nofile 65535' >> /etc/security/limits.conf
 48         ulimit -SHn 65535
 49         Msg "Open files" 
 50     fi
 51 }
 52 
 53 # Defined kernel paramters function
 54 function Kernel(){
 55     if [ -f /tmp/sysctl.conf ];then 
 56         /usr/bin/\cp /etc/sysctl.conf /etc/sysctl.conf.$RANDOM
 57         /usr/bin/\cp /tmp/sysctl.conf /etc/
 58         sysctl -p >/dev/null 2>&1
 59         Msg "kernel paramters"
 60     else
 61         echo "/tmp/sysctl.conf is not exist"
 62     fi
 63 }
 64 
 65 # Defined hostname function
 66 function Hostname(){
 67     ip=`/usr/sbin/ip addr|grep brd|awk 'NR==3{print $2}'|awk -F "/" '{print $1}'`
 68     name=`grep -w "$ip" /etc/hosts|awk '{print $2}'`
 69     if [ -z $name ];then
 70         sleep 1
 71     else
 72         echo $name > /etc/hostname
 73         hostnamectl set-hostname $name
 74         Msg "Hostname"
 75     fi
 76 }
 77 
 78 # Defined device function
 79 function Device(){
 80     /usr/sbin/ip addr|grep eth0  >/dev/null
 81     RETVAL=$?
 82     if [ $RETVAL -ne 0 ];then
 83         /usr/bin/mv /etc/sysconfig/network-scripts/ifcfg-e* /etc/sysconfig/network-scripts/ifcfg-eth0 >/dev/null 2>&1
 84         sed -i 's/quiet/quiet net.ifnames=0 biosdevname=0/g' /etc/default/grub
 85         sed -i 's/^DEVICE/#DEVICE/g' /etc/sysconfig/network-scripts/ifcfg-e*
 86         sed -i '1i DEVICE=eth0' /etc/sysconfig/network-scripts/ifcfg-e*
 87         /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg >/dev/null 2>&1
 88         Msg "Device--[WARNING]effecting after reboot~~~"
 89     else
 90         echo "the name of eths is exist"
 91     fi
 92 }
 93 
 94 # History collect
 95 function History(){
 96     cat >>/etc/profile.d/history.sh <<EOF
 97 #history
 98 USER=\`whoami\`
 99 USER_IP=\`who -u am i 2>/dev/null|egrep -o "([0-9]{1,3}\\.){3}[0-9]{1,3}"\`
100 if [ "\$USER_IP" = "" ]; then
101 USER_IP=\`hostname\`
102 fi
103 if [ ! -d /var/log/history ]; then
104 mkdir /var/log/history
105 chmod 777 /var/log/history
106 fi
107 if [ ! -d /var/log/history/\${LOGNAME} ]; then
108 mkdir /var/log/history/\${LOGNAME}
109 chmod 300 /var/log/history/\${LOGNAME}
110 fi
111 export HISTSIZE=4096
112 DT=\`date +"%Y%m%d_%H:%M:%S"\`
113 export HISTFILE="/var/log/history/\${LOGNAME}/\${USER}@\${USER_IP}_\$DT"
114 chmod 600 /var/log/history/\${LOGNAME}/*history* 2>/dev/null
115 EOF
116     Msg "History collect"
117 }
118 
119 # Defined wait function
120 function Wait(){
121     echo ""
122     echo -n -e "\033[31mTHE SYSTEM IS REBOOTING\033[0m"
123     for ((i=0;i<3;i++))
124     do
125         echo -n "~~ "
126         sleep 1
127     done
128     echo 
129 }
130 
131 # Defined main function
132 function main(){
133 Selinux
134 Firewalld
135 Bashrc
136 Openfile
137 Kernel
138 Hostname
139 History
140 #Device
141 Wait 
142 reboot
143 }
144 main

  若有其他需求,可以其为基底进行个性化修改!