Centos 6.2 局部优化脚本,适用于生产环境

 
  
  1. #!/bin/bash 
  2. #author suzezhi 
  3. #this script is only for CentOS 6 
  4. #check the OS 
  5.  
  6. #先对系统进行判断,如果是Cent OS 64位,就继续运行 
  7. platform=`uname -i` 
  8. if [ $platform != "x86_64" ];then  
  9. echo "this script is only for 64bit Operating System !" 
  10. exit 1 
  11. fi 
  12. echo "the platform is ok" 
  13. version=`lsb_release -r |awk '{print substr($2,1,1)}'
  14. if [ $version != 6 ];then 
  15. echo "this script is only for CentOS 6 !" 
  16. exit 1 
  17. fi 
  18. cat << EOF 
  19. +---------------------------------------+ 
  20. |   your system is CentOS 6 x86_64      | 
  21. |      start optimizing.......          | 
  22. +--------------------------------------- 
  23. EOF 
  24.  
  25. #先将系统的安装源设置为网易的(网易的安装源算是国内比较稳定的) 
  26. #make the 163.com as the default yum repo 
  27. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 
  28. wget http://mirrors.163.com/.help/CentOS6-Base-163.repo -O /etc/yum.repos.d/CentOS-Base.repo 
  29.  
  30. #安装epel的源和rpmforge的源,利用第三方的源来让yum安装起来更方便 
  31. #add the third-party repo 
  32. #add the epel 
  33. rpm -Uvh http://download.Fedora.RedHat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm  
  34. rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6 
  35.  
  36. #add the rpmforge 
  37. rpm -Uvh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm 
  38. rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-rpmforge-dag 
  39.  
  40. #更新系统及已安装的软件,建议删除不需要的软件后进行更新; 
  41. #update the system and set the ntp 
  42. yum clean all 
  43. yum -y update glibc\* 
  44. yum -y update yum\* rpm\* python\*  
  45.  
  46. #执行系统更新,注意:如果你的服务器磁盘做了阵列,请查看阵列驱动与操作系统的兼容性 
  47. yum -y update 
  48.  
  49. #安装NTP服务,设置为每天凌晨四点进行时间同步(跟国家授时中心的服务器进行时间同步) 
  50. yum -y install ntp 
  51. echo "* 4 * * * /usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1" >> /var/spool/cron/root 
  52. service crond restart 
  53.  
  54. #将系统同时打开的文件个数增大 
  55. #set the file limit 
  56. echo "ulimit -SHn 102400" >> /etc/rc.local 
  57. cat >> /etc/security/limits.conf << EOF 
  58. *           soft   nofile       65535 
  59. *           hard   nofile       65535 
  60. EOF 
  61.  
  62. #将ctrl alt delete键进行屏蔽,防止误操作的时候服务器重启 
  63. #set the control-alt-delete to guard against the miSUSE 
  64. sed -i 's#exec /sbin/shutdown -r now#\#exec /sbin/shutdown -r now#' /etc/init/control-alt-delete.conf 
  65.  
  66. #关闭selinux 
  67. #disable selinux 
  68. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config 
  69.  
  70. #禁用GSSAPI来认证 
  71. #set ssh 
  72. sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config 
  73. #禁用DNS反向解析 
  74. sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config 
  75. service sshd restart 
  76.  
  77. #优化一些内核参数,主要为网络性能 
  78. #tune kernel parametres 
  79. cat >> /etc/sysctl.conf << EOF 
  80. net.ipv4.tcp_fin_timeout = 1 
  81. net.ipv4.tcp_keepalive_time = 1200 
  82. net.ipv4.tcp_mem = 94500000 915000000 927000000 
  83. net.ipv4.tcp_tw_reuse = 1 
  84. net.ipv4.tcp_timestamps = 0 
  85. net.ipv4.tcp_synack_retries = 1 
  86. net.ipv4.tcp_syn_retries = 1 
  87. net.ipv4.tcp_tw_recycle = 1 
  88. net.core.rmem_max = 16777216 
  89. net.core.wmem_max = 16777216 
  90. net.core.netdev_max_backlog = 262144 
  91. net.core.somaxconn = 262144 
  92. net.ipv4.tcp_max_orphans = 3276800 
  93. net.ipv4.tcp_max_syn_backlog = 262144 
  94. net.core.wmem_default = 8388608 
  95. net.core.rmem_default = 8388608 
  96. EOF 
  97. /sbin/sysctl -p 
  98.  
  99. #调整删除字符的按键为backspace(某些系统默认是delete) 
  100. #define the backspace button can erase the last character typed 
  101. echo 'stty erase ^H' >> /etc/profile 
  102. #打开vim的语法高亮 
  103. echo "syntax on" >> /root/.vimrc 
  104.  
  105. #取消生成whatis数据库和locate数据库 
  106. #stop some crontab 
  107. mkdir /etc/cron.daily.bak 
  108. mv /etc/cron.daily/makewhatis.cron /etc/cron.daily.bak 
  109. mv /etc/cron.daily/mlocate.cron /etc/cron.daily.bak 
  110.  
  111. #关闭不常用的服务,根据实际情况而定
  112. chkconfig bluetooth off 
  113. chkconfig cups off 
  114. chkconfig ip6tables off 
  115.  
  116. #禁用IPV6 
  117. #disable the ipv6 
  118. cat > /etc/modprobe.d/dist.conf << EOFI 
  119. alias net-pf-10 off 
  120. options ipv6 disable=1 
  121. EOFI 
  122. echo "NETWORKING_IPV6=off" >> /etc/sysconfig/network 
  123. cat << EOF 
  124. +-------------------------------------------------+ 
  125. |               optimizer is done                 | 
  126. |   it's recommond to restart this server !       | 
  127. +-------------------------------------------------+ 
  128. EOF