作為運維人員,經常會初始化系統,系統在安裝過程中基本都會選擇最小化安裝,這樣安裝好的系統裡會缺少很多環境。
下面分享一個系統安裝後的初始化指令碼:
#!/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
提取密碼:d2xr