下面介绍的是生产环境Linux(CentOS,RHEL)系统初始化的shell脚本,去除不需要的服务,做些优化、安全和管理相关的配置等等。不一定适应所有的生产环境,还是要根据自己的实际业务应用来做些调整。可以把此脚本整合在PXE中,或其他自动化安装系统的工具中。具体内容详见脚本。

centos_sys_init.sh

 
 
    
  1.  
  2. 001 #!/bin/bash   
  3.  
  4. 002 #   
  5.  
  6. 003 # Script Name: centos_sys_init.sh   
  7.  
  8. 004 # Description: fits CentOS and RHEL series   
  9.  
  10. 005 #   
  11.  
  12. 006 # Author: Xinggang Wang - OpsEye.com   
  13.  
  14. 007 # Create Date: 2010-09-16   
  15.  
  16. 008 # Last Modified: 2011-09-19   
  17.  
  18. 009     
  19.  
  20. 010 # turnoff services of no need   
  21.  
  22. 011 chkconfig="/sbin/chkconfig"   
  23.  
  24. 012 services=`$chkconfig --list|awk '{print $1}'`   
  25.  
  26. 013     
  27.  
  28. 014 for i in $services   
  29.  
  30. 015 do   
  31.  
  32. 016 case $i in   
  33.  
  34. 017 crond|irqbalance|microcode_ctl|network|sshd|syslog|random| \   
  35.  
  36. 018 lm_sensors|lvm2-monitor|mdmonitor|readahead_early|smartd| \   
  37.  
  38. 019 ipmi|iscsi|iscsid|local)   
  39.  
  40. 020 $chkconfig --level 2345 $i on   
  41.  
  42. 021 ;;   
  43.  
  44. 022 *)   
  45.  
  46. 023 $chkconfig $i off   
  47.  
  48. 024 /sbin/service $i stop &>/dev/null   
  49.  
  50. 025 ;;   
  51.  
  52. 026 esac   
  53.  
  54. 027     
  55.  
  56. 028 done   
  57.  
  58. 029     
  59.  
  60. 030 # disable ipv6   
  61.  
  62. 031 cat >>/etc/modprobe.conf <<EOF   
  63.  
  64. 032 alias net-pf-10 off   
  65.  
  66. 033 alias ipv6 off   
  67.  
  68. 034 EOF   
  69.  
  70. 035     
  71.  
  72. 036 # disable selinux   
  73.  
  74. 037 sed -i '/^SELINUX=/s/.*/SELINUX=disabled/' /etc/selinux/config   
  75.  
  76. 038     
  77.  
  78. 039 # delete some users of no need   
  79.  
  80. 040 for i in adm lp shutdown halt news uucp games operator gopher   
  81.  
  82. 041 do   
  83.  
  84. 042 /usr/sbin/userdel $i 2>/dev/null   
  85.  
  86. 043 done   
  87.  
  88. 044     
  89.  
  90. 045 # delete some groups of no need   
  91.  
  92. 046 for i in adm lp news uucp games dip   
  93.  
  94. 047 do   
  95.  
  96. 048 /usr/sbin/groupdel $i 2>/dev/null   
  97.  
  98. 049 done   
  99.  
  100. 050     
  101.  
  102. 051 # set start level 3   
  103.  
  104. 052 grep -q 'id:5' /etc/inittab && sed -i '/^id:/s/5/3/' /etc/inittab   
  105.  
  106. 053     
  107.  
  108. 054 # disable ctrl+alt+del   
  109.  
  110. 055 sed -i '/^ca::ctrlaltdel:/s/^/#/' /etc/inittab   
  111.  
  112. 056     
  113.  
  114. 057 # sysctl.conf   
  115.  
  116. 058 cat >/etc/sysctl.conf<<eof   
  117.  
  118. 059 net.ipv4.ip_forward = 0   
  119.  
  120. 060 net.ipv4.conf.default.rp_filter = 1   
  121.  
  122. 061 net.ipv4.conf.default.accept_source_route = 0   
  123.  
  124. 062 kernel.sysrq = 0   
  125.  
  126. 063 kernel.core_uses_pid = 1   
  127.  
  128. 064 net.ipv4.tcp_syncookies = 1   
  129.  
  130. 065 kernel.msgmnb = 65536   
  131.  
  132. 066 kernel.msgmax = 65536   
  133.  
  134. 067 kernel.shmmax = 68719476736   
  135.  
  136. 068 kernel.shmall = 4294967296   
  137.  
  138. 069 net.ipv4.tcp_max_tw_buckets = 6000   
  139.  
  140. 070 net.ipv4.tcp_sack = 1   
  141.  
  142. 071 net.ipv4.tcp_window_scaling = 1   
  143.  
  144. 072 net.ipv4.tcp_rmem = 4096 87380 4194304   
  145.  
  146. 073 net.ipv4.tcp_wmem = 4096 16384 4194304   
  147.  
  148. 074 net.core.wmem_default = 8388608   
  149.  
  150. 075 net.core.rmem_default = 8388608   
  151.  
  152. 076 net.core.rmem_max = 16777216   
  153.  
  154. 077 net.core.wmem_max = 16777216   
  155.  
  156. 078 net.core.netdev_max_backlog = 262144   
  157.  
  158. 079 net.core.somaxconn = 262144   
  159.  
  160. 080 net.ipv4.tcp_max_orphans = 3276800   
  161.  
  162. 081 net.ipv4.tcp_max_syn_backlog = 262144   
  163.  
  164. 082 net.ipv4.tcp_timestamps = 0   
  165.  
  166. 083 net.ipv4.tcp_synack_retries = 1   
  167.  
  168. 084 net.ipv4.tcp_syn_retries = 1   
  169.  
  170. 085 net.ipv4.tcp_tw_recycle = 1   
  171.  
  172. 086 net.ipv4.tcp_tw_reuse = 1   
  173.  
  174. 087 net.ipv4.tcp_mem = 94500000 915000000 927000000   
  175.  
  176. 088 net.ipv4.tcp_fin_timeout = 1   
  177.  
  178. 089 net.ipv4.tcp_keepalive_time = 1200   
  179.  
  180. 090 net.ipv4.ip_local_port_range = 1024 65535   
  181.  
  182. 091 eof   
  183.  
  184. 092     
  185.  
  186. 093 sysctl -p &>/dev/null   
  187.  
  188. 094     
  189.  
  190. 095 # limits.conf   
  191.  
  192. 096 echo '* - nofile 65535' >> /etc/security/limits.conf   
  193.  
  194. 097     
  195.  
  196. 098 # configure the vim editor   
  197.  
  198. 099 cp /usr/share/vim/vim70/vimrc_example.vim /root/.vimrc   
  199.  
  200. 100 cat >>/root/.vimrc <<eof   
  201.  
  202. 101 set shiftwidth=4   
  203.  
  204. 102 ""set encoding=prc   
  205.  
  206. 103 set encoding=utf-8 fileencodings=utf-8,gbk,gb2312   
  207.  
  208. 104 set nu   
  209.  
  210. 105 set nuw=1   
  211.  
  212. 106 set tabstop=4   
  213.  
  214. 107 ""set ai   
  215.  
  216. 108 hi LineNr ctermfg=DarkCyan ctermbg=black   
  217.  
  218. 109 hi PmenuSel ctermfg=blue ctermbg=grey   
  219.  
  220. 110 eof   
  221.  
  222. 111     
  223.  
  224. 112 sed -i '/^set mouse=a/s/^/"/' /root/.vimrc   
  225.  
  226. 113 sed -i '/filetype plugin indent on/s/^/"""/' /root/.vimrc   
  227.  
  228. 114 sed -i '/set backup/s/^/"""/' /root/.vimrc   
  229.  
  230. 115     
  231.  
  232. 116 cat >>/root/.bashrc<<eof   
  233.  
  234. 117 alias vi='vim'   
  235.  
  236. 118 eof   
  237.  
  238. 119     
  239.  
  240. 120 # history custom   
  241.  
  242. 121 cat >>/etc/profile<<eof   
  243.  
  244. 122 export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"   
  245.  
  246. 123 export HISTSIZE=4096   
  247.  
  248. 124 HISTDIR=/var/log/.hist   
  249.  
  250. 125 DATE=\$(date +%Y%m%d)   
  251.  
  252. 126 [ ! -d \$HISTDIR ] && { mkdir -p \$HISTDIR ;chmod 777 \$HISTDIR ;}   
  253.  
  254. 127 export HISTFILE="\$HISTDIR/\$USER.\$DATE"   
  255.  
  256. 128 chmod 600 \$HISTDIR/* 2>/dev/null   
  257.  
  258. 129     
  259.  
  260. 130 eof   
  261.  
  262. 131     
  263.  
  264. 132 # dns   
  265.  
  266. 133 cat >/etc/resolv.conf<<EOF   
  267.  
  268. 134 nameserver 8.8.8.8   
  269.  
  270. 135 EOF   
  271.  
  272. 136     
  273.  
  274. 137 # ntp   
  275.  
  276. 138 chmod +s /usr/sbin/ntpdate   
  277.  
  278. 139 /usr/sbin/ntpdate time.nist.gov;/sbin/clock -w   
  279.  
  280. 140     
  281.  
  282. 141 cat >/var/spool/cron/root<<EOF   
  283.  
  284. 142 # CRONTAB   
  285.  
  286. 143 SHELL=/bin/bash   
  287.  
  288. 144 TZ="Asia/Shanghai"   
  289.  
  290. 145 PATH="/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"   
  291.  
  292. 146 MAILTO=""   
  293.  
  294. 147 #   
  295.  
  296. 148 1 0 * * * /usr/sbin/ntpdate time.nist.gov;/sbin/clock -w   
  297.  
  298. 149 EOF   
  299.  
  300. 150     
  301.  
  302. 151 cat >>/etc/rc.local <<EOF   
  303.  
  304. 152 /usr/sbin/ntpdate time.nist.gov;/sbin/clock -w   
  305.  
  306. 153 EOF   
  307.  
  308. 154     
  309.  
  310. 155 # ssh-key   
  311.  
  312. 156 mkdir -p /root/.ssh   
  313.  
  314. 157 chmod 700 /root/.ssh   
  315.  
  316. 158 cat >/root/.ssh/authorized_keys<<EOF   
  317.  
  318. 159 your ssh private key   
  319.  
  320. 160 EOF   
  321.  
  322. 161     
  323.  
  324. 162 # ssh config   
  325.  
  326. 163 sed -i '/^UseDNS yes/d;/^PasswordAuthentication yes/d' /etc/ssh/sshd_config   
  327.  
  328. 164 sed -i -e '/^#PasswordAuthentication/a PasswordAuthentication no' -e '/^#UseDNS yes/a UseDNS no' /etc/ssh/sshd_config   
  329.  
  330. 165 sed -i '/^GSSAPIAuthentication/s/^/#/;/^GSSAPICleanupCredentials/s/^/#/' /etc/ssh/sshd_config   
  331.  
  332. 166     
  333.  
  334. 167 # restart   
  335.  
  336. 168 init 6   
  337.  
  338. 169     
  339.  
  340. 170 exit 0