CentOS 7 系統優化腳本
大家好,我是中文妹。
一、介紹
作爲一名運維,經常會部署各種用途的操作系統,但在這些工作中,我們會發現很多工作其實是重複性的勞動,操作的內容也是大同小異,基於這類情況,我們可以把相同的操作做成統一執行的腳本,不同的東西作爲變量手動輸入。節約下來的時間不就可以做更多有意義的事情嗎?
最近在粉絲有推薦下發現一款比較好用的 shell 源碼,也基於此改編了一下,分享給大家。
二、菜單
主菜單:
二級菜單:
主要實現系統的各類優化,比如常用的修改字符集、關閉 selinux、關閉防火牆、安裝常用工具和加快 ssh 登錄等功能。
三、源碼
#!/bin/sh
. /etc/rc.d/init.d/functions
export LANG=zh_CN.UTF-8
#一級菜單
menu1()
{
clear
cat <<eof
----------------------------------------
|**** 歡迎使用cetnos7.9優化腳本 ****|
|**** 博客地址: aaa.al ****|
----------------------------------------
1. 一鍵優化
2. 自定義優化
3. 退出
EOF
read -p "please enter your choice[1-3]:" num1
}
#二級菜單
menu2()
{
clear
cat <<eof
----------------------------------------
|****Please Enter Your Choice:[0-13]****|
----------------------------------------
1. 修改字符集
2. 關閉selinux
3. 關閉firewalld
4. 精簡開機啓動
5. 修改文件描述符
6. 安裝常用工具及修改yum源
7. 優化系統內核
8. 加快ssh登錄速度
9. 禁用ctrl+alt+del重啓
10.設置時間同步
11.history優化
12.返回上級菜單
13.退出
EOF
read -p "please enter your choice[1-13]:" num2
}
#1.修改字符集
localeset()
{
echo "========================修改字符集========================="
cat > /etc/locale.conf <<eof
LANG="zh_CN.UTF-8"
#LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
EOF
source /etc/locale.conf
echo "#cat /etc/locale.conf"
cat /etc/locale.conf
action "完成修改字符集" /bin/true
echo "==========================================================="
sleep 2
}
#2.關閉selinux
selinuxset()
{
selinux_status=`grep "SELINUX=disabled" /etc/sysconfig/selinux | wc -l`
echo "========================禁用SELINUX========================"
if [ $selinux_status -eq 0 ];then
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/sysconfig/selinux
setenforce 0
echo '#grep SELINUX=disabled /etc/sysconfig/selinux'
grep SELINUX=disabled /etc/sysconfig/selinux
echo '#getenforce'
getenforce
else
echo 'SELINUX已處於關閉狀態'
echo '#grep SELINUX=disabled /etc/sysconfig/selinux'
grep SELINUX=disabled /etc/sysconfig/selinux
echo '#getenforce'
getenforce
fi
action "完成禁用SELINUX" /bin/true
echo "==========================================================="
sleep 2
}
#3.關閉firewalld
firewalldset()
{
echo "=======================禁用firewalld========================"
systemctl stop firewalld.service &> /dev/null
echo '#firewall-cmd --state'
firewall-cmd --state
systemctl disable firewalld.service &> /dev/null
echo '#systemctl list-unit-files | grep firewalld'
systemctl list-unit-files | grep firewalld
action "完成禁用firewalld,生產環境下建議啓用!" /bin/true
echo "==========================================================="
sleep 5
}
#4.精簡開機啓動
chkset()
{
echo "=======================精簡開機啓動========================"
systemctl disable auditd.service
systemctl disable postfix.service
systemctl disable dbus-org.freedesktop.NetworkManager.service
echo '#systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"'
systemctl list-unit-files | grep -E "auditd|postfix|dbus-org\.freedesktop\.NetworkManager"
action "完成精簡開機啓動" /bin/true
echo "==========================================================="
sleep 2
}
#5.修改文件描述符
limitset()
{
echo "======================修改文件描述符======================="
echo '* - nofile 65535'>/etc/security/limits.conf
ulimit -SHn 65535
echo "#cat /etc/security/limits.conf"
cat /etc/security/limits.conf
echo "#ulimit -Sn ; ulimit -Hn"
ulimit -Sn ; ulimit -Hn
action "完成修改文件描述符" /bin/true
echo "==========================================================="
sleep 2
}
#6.安裝常用工具及修改yum源
yumset()
{
echo "=================安裝常用工具及修改yum源==================="
yum install wget -y &> /dev/null
if [ $? -eq 0 ];then
cd /etc/yum.repos.d/
\cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)
ping -c 1 mirrors.aliyun.com &> /dev/null
if [ $? -eq 0 ];then
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum makecache &> /dev/null
else
echo "無法連接網絡"
exit $?
fi
else
echo "wget安裝失敗"
exit $?
fi
yum -y install ntpdate lsof net-tools telnet vim lrzsz tree nmap nc sysstat &> /dev/null
action "完成安裝常用工具及修改yum源" /bin/true
echo "==========================================================="
sleep 2
}
#7. 優化系統內核
#另外,搜索公衆號技術社區後臺回覆“壁紙”,獲取一份驚喜禮包。
kernelset()
{
echo "======================優化系統內核========================="
chk_nf=`cat /etc/sysctl.conf | grep conntrack |wc -l`
if [ $chk_nf -eq 0 ];then
cat >>/etc/sysctl.conf<<eof
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 0
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
net.netfilter.nf_conntrack_max = 25000000
net.netfilter.nf_conntrack_tcp_timeout_established = 180
net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
EOF
sysctl -p
else
echo "優化項已存在。"
fi
action "內核調優完成" /bin/true
echo "==========================================================="
sleep 2
}
#8.加快ssh登錄速度
sshset()
{
echo "======================加快ssh登錄速度======================"
sed -i 's#^GSSAPIAuthentication yes$#GSSAPIAuthentication no#g' /etc/ssh/sshd_config
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
systemctl restart sshd.service
echo "#grep GSSAPIAuthentication /etc/ssh/sshd_config"
grep GSSAPIAuthentication /etc/ssh/sshd_config
echo "#grep UseDNS /etc/ssh/sshd_config"
grep UseDNS /etc/ssh/sshd_config
action "完成加快ssh登錄速度" /bin/true
echo "==========================================================="
sleep 2
}
#9. 禁用ctrl+alt+del重啓
restartset()
{
echo "===================禁用ctrl+alt+del重啓===================="
rm -rf /usr/lib/systemd/system/ctrl-alt-del.target
action "完成禁用ctrl+alt+del重啓" /bin/true
echo "==========================================================="
sleep 2
}
#10. 設置時間同步
ntpdateset()
{
echo "=======================設置時間同步========================"
yum -y install ntpdate &> /dev/null
if [ $? -eq 0 ];then
/usr/sbin/ntpdate time.windows.com
echo "*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null" >> /var/spool/cron/root
else
echo "ntpdate安裝失敗"
exit $?
fi
action "完成設置時間同步" /bin/true
echo "==========================================================="
sleep 2
}
#11. history優化
historyset()
{
echo "========================history優化========================"
chk_his=`cat /etc/profile | grep HISTTIMEFORMAT |wc -l`
if [ $chk_his -eq 0 ];then
cat >> /etc/profile <<'EOF'
#設置history格式
export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] [`whoami`] [`who am i|awk '{print $NF}'|sed -r 's#[()]##g'`]: "
#記錄shell執行的每一條命令
export PROMPT_COMMAND='\
if [ -z "$OLD_PWD" ];then
export OLD_PWD=$PWD;
fi;
if [ ! -z "$LAST_CMD" ] && [ "$(history 1)" != "$LAST_CMD" ]; then
logger -t `whoami`_shell_dir "[$OLD_PWD]$(history 1)";
fi;
export LAST_CMD="$(history 1)";
export OLD_PWD=$PWD;'
EOF
source /etc/profile
else
echo "優化項已存在。"
fi
action "完成history優化" /bin/true
echo "==========================================================="
sleep 2
}
#控制函數
main()
{
menu1
case $num1 in
1)
localeset
selinuxset
firewalldset
chkset
limitset
yumset
kernelset
sshset
restartset
ntpdateset
historyset
;;
2)
menu2
case $num2 in
1)
localeset
;;
2)
selinuxset
;;
3)
firewalldset
;;
4)
chkset
;;
5)
limitset
;;
6)
yumset
;;
7)
kernelset
;;
8)
sshset
;;
9)
restartset
;;
10)
ntpdateset
;;
11)
historyset
;;
12)
main
;;
13)
exit
;;
*)
echo 'Please select a number from [1-13].'
;;
esac
;;
3)
exit
;;
*)
echo 'Err:Please select a number from [1-3].'
sleep 3
main
;;
esac
}
main $*
將其保存爲 init.sh,然後賦予執行權限後執行即可。
chmod +x init.sh && ./init.sh
如果這樣來回地複製粘貼很麻煩,也可以通過我的一鍵命令執行,同樣能達到上面的效果:
bash -c "$(curl -L s.aaa.al/init.sh)"
最後,如果大家有想實現的功能,也可以在原有腳本的基礎上進行修改實現。
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/pSmn3qSxWKmnfxG7cyh1dw