以下脚本用于linux系统的初始化脚本,可以在服务器系统安装完毕之后立即执行。脚本结合各位大牛一些参数,已经在CentOS 5下通过。
     使用方法:将其复制,保存为一个shell文件,比如init.sh。将其上传到linux服务器上,执行sh init.sh。建议大家在系统安装后立即执行。
脚本内容:

  1. #!/bin/bash 
  2. #by LinuxEye 
  3. #BLOG: http://blog.linuxeye.com 
  4. #系统瘦身 
  5. yum -y groupremove "FTP Server" "Text-based Internet" "Windows File Server" "PostgreSQL Database" "News Server" "DNS Name Server" "Web Server" "Dialup Networking Support" "Mail Server" "Office/Productivity" "Ruby" "Office/Productivity" "Sound and Video" "X Window System" "X Software Development" "Printing Support" "OpenFabrics Enterprise Distribution"  
  6. #安装依赖包 
  7. yum -y install 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 openldap-servers libxslt-devel libevent-devel ntp  libtool-ltdl bison libtool vim-enhanced 
  8. #关闭不必要的服务 
  9. chkconfig --list | awk '{print "chkconfig " $1 " off"}' > /tmp/chkconfiglist.sh;/bin/sh /tmp/chkconfiglist.sh;rm -rf /tmp/chkconfiglist.sh 
  10. chkconfig  crond on 
  11. chkconfig  irqbalance on 
  12. chkconfig  network on 
  13. chkconfig  sshd on 
  14. chkconfig  syslog on 
  15. chkconfig  iptables on 
  16. setenforce 0 
  17. sed -i 's/^SELINUX=.*$/SELINUX=disabled/g' /etc/sysconfig/selinux
  18. #i18n
    sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  19. #修改启动模式 
  20. sed -i 's/id:.*$/id:3:initdefault:/g' /etc/inittab  
  21. #关闭不需要的tty 
  22. sed -i 's/3:2345:respawn/#3:2345:respawn/g' /etc/inittab 
  23. sed -i 's/4:2345:respawn/#4:2345:respawn/g' /etc/inittab 
  24. sed -i 's/5:2345:respawn/#5:2345:respawn/g' /etc/inittab 
  25. sed -i 's/6:2345:respawn/#6:2345:respawn/g' /etc/inittab 
  26. sed -i 's/ca::ctrlaltdel/#ca::ctrlaltdel/g' /etc/inittab 
  27. /sbin/init q 
  28. #修改PS1 
  29. echo 'PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\$ \[\e[33;40m\]">> /etc/profile 
  30. #修改shell命令的history记录个数 
  31. sed -i 's/HISTSIZE=.*$/HISTSIZE=100/g' /etc/profile 
  32. source /etc/profile 
  33. #记录每个命令 
  34. mkdir /root/logs 
  35. echo "export PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });user=\$(whoami); echo \$(date \"+%Y-%m-%d %H:%M:%S\"):\$user:\`pwd\`/:\$msg ---- \$(who am i); } >> \$HOME/logs/\`hostname\`.\`whoami\`.history-timestamp'>> /root/.bash_profile 
  36. #密码输错5次锁定180s 
  37. sed -i '4a auth        required      pam_tally2.so deny=5 unlock_time=180' /etc/pam.d/system-auth 
  38. #alias设置 
  39. sed -i '7a alias vi=vim' /root/.bashrc  
  40. #调整Linux的最大文件打开数 
  41. echo "* soft nofile 60000" >> /etc/security/limits.conf 
  42. echo "* hard nofile 65535" >> /etc/security/limits.conf 
  43. echo "ulimit -SH 65535" >> /etc/rc.local 
  44. #关闭ipv6 
  45. sed -i 's/NETWORKING_IPV6=.*$/NETWORKING_IPV6=no/g' /etc/sysconfig/network  
  46. #网络参数调整 
  47. sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf 
  48. echo 'net.ipv4.tcp_tw_reuse = 1>> /etc/sysctl.conf 
  49. echo 'net.ipv4.tcp_tw_recycle = 1>> /etc/sysctl.conf 
  50. echo 'net.ipv4.ip_local_port_range = 1024 65000' >> /etc/sysctl.conf 
  51. sysctl -p 
  52. #校正时间 
  53. /usr/sbin/ntpdate ntp.api.bz 
  54. echo '/usr/sbin/ntpdate ntp.api.bz > /dev/null 2>&1' > /var/spool/cron/root;chmod 600 /var/spool/cron/root 
  55. /sbin/service crond restart 
  56. #iptables配置 
  57. sed -i 's/IPTABLES_MODULES="ip_conntrack_netbios_ns"/#IPTABLES_MODULES="ip_conntrack_netbios_ns"/g' /etc/sysconfig/iptables-config 
  58. cat > /etc/sysconfig/iptables << EOF 
  59. # Firewall configuration written by system-config-securitylevel 
  60. # Manual customization of this file is not recommended. 
  61. *filter 
  62. :INPUT DROP [0:0] 
  63. :FORWARD ACCEPT [0:0] 
  64. :OUTPUT ACCEPT [0:0] 
  65. -A INPUT -i lo -j ACCEPT 
  66. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
  67. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
  68. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 
  69. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT 
  70. -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT 
  71. # -A INPUT -p udp -m udp --dport 20 -j ACCEPT 
  72. COMMIT 
  73. EOF 
  74. /sbin/service atd start 
  75. echo "/sbin/service iptables stop" | at now+3minutes 
  76. /sbin/service iptables restart