CentOS系統初始化腳本

     系統初始化腳本是剛安裝完操作系統之後運行的腳本,主要功能是將系統環境進行優化,並更改常用設置,統一系統環境配置,該腳本重複運行不會修改多次,運行完成後最好重啓一下系統。

腳本功能:

         1.       安裝EPEL源並安裝常用的lib

         2.       開啓必要的服務,關於服務的策略是將所有的服務設置爲off,只將必要的系統服務設置爲on,具體開啓的服務需要根據實際環境配置

         3.       關閉SELinux

         4.       LANG設置爲en_US.UTF-8

         5.       將歷史命令記錄設置爲100

         6.       記錄每個登陸用戶運行的命令,除了root(或者用戶有sudo權限)其他用戶都無法修改和刪除用戶的歷史命令日誌文件,確保安全

         7.       修改系統的hard/soft限制

         8.       優化常用內核參數

         9.       同步系統時間,將ntp加入cron

         10.   創建默認的運維人員賬戶,給予sudo權限,並設置下次登陸強制修改密碼

         11.   設置完畢後重啓系統

功能演示:

1.       運行腳本輸出,每項設置成功都會輸出綠色done,如果設置開啓的服務不存在,則會用紅色 not exits標出,已有的用戶不會重複添加,最後提示是否立刻重啓:


 新增的運維賬號首次登陸需修改密碼,默認密碼在腳本中修改:

可以查看所有登陸用戶的運行命令:

除了root(或者用戶有sudo權限)其他用戶都無權限修改或刪除命令日誌文件

腳本源代碼:

  1. #!/bin/bash   
  2. #=============================================================================== 
  3. #          FILE: linux_init.sh 
  4.  
  5. #   DESCRIPTION: This script is used to install usual libs, close unnecessary services,optimize kernel parameters and so on 
  6. #        BLOG: http://waydee.blog.51cto.com/#  
  7. #       CREATED: 2012-4-17 11:27:19 
  8. #      REVISION: 1.0  
  9. #=============================================================================== 
  10.  
  11. set -o nounset                              # Treat unset variables as an error 
  12.  
  13.  
  14. #############  VARIABLES DEFINED #############  
  15. SRV_TEMP="/tmp/chkconfig_list.tmp"   #SRV_TEMP keep result from chkconfig command 
  16.  
  17. SRV_ON="NetworkManager acpid auditd  crond haldaemon iptables irqbalance kudzu lvm2-monitor mcstrans messagebus microcode_ctl netfs network portmap readahead_early restorecond smartd sshd syslog xfs xinetd yum-updatesd"     #add services to this VARIABLES if you want to start them  when linux start 
  18.  
  19. INSTALL_LIBS="gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel  openldap-clients libxslt-devel libevent-devel libtool-ltdl bison libtool vim-enhanced"    # the libs files will be used later 
  20.  
  21. HISTORY_DIR="/var/log/.history"  #the dir where to log user command 
  22.  
  23. DONE="\e[0;32m\033[1mdone\e[m" 
  24.  
  25. ADMIN_USER="test1 test2 test3" #default users who are administrators 
  26.  
  27. DEFAULT_PASSWD="123456"  #set the default password for administrators,it will be changed when user first login   
  28.  
  29. # install EPEL  
  30. if [ ! -e /etc/yum.repos.d/epel.repo ] 
  31. then 
  32.     rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm 1>/dev/null 
  33.     echo -e "Install EPEL source ${DONE}." 
  34. fi 
  35.  
  36. # install usual libs 
  37. yum -y install ${INSTALL_LIBS} 1>/dev/null 
  38. echo -e "Install the usual libs ${DONE}." 
  39.  
  40.  
  41. # close all services and set necessary services on  
  42. chkconfig  --list | awk '{print $1}' > ${SRV_TEMP} 
  43.  
  44. # close all services  
  45. while read SERVICE 
  46. do 
  47.     chkconfig --level 345 ${SERVICE} off 1>/dev/null  
  48.  
  49. done < ${SRV_TEMP} 
  50.  
  51. #open necessary services  
  52. for SRVS in ${SRV_ON} 
  53. do 
  54.     if [ -e /etc/init.d/${SRVS} ] 
  55.     then  
  56.         chkconfig --level 345 ${SRVS} on 1>/dev/null 
  57.     else 
  58.         echo -e "Service ${SRVS} is \e[0;31m\033[1mnot exits\e[m." 
  59.     fi 
  60.      
  61. done 
  62.  
  63. #check if the server is vmware virtual machine 
  64. chkconfig  --list|grep vmware* 2>&1 /dev/null 
  65. GREP_STATUS=$? 
  66. if [[ ${GREP_STATUS} == 0 ]] 
  67. then 
  68.     chkconfig --level 345 vmware on 1>/dev/null  
  69.     chkconfig --level 345 vmware-USBArbitrator on 1>/dev/null  
  70.     chkconfig --level 345 vmware-workstation-server on 1>/dev/null  
  71.     chkconfig --level 345 vmware-wsx-server on 1>/dev/null  
  72.     chkconfig --level 345 vmamqpd on 1>/dev/null  
  73. fi 
  74.  
  75. echo -e "Stop unnecessary services ${DONE}." 
  76.  
  77.  
  78. # set selinux disabled 
  79. sed -i s/SELINUX=enforcing/SELINUX=disabled/  /etc/selinux/config  
  80. echo -e "Set SELinux disabled ${DONE}." 
  81.  
  82. # set LANG en_US.UTF-8 
  83. sed -i s/LANG=.*$/LANG="en_US.UTF-8"/  /etc/sysconfig/i18n  
  84. echo -e "Set LANG en_US.UTF-8 ${DONE}." 
  85.  
  86. # set history 100 
  87. sed -i s/HISTSIZE=.*/HISTSIZE=100/  /etc/profile 
  88. echo -e "Set history 100 ${DONE}." 
  89.  
  90. #log every command for every user 
  91. if [ ! -d ${HISTORY_DIR} ] 
  92. then 
  93.     mkdir -p ${HISTORY_DIR} 
  94.     chmod -R 1777 ${HISTORY_DIR} 
  95. fi 
  96.  
  97. # use PROMPT_COMMAND to log every user command  
  98. more /etc/profile |grep PROMPT_COMMAND 2>&1 >/dev/null  
  99. COMMAND_STATUS=$? 
  100.  
  101. if [[ ${COMMAND_STATUS} != 0 ]] 
  102. then 
  103. #   export HISTORY_FILE="${HISTORY_DIR}/`date '+%y-%m-%d'`-`whoami`.log" 
  104. cat>>/etc/profile<<EOF 
  105. #log every user command  
  106. export HISTORY_FILE="/var/log/.history/\`whoami\`-\`date '+%y-%m-%d'\`.log" 
  107. export PROMPT_COMMAND='{ date "+%y-%m-%d %H:%M:%S - \$(who am i |awk "{print \\\$1\" \"\\\$2\" \"\\\$5}")  - \$(history 1 | { read x cmd; echo "\$cmd"; })"; } >> \$HISTORY_FILE' 
  108. EOF 
  109.     echo -e "Log user command ${DONE}." 
  110. fi 
  111.  
  112. more /etc/rc.local|fgrep "ulimit -SHn 65535" 2>&1 >/dev/null  
  113. ULIMIT_STATUS=$? 
  114.  
  115. if [[ ${ULIMIT_STATUS} != 0 ]] 
  116. then  
  117. #set linux  limit 
  118. echo "* soft nofile 60000" >> /etc/security/limits.conf  
  119. echo "* hard nofile 65535" >> /etc/security/limits.conf  
  120. echo "ulimit -SHn 65535" >> /etc/rc.local 
  121. echo -e "Set hard/soft limit ${DONE}." 
  122. echo "source /etc/profile" >>/etc/rc.local 
  123. echo "source /etc/profile" >> /root/.bash_profile 
  124. fi  
  125.  
  126. #linux kernel optimize 
  127. cat >/etc/sysctl.conf<<EOF 
  128. net.ipv4.ip_forward = 0 
  129. net.ipv4.conf.default.rp_filter = 1 
  130. net.ipv4.conf.default.accept_source_route = 0 
  131. kernel.sysrq = 0 
  132. kernel.core_uses_pid = 1 
  133. net.ipv4.tcp_syncookies = 1 
  134. #net.bridge.bridge-nf-call-ip6tables = 0 
  135. #net.bridge.bridge-nf-call-iptables = 0 
  136. #net.bridge.bridge-nf-call-arptables = 0 
  137. kernel.msgmnb = 65536 
  138. kernel.msgmax = 65536 
  139. kernel.shmmax = 68719476736 
  140. kernel.shmall = 4294967296 
  141. net.core.rmem_max = 873200 
  142. net.core.wmem_max = 873200 
  143. net.ipv4.tcp_wmem = 8192 436600 873200 
  144. net.ipv4.tcp_rmem = 8192 436600 873200 
  145. net.ipv4.tcp_mem = 786432 1048576 1572864 
  146. net.ipv4.ip_local_port_range = 1024 65000 
  147. net.ipv4.tcp_max_tw_buckets = 180000 
  148. net.ipv4.icmp_echo_ignore_broadcasts = 1 
  149. net.ipv4.tcp_keepalive_probes = 5 
  150. net.ipv4.tcp_keepalive_intvl = 15 
  151. net.ipv4.tcp_retries1 = 3 
  152. net.ipv4.tcp_retries2 = 15 
  153. net.ipv4.tcp_tw_recycle = 1 
  154. net.ipv4.tcp_tw_reuse = 1 
  155. net.ipv4.tcp_max_orphans = 131072 
  156. net.core.somaxconn = 1024  
  157. net.core.netdev_max_backlog = 1000 
  158. net.ipv4.tcp_max_syn_backlog = 20480 
  159. net.ipv4.tcp_synack_retries = 3 
  160. net.ipv4.tcp_syn_retries = 3 
  161. net.ipv4.tcp_window_scaling = 1 
  162. net.ipv4.tcp_fin_timeout = 30 
  163. net.ipv4.tcp_keepalive_time = 1800 
  164. net.ipv4.tcp_sack = 1 
  165. net.ipv4.tcp_timestamps = 0 
  166. EOF 
  167.  
  168. #make optimize effect 
  169. sysctl -p 1>/dev/null 
  170. echo -e "Optimize kernel ${DONE}." 
  171.  
  172.  
  173. #time set 
  174. /usr/sbin/ntpdate  ntp.fudan.edu.cn 1>/dev/null  
  175.  
  176. #add to cron job 
  177. more /var/spool/cron/root |grep "ntp.fudan.edu.cn"  2>&1>/dev/null  
  178. NTP_STATUS=$? 
  179. if [[ ${NTP_STATUS} != 0 ]] 
  180. then 
  181. cat >> /var/spool/cron/root<<EOF 
  182. #time set  
  183. */5 * * * * /usr/sbin/ntpdate  ntp.fudan.edu.cn  
  184. EOF 
  185.     echo -e "Time ntpdate set ${DONE}." 
  186. fi  
  187.  
  188. #add default administrators  
  189. for ADMIN in ${ADMIN_USER} 
  190. do 
  191.     if cat /etc/passwd |awk -F: '{print $1}'|grep ${ADMIN}  2>&1 >/dev/null 
  192.     then 
  193.         echo -e "User ${ADMIN} has been \e[0;32m\033[1madded\e[m." 
  194.     else 
  195.         #add new user, change password when user login  
  196.         useradd ${ADMIN} 
  197.         echo "${DEFAULT_PASSWD}" | passwd --stdin ${ADMIN} 2>&1 >/dev/null 
  198.         usermod -L ${ADMIN} 
  199.         chage -d 0 ${ADMIN} 
  200.         usermod -U ${ADMIN} 
  201.         echo -e "Add User \e[0;32m\033[1m${ADMIN}\e[m done."  
  202.  
  203.         #add user to sudoers file  
  204.         echo "${ADMIN}    ALL=(ALL)       ALL" >> /etc/sudoers 
  205.     fi 
  206. done 
  207.  
  208.  
  209. #init done,and reboot system  
  210. echo -e "Do you want to \e[0;31m\033[1mreboot\e[m system now? [Y/N]:\t " 
  211. read REPLY 
  212. case $REPLY in  
  213.     Y|y) 
  214.         echo "The system will reboot now ..." 
  215.         shutdown -r now  
  216.         ;; 
  217.     N|n) 
  218.         echo "You must reboot later..." 
  219.         source /etc/profile  
  220.         ;; 
  221.     *) 
  222.         echo "You must input [Y/N]." 
  223.         source /etc/profile  
  224.         ;; 
  225. esac 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章