前言:
因公司業務增加,陸續新增伺服器,時不時的來幾臺,手動地一臺臺對伺服器初始化操作感覺太麻煩。於是乎,根據初始化需求整合了一個初始化指令碼,實現批次指令碼初始化操作。
說明:
本指令碼根據自身需求編寫而成,整合了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主機名設定成 「jumpserver」 ;
C)確儲存在 /tmp/sysctl.conf 檔案,即將已設定好的Kernel核心優化引數檔案放置 /tmp 目錄下,執行完指令碼後,其優化引數將自動設定到伺服器中;如無優化檔案,即在最後的函數中註釋 Kernel 即可;
內容:
指令碼內容如下:
[root@jumpserver ~]# cat /shell/init.sh
在這裡插入程式碼片
```#!/bin/bash
#====================================================
# Author: Mr.song
# Blog: https://blog.csdn.net/qq_46229380/article/details/108982089
# Create Date: 2020-10-9
# Description: It works for system initalization.
#====================================================
#State:Plese confirm the files of /etc/hosts and /tmp/sysctl.conf before using the script
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
# Defined result function
function Msg(){
if [ $? -eq 0 ];then
action "$1" /bin/true
else
action "$1" /bin/false
fi
}
# Defined close selinux function
function Selinux(){
[ -f /etc/selinux/config ] && {
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
Msg "Close selinux"
}
}
# Defined close firewalld function
function Firewalld(){
systemctl stop firewalld.service
systemctl disable firewalld.service >/dev/null 2>&1
Msg "Close firewalld"
}
# Defined bashrc function
function Bashrc(){
sed -i 's/\\h \\W/\\h \\w/g' /etc/bashrc
Msg "Bashrc"
}
# Defined open files function for Centos6.
function Openfile6(){
if [ `egrep "^\*" /etc/security/limits.conf|wc -l` -eq 0 ];then
echo '* - nofile 65535' >> /etc/security/limits.conf
ulimit -SHn 65535
Msg "Open files"
fi
}
# Defined open files function for Centos7.
function Openfile7(){
if [ `egrep "^De" /etc/systemd/system.conf|wc -l` -eq 0 ];then
echo 'DefaultLimitCORE=infinity' >> /etc/systemd/system.conf
echo 'DefaultLimitNOFILE=100000' >> /etc/systemd/system.conf
echo 'DefaultLimitNPROC=100000' >> /etc/systemd/system.conf
ulimit -SHn 100000
Msg "Open files"
fi
}
# Defined kernel paramters function
function Kernel(){
if [ -f /tmp/sysctl.conf ];then
/usr/bin/\cp /etc/sysctl.conf /etc/sysctl.conf.$RANDOM
/usr/bin/\cp /tmp/sysctl.conf /etc/
sysctl -p >/dev/null 2>&1
Msg "kernel paramters"
else
echo "/tmp/sysctl.conf is not exist"
fi
}
# Defined hostname function
function Hostname(){
ip=`/usr/sbin/ip addr|grep brd|awk 'NR==3{print $2}'|awk -F "/" '{print $1}'`
name=`grep -w "$ip" /etc/hosts|awk '{print $2}'`
if [ -z $name ];then
sleep 1
else
echo $name > /etc/hostname
hostnamectl set-hostname $name
Msg "Hostname"
fi
}
# Defined device function
function Device(){
/usr/sbin/ip addr|grep ens192 >/dev/null
RETVAL=$?
if [ $RETVAL -ne 0 ];then
/usr/bin/mv /etc/sysconfig/network-scripts/ifcfg-e* /etc/sysconfig/network-scripts/ifcfg-ens192 >/dev/null 2>&1
sed -i 's/quiet/quiet net.ifnames=0 biosdevname=0/g' /etc/default/grub
sed -i 's/^DEVICE/#DEVICE/g' /etc/sysconfig/network-scripts/ifcfg-e*
sed -i '1i DEVICE=ens192' /etc/sysconfig/network-scripts/ifcfg-e*
/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg >/dev/null 2>&1
Msg "Device--[WARNING]effecting after reboot~~~"
else
echo "the name of eths is exist"
fi
}
# History collect
function History(){
cat >>/etc/profile.d/history.sh <<EOF
#history
USER=\`whoami\`
USER_IP=\`who -u am i 2>/dev/null|egrep -o "([0-9]{1,3}\\.){3}[0-9]{1,3}"\`
if [ "\$USER_IP" = "" ]; then
USER_IP=\`hostname\`
fi
if [ ! -d /var/log/history ]; then
mkdir /var/log/history
chmod 777 /var/log/history
fi
if [ ! -d /var/log/history/\${LOGNAME} ]; then
mkdir /var/log/history/\${LOGNAME}
chmod 300 /var/log/history/\${LOGNAME}
fi
export HISTSIZE=4096
DT=\`date +"%Y%m%d_%H:%M:%S"\`
export HISTFILE="/var/log/history/\${LOGNAME}/\${USER}@\${USER_IP}_\$DT"
chmod 600 /var/log/history/\${LOGNAME}/*history* 2>/dev/null
EOF
Msg "History collect"
}
# Defined the hobby.
function Hobby(){
mkdir -p /{luomurui,luomurui-bak}/{scr,pkg,test,info}
}
# Defined wait function
function Wait(){
echo ""
echo -n -e "\033[31mTHE SYSTEM IS REBOOTING\033[0m"
for ((i=0;i<3;i++))
do
echo -n "~~ "
sleep 1
done
echo
}
# Defined main function
function main(){
Selinux
Firewalld
Bashrc
#Openfile6
Openfile7
Kernel
Hostname
Device
History
Hobby
Wait
reboot
}
main