linux系統最小化安裝後的初始化腳本

 

做爲運維人員,常常會初始化系統,系統在安裝過程當中基本都會選擇最小化安裝,這樣安裝好的系統裏會缺乏不少環境。node

下面分享一個系統安裝後的初始化腳本:linux

#!/bin/bash

#系統時最小化安裝的,這裏要安裝系統的軟件庫
yum groupinstall -y "development tools"

#建立目錄
[ ! -d /server/tools ] && mkdir -p /server/tools
[ ! -d /application ] && mkdir -p /application
[ ! -d /data ] && mkdir -p /data
[ ! -d /app/logs ] && mkdir -p /app/logs
[ ! -d /server/backup ] && mkdir -p /server/backup
[ ! -d /delete ] && mkdir -p /delete

#每週六凌晨1點0分更新服務器系統時間
echo "############### auto update time ###############" >> /var/spool/cron/root
echo "00 01 * * *	/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1" >> /var/spool/cron/root
[ `grep ntpdate /var/spool/cron/root |wc -l` -ne 0 ] && action "uptime set" /bin/true || action "uptime set" /bin/false

#配置yum源
wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS6-Base-163.repo #下載配置文件
/bin/mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
/bin/cp /etc/yum.repos.d/CentOS6-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo
[ `grep 163.com /etc/yum.repos.d/CentOS-Base.repo | wc -l` -ne 0 ] && action "yum set" /bin/true || action "yum set" /bin/false

#關閉SELINUX及iptables
/bin/cp /etc/selinux/config /etc/selinux/config.bak
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 2>&1
/etc/init.d/iptables stop >/dev/null
chkconfig iptables off >/dev/null
[ `chkconfig --list |grep iptables|grep 3:on|wc -l` -eq 0 -a `grep "SELINUX=enforcing" /etc/selinux/config|wc -l` -eq 0 ] && action "iptables and selinux close" /bin/true || action "iptables and selinux close" /bin/false

#調整文件描述符數量
/bin/cp /etc/security/limits.conf /etc/security/limits.conf.bak
echo '*	-	nofile	65535'>>/etc/security/limits.conf
[ `tail -1 /etc/security/limits.conf|grep 65535|wc -l` -eq 1 ] && action "limit set" /bin/true || action "limit set" /bin/false

#更改字符集
/bin/cp /etc/sysconfig/i18n /etc/sysconfig/i18n.bak
echo 'LANG="en_US.UTF-8"' >/etc/sysconfig/i18n

#定時清理/var/spool/clientmqueue/目錄下的垃圾文件,防止inodes節點被佔滿
##建立腳本目錄
[ ! -d /server/scripts ] && mkdir -p /server/scripts
if [ `rpm -qa sendmail |wc -l` -ne 0 ];then
##建立查找刪除腳本
echo >/server/scripts/del.sh<<EOF
#!/bin/bash
find /var/spool/clientmqueue/ -type f|xargs rm -f >/dev/null 2>&1
EOF
##添加到定時任務,每週一凌晨0點0分執行
echo '################ clean /var/spool/clientmqueue/ ################' >>/var/spool/cron/root
echo '00 00 * * 1	/bin/sh /server/scripts/del.sh >/dev/null 2>&1' >>/var/spool/cron/root
[ "$?" -eq 0 ] && action "clean /var/spool/clientmqueue/ set" /bin/true || action "clean /var/spool/clientmqueue/ set" /bin/false
else
action "service sendmail is not installed,do not need set" /bin/false
fi

#精簡開機自啓動服務(只啓動crond,sshd,network,syslog)
##篩選出全部在運行級別3自啓動的服務並關閉自啓動
for cgt in `chkconfig --list | grep 3:on | awk '{print $1}'`;do chkconfig --level 3 $cgt off;done
##僅設置crond,sshd,network,syslog自啓動
for cgt in {crond,sshd,network,rsyslog};do chkconfig --level 3 $cgt on;done
flag=0
[ `chkconfig --list|grep 3:on|wc -l` -eq 4 ] && action "auto_start services set" /bin/true || action "auto_start services set" /bin/false

#內核參數優化
[ -f /etc/sysctl.conf.bak ] && /bin/cp /etc/sysctl.conf.bak /etc/sysctl.conf.bak.$(date +%F-%H%M%S) ||/bin/cp /etc/sysctl.conf /etc/sysctl.conf.bak
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 = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
#如下參數是對iptables防火牆的優化,防火牆不開會提示,能夠忽略不理。
#net.ipv4.ip_conntrack_max = 25000000
#net.ipv4.netfilter.ip_conntrack_max=25000000
#net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=180
#net.ipv4.netfilter.ip_conntrack_tcp_timeout_time_wait=120 
#net.ipv4.netfilter.ip_conntrack_tcp_timeout_close_wait=60
#net.ipv4.netfilter.ip_conntrack_tcp_timeout_fin_wait=120
#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 >/dev/null 2>&1
[ `grep "net.ipv4.tcp_max_orphans = 16384" /etc/sysctl.conf|wc -l` -ne 0 ] && action "kernel set" /bin/true || action "kernel set" /bin/false

#更改默認的ssh服務端口,禁止root用戶遠程鏈接,禁止空密碼鏈接
/bin/cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
#sed -i 's/\#Port 22/Port 52113/' /etc/ssh/sshd_config
sed -i 's/\#PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config
sed -i 's/\#PermitEmptyPasswords no/PermitEmptyPasswords no/' /etc/ssh/sshd_config
sed -i 's/\#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
#[ `grep "Port 52113" /etc/ssh/sshd_config |wc -l` -ne 0 -a `grep "PermitRootLogin no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "PermitEmptyPasswords no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "UseDNS no" /etc/ssh/sshd_config|wc -l` -ne 0 ] &&action "ssh set" /bin/true || action "ssh set" /bin/false
[ `grep "PermitRootLogin no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "PermitEmptyPasswords no" /etc/ssh/sshd_config|wc -l` -ne 0 -a `grep "UseDNS no" /etc/ssh/sshd_config|wc -l` -ne 0 ] &&action "ssh set" /bin/true || action "ssh set" /bin/false

#鎖定關鍵系統文件
chattr +ai /etc/passwd
chattr +ai /etc/shadow
chattr +ai /etc/group
chattr +ai /etc/gshadow
chattr +ai /etc/inittab

#清空/etc/issue,去除系統及內核版本登錄前的屏幕顯示
/bin/cp /etc/issue /etc/issue.bak
>/etc/issue
[ `cat /etc/issue|wc -l` -eq 0 ] && action "/etc/issue set" /bin/true || action "/etc/issue set" /bin/false

本身整理的服務器安裝後的初始化腳本:
下載:https://pan.baidu.com/s/1caZ3GE
提取密碼:d2xrbash

相關文章
相關標籤/搜索