页首自改代码

Hey, Nice to meet You.  why ?

☆☆☆所谓豪杰之士,必有过人之节.人情有所不能忍者,匹夫见辱,拔剑而起,挺身而斗,此不足为勇也,天下有大勇者,猝然临之而不惊,无故加之而不怒.此其所挟持者甚大,而其志甚远也.          
返回顶部

Centos7系统初始化脚本

前言:

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

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

说明:

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

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

  1)SELinux关闭;

  2)Firewalld关闭;

  3)Bash环境修改;

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

  5)系统内核参数优化配置;

  6)Hostname主机名修改;

  7)History历史记录配置;

  8)个性化配置等。

注意:

  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 for Centos6.
 45 function Openfile6(){
 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 open files function for Centos7.
 54 function Openfile7(){
 55     if [ `egrep "^De" /etc/systemd/system.conf|wc -l` -eq 0 ];then
 56         echo 'DefaultLimitCORE=infinity' >> /etc/systemd/system.conf
 57         echo 'DefaultLimitNOFILE=100000' >> /etc/systemd/system.conf
 58         echo 'DefaultLimitNPROC=100000' >> /etc/systemd/system.conf
 59         ulimit -SHn 100000
 60         Msg "Open files" 
 61     fi
 62 }
 63 
 64 # Defined kernel paramters function
 65 function Kernel(){
 66     if [ -f /tmp/sysctl.conf ];then 
 67         /usr/bin/\cp /etc/sysctl.conf /etc/sysctl.conf.$RANDOM
 68         /usr/bin/\cp /tmp/sysctl.conf /etc/
 69         sysctl -p >/dev/null 2>&1
 70         Msg "kernel paramters"
 71     else
 72         echo "/tmp/sysctl.conf is not exist"
 73     fi
 74 }
 75 
 76 # Defined hostname function
 77 function Hostname(){
 78     ip=`/usr/sbin/ip addr|grep brd|awk 'NR==3{print $2}'|awk -F "/" '{print $1}'`
 79     name=`grep -w "$ip" /etc/hosts|awk '{print $2}'`
 80     if [ -z $name ];then
 81         sleep 1
 82     else
 83         echo $name > /etc/hostname
 84         hostnamectl set-hostname $name
 85         Msg "Hostname"
 86     fi
 87 }
 88 
 89 # Defined device function
 90 function Device(){
 91     /usr/sbin/ip addr|grep eth0  >/dev/null
 92     RETVAL=$?
 93     if [ $RETVAL -ne 0 ];then
 94         /usr/bin/mv /etc/sysconfig/network-scripts/ifcfg-e* /etc/sysconfig/network-scripts/ifcfg-eth0 >/dev/null 2>&1
 95         sed -i 's/quiet/quiet net.ifnames=0 biosdevname=0/g' /etc/default/grub
 96         sed -i 's/^DEVICE/#DEVICE/g' /etc/sysconfig/network-scripts/ifcfg-e*
 97         sed -i '1i DEVICE=eth0' /etc/sysconfig/network-scripts/ifcfg-e*
 98         /usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg >/dev/null 2>&1
 99         Msg "Device--[WARNING]effecting after reboot~~~"
100     else
101         echo "the name of eths is exist"
102     fi
103 }
104 
105 # History collect
106 function History(){
107     cat >>/etc/profile.d/history.sh <<EOF
108 #history
109 USER=\`whoami\`
110 USER_IP=\`who -u am i 2>/dev/null|egrep -o "([0-9]{1,3}\\.){3}[0-9]{1,3}"\`
111 if [ "\$USER_IP" = "" ]; then
112 USER_IP=\`hostname\`
113 fi
114 if [ ! -d /var/log/history ]; then
115 mkdir /var/log/history
116 chmod 777 /var/log/history
117 fi
118 if [ ! -d /var/log/history/\${LOGNAME} ]; then
119 mkdir /var/log/history/\${LOGNAME}
120 chmod 300 /var/log/history/\${LOGNAME}
121 fi
122 export HISTSIZE=4096
123 DT=\`date +"%Y%m%d_%H:%M:%S"\`
124 export HISTFILE="/var/log/history/\${LOGNAME}/\${USER}@\${USER_IP}_\$DT"
125 chmod 600 /var/log/history/\${LOGNAME}/*history* 2>/dev/null
126 EOF
127     Msg "History collect"
128 }
129 
130 # Defined the hobby.
131 function Hobby(){
132     mkdir -p /{luomurui,luomurui-bak}/{scr,pkg,test,info}
133 }
134 
135 # Defined wait function
136 function Wait(){
137     echo ""
138     echo -n -e "\033[31mTHE SYSTEM IS REBOOTING\033[0m"
139     for ((i=0;i<3;i++))
140     do
141         echo -n "~~ "
142         sleep 1
143     done
144     echo 
145 }
146 
147 # Defined main function
148 function main(){
149 Selinux
150 Firewalld
151 Bashrc
152 #Openfile6
153 Openfile7
154 Kernel
155 Hostname
156 #Device
157 History
158 Hobby
159 Wait 
160 reboot
161 }
162 main

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

posted on 2019-01-24 10:58  罗穆瑞  阅读(1378)  评论(0编辑  收藏  举报

导航