系统初始化脚本是刚安装完操作系统之后运行的脚本,主要功能是将系统环境进行优化,并更改常用设置,统一系统环境配置,该脚本重复运行不会修改多次,运行完成后最好重启一下系统。

脚本功能:

         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标出,已有的用户不会重复添加,最后提示是否立刻重启:

CentOS系统初始化脚本_初始化脚本


CentOS系统初始化脚本_初始化脚本_02

 新增的运维账号首次登陆需修改密码,默认密码在脚本中修改:

CentOS系统初始化脚本_centos_03

可以查看所有登陆用户的运行命令:

CentOS系统初始化脚本_初始化脚本_04

CentOS系统初始化脚本_bash_05

除了root(或者用户有sudo权限)其他用户都无权限修改或删除命令日志文件

CentOS系统初始化脚本_bash_06

CentOS系统初始化脚本_bash_07

脚本源代码:

  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