Linux初始化脚本

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

  1. #!/bin/bash  
  2. # Configure yum source  
  3. cd /tmp  
  4. wget -c http://yum.baseurl.org/download/3.4/yum-3.4.3.tar.gz  
  5. tar zxf yum-3.4.3.tar.gz  
  6. cd yum-3.4.3  
  7. ./yummain.py install yum -y  
  8. cd ..  
  9. rm -rf yum-3.4.3*  
  10. sed -i ‘s@^exclude@#exclude@’ /etc/yum.conf  
  11. yum clean all  
  12. yum check-update  
  13.   
  14. mv /etc/yum.repos.d/CentOS-Debuginfo.repo /etc/yum.repos.d/CentOS-Debuginfo.repo$(date +%m%d)  
  15. mv /etc/yum.repos.d/CentOS-Media.repo /etc/yum.repos.d/CentOS-Media.repo$(date +%m%d)  
  16. mv /etc/yum.repos.d/CentOS-Vault.repo /etc/yum.repos.d/CentOS-Vault.repo$(date +%m%d)  
  17.   
  18. # Remove obsolete rpm package  
  19. if [ -z “$(cat /etc/redhat-release | grep ‘5\.’)” ];then  
  20.     yum -y groupremove “FTP Server” “Text-based Internet” “Windows File Server” “PostgreSQL Database” “News Server” “MySQL Database” “DNS Name Server” “Web Server” “Dialup Networking Support” “Mail Server” “Ruby” “Office/Productivity” “Sound and Video” “X Window System” “X Software Development” “Printing Support” “OpenFabrics Enterprise Distribution”  
  21. else  
  22.     yum -y groupremove “FTP Server” “PostgreSQL Database client” “PostgreSQL Database server” “MySQL Database server” “MySQL Database client” “Web Server” “Office Suite and Productivity” “Ruby Support” “X Window System” “Printing client” “Desktop*”  
  23. fi  
  24.   
  25. # Update rpm package  
  26. yum -y update  
  27.   
  28. # Install dependencies package  
  29. yum -y install gcc gcc-c++ make 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 libtool-ltdl bison gd-devel vim-enhanced pcre-devel zip unzip sendmail  
  30.   
  31. # chkconfig   
  32. chkconfig —list | awk ‘{print “chkconfig ” $1 ” off”}’ > /tmp/chkconfiglist.sh;/bin/sh /tmp/chkconfiglist.sh;rm -rf /tmp/chkconfiglist.sh  
  33. chkconfig crond on  
  34. chkconfig irqbalance on  
  35. chkconfig network on  
  36. chkconfig sshd on  
  37. chkconfig rsyslog on #CentOS 6  
  38. chkconfig syslog on #CentOS/RHEL 5  
  39. chkconfig iptables on  
  40. chkconfig sendmail on  
  41. service sendmail restart  
  42.   
  43. # /etc/hosts  
  44. [ “$(hostname -i)” != “127.0.0.1” ] && sed -i “s@^127.0.0.1\(.*\)@127.0.0.1   `hostname` \1@” /etc/hosts  
  45.   
  46. # Close SELINUX  
  47. setenforce 0  
  48. sed -i ‘s/^SELINUX=.*$/SELINUX=disabled/’ /etc/selinux/config  
  49.   
  50. # initdefault  
  51. sed -i ‘s/^id:.*$/id:3:initdefault:/’ /etc/inittab  
  52. /sbin/init q  
  53. # PS1  
  54. echo ‘PS1=”\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\$ \[\e[33;40m\]”‘ >> /etc/profile  
  55.   
  56. # Record command  
  57. sed -i ‘s/^HISTSIZE=.*$/HISTSIZE=100/’ /etc/profile  
  58. 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); } >> /tmp/\`hostname\`.\`whoami\`.history-timestamp'” >> /root/.bash_profile  
  59.   
  60. # Wrong password five times locked 180s  
  61. sed -i ‘4a auth        required      pam_tally2.so deny=5 unlock_time=180’ /etc/pam.d/system-auth  
  62.   
  63. # alias vi  
  64. sed “s@alias mv=.*@alias mv=’mv -i’\nalias vi=vim@” /root/.bashrc  
  65. echo ‘syntax on’ >> /etc/vimrc  
  66.   
  67. # /etc/security/limits.conf  
  68. cat >> /etc/security/limits.conf <<EOF  
  69. * soft nproc 65535  
  70. * hard nproc 65535  
  71. * soft nofile 65535  
  72. * hard nofile 65535  
  73. EOF  
  74. echo “ulimit -SH 65535” >> /etc/rc.local  
  75.   
  76. # /etc/sysctl.conf  
  77. sed -i ‘s/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g’ /etc/sysctl.conf  
  78. cat >> /etc/sysctl.conf << EOF  
  79. fs.filemax=65535  
  80. net.ipv4.tcp_tw_reuse = 1  
  81. net.ipv4.tcp_tw_recycle = 1  
  82. net.ipv4.ip_local_port_range = 1024 65000  
  83. EOF  
  84. sysctl -p  
  85.   
  86. if [ -z “$(cat /etc/redhat-release | grep ‘6\.’)” ];then  
  87.     sed -i ‘s/3:2345:respawn/#3:2345:respawn/g’ /etc/inittab  
  88.     sed -i ‘s/4:2345:respawn/#4:2345:respawn/g’ /etc/inittab  
  89.     sed -i ‘s/5:2345:respawn/#5:2345:respawn/g’ /etc/inittab  
  90.     sed -i ‘s/6:2345:respawn/#6:2345:respawn/g’ /etc/inittab  
  91.     sed -i ‘s/ca::ctrlaltdel/#ca::ctrlaltdel/g’ /etc/inittab  
  92.     sed -i ‘s@LANG=.*$@LANG=”en_US.UTF-8″@g’ /etc/sysconfig/i18n  
  93. else  
  94.     sed -i ‘s@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@’ /etc/sysconfig/init      
  95.     sed -i ‘s@^start@#start@’ /etc/init/control-alt-delete.conf  
  96. fi  
  97. /sbin/init q  
  98.   
  99. # Set timezone  
  100. rm -rf /etc/localtime  
  101. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  
  102.   
  103. # Update time  
  104. /usr/sbin/ntpdate pool.ntp.org   
  105. echo ‘*/5 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1’ > /var/spool/cron/root;chmod 600 /var/spool/cron/root  
  106. /sbin/service crond restart  
  107.   
  108. # iptables  
  109. cat > /etc/sysconfig/iptables << EOF  
  110. # Firewall configuration written by system-config-securitylevel  
  111. # Manual customization of this file is not recommended.  
  112. *filter  
  113. :INPUT DROP [0:0]  
  114. :FORWARD ACCEPT [0:0]  
  115. :OUTPUT ACCEPT [0:0]  
  116. :syn-flood – [0:0]  
  117. -A INPUT -i lo -j ACCEPT  
  118. -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT  
  119. -A INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT  
  120. -A INPUT -p tcp -m state –state NEW -m tcp –dport 80 -j ACCEPT  
  121. -A INPUT -p icmp -m limit –limit 100/sec –limit-burst 100 -j ACCEPT  
  122. -A INPUT -p icmp -m limit –limit 1/s –limit-burst 10 -j ACCEPT  
  123. -A INPUT -p tcp -m tcp –tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood  
  124. -A INPUT -j REJECT –reject-with icmp-host-prohibited  
  125. -A syn-flood -p tcp -m limit –limit 3/sec –limit-burst 6 -j RETURN  
  126. -A syn-flood -j REJECT –reject-with icmp-port-unreachable  
  127. COMMIT  
  128. EOF  
  129. /sbin/service iptables restart  
  130. source /etc/profile  
  131.   
  132. ###install tmux  
  133. mkdir tmux  
  134. cd tmux  
  135. wget -c http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz   
  136. wget -c http://downloads.sourceforge.net/tmux/tmux-1.8.tar.gz  
  137. tar xzf libevent-2.0.21-stable.tar.gz  
  138. cd libevent-2.0.21-stable  
  139. ./configure  
  140. make && make install  
  141. cd ../  
  142.   
  143. tar xzf tmux-1.8.tar.gz  
  144. cd tmux-1.8  
  145. CFLAGS=”-I/usr/local/include” LDFLAGS=”-L//usr/local/lib” ./configure  
  146. make && make install  
  147. cd ../../  
  148. rm -rf tmux  
  149.   
  150. if [ `getconf WORD_BIT` = ’32’ ] && [ `getconf LONG_BIT` = ’64’ ] ; then  
  151.     ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5  
  152. else  
  153.     ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5  
  154. fi  
  155.   
  156. ###install htop  
  157. mkdir htop  
  158. cd htop  
  159. wget -c http://downloads.sourceforge.net/project/htop/htop/1.0.2/htop-1.0.2.tar.gz   
  160. tar xzf htop-1.0.2.tar.gz  
  161. cd htop-1.0.2  
  162. ./configure  
  163. make && make install  
  164. cd ../../  
  165. rm -rf htop  
Fri Aug 16 17:49:05 CST 2013

【AD】炭云:768元/年/1GB内存/20GB SSD空间/2TB流量/500Mbps-1Gbps端口/独立IPv4/KVM/广州移动

【AD】美国洛杉矶CN2 VPS/香港CN2 VPS/日本CN2 VPS推荐,延迟低、稳定性高、免费备份_搬瓦工vps