init_CentOS.sh 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # Blog: http://blog.linuxeye.com
  4. cd src
  5. . ../functions/download.sh
  6. # add swapfile
  7. Mem=`free -m | awk '/Mem:/{print $2}'`
  8. Swap=`free -m | awk '/Swap:/{print $2}'`
  9. if [ "$Swap" == '0' ] ;then
  10. if [ $Mem -le 1024 ];then
  11. dd if=/dev/zero of=/swapfile count=1024 bs=1M
  12. mkswap /swapfile
  13. swapon /swapfile
  14. chmod 600 /swapfile
  15. elif [ $Mem -gt 1024 -a $Mem -le 2048 ];then
  16. dd if=/dev/zero of=/swapfile count=2048 bs=1M
  17. mkswap /swapfile
  18. swapon /swapfile
  19. chmod 600 /swapfile
  20. fi
  21. cat >> /etc/fstab << EOF
  22. /swapfile swap swap defaults 0 0
  23. EOF
  24. fi
  25. sed -i 's@^exclude@#exclude@' /etc/yum.conf
  26. yum clean all
  27. #if [ -n "`grep ' 7\.' /etc/redhat-release`" ];then
  28. # if [ -n "`grep 'Red Hat' /etc/redhat-release`" ];then
  29. # /bin/mv /etc/yum.repos.d/CentOS-Base.repo{,_bk}
  30. # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  31. # sed -i 's@\$releasever@7@g' /etc/yum.repos.d/CentOS-Base.repo
  32. # sed -i 's@gpgcheck=1@gpgcheck=0@g' /etc/yum.repos.d/CentOS-Base.repo
  33. # fi
  34. #elif [ -n "`grep ' 6\.' /etc/redhat-release`" ];then
  35. # if [ -n "`grep 'Red Hat' /etc/redhat-release`" ];then
  36. # /bin/mv /etc/yum.repos.d/CentOS-Base.repo{,_bk}
  37. # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  38. # sed -i 's@\$releasever@6@g' /etc/yum.repos.d/CentOS-Base.repo
  39. # sed -i 's@gpgcheck=1@gpgcheck=0@g' /etc/yum.repos.d/CentOS-Base.repo
  40. # fi
  41. #elif [ -n "`grep ' 5\.' /etc/redhat-release`" ];then
  42. # if [ -n "`grep 'Red Hat' /etc/redhat-release`" ];then
  43. # /bin/mv /etc/yum.repos.d/CentOS-Base.repo{,_bk}
  44. # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
  45. # sed -i 's@\$releasever@5@g' /etc/yum.repos.d/CentOS-Base.repo
  46. # sed -i 's@gpgcheck=1@gpgcheck=0@g' /etc/yum.repos.d/CentOS-Base.repo
  47. # fi
  48. #fi
  49. yum makecache
  50. if [ -n "`grep ' 7\.' /etc/redhat-release`" ];then
  51. yum -y install iptables-services
  52. systemctl mask firewalld.service
  53. systemctl enable iptables.service
  54. elif [ -n "`grep ' 6\.' /etc/redhat-release`" ];then
  55. yum -y groupremove "FTP Server" "PostgreSQL Database client" "PostgreSQL Database server" "MySQL Database server" "MySQL Database client" "Web Server" "Office Suite and Productivity" "E-mail server" "Ruby Support" "Printing client"
  56. elif [ -n "`grep ' 5\.' /etc/redhat-release`" ];then
  57. yum -y groupremove "FTP Server" "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" "Printing Support" "OpenFabrics Enterprise Distribution"
  58. fi
  59. yum check-update
  60. # check upgrade OS
  61. [ "$upgrade_yn" == 'y' ] && yum -y upgrade
  62. cd /etc/yum.repos.d/
  63. if [ -e "CentOS-Base.repo" -a ! -e "centos.repo" ];then
  64. rename repo repo_bk *.repo
  65. /bin/mv CentOS-Base.repo{_bk,}
  66. elif [ -e "CentOS-Base.repo" -a -e "centos.repo" ];then
  67. rename repo repo_bk *.repo
  68. /bin/mv centos.repo{_bk,}
  69. fi
  70. cd -
  71. # Install needed packages
  72. for Package in gcc gcc-c++ make cmake 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 libaio readline-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel libxslt-devel libevent-devel libtool libtool-ltdl bison gd-devel vim-enhanced pcre-devel zip unzip ntpdate sysstat patch bc expect rsync git
  73. do
  74. yum -y install $Package
  75. done
  76. yum -y update bash openssl glibc
  77. # use gcc-4.4
  78. if [ -n "`gcc --version | head -n1 | grep '4\.1\.'`" ];then
  79. yum -y install gcc44 gcc44-c++ libstdc++44-devel
  80. export CC="gcc44" CXX="g++44"
  81. fi
  82. # check sendmail
  83. #[ "$sendmail_yn" == 'y' ] && yum -y install sendmail && service sendmail restart
  84. # closed Unnecessary services and remove obsolete rpm package
  85. for Service in `chkconfig --list | grep 3:on | awk '{print $1}'`;do chkconfig --level 3 $Service off;done
  86. for Service in sshd network crond iptables messagebus irqbalance syslog rsyslog sendmail;do chkconfig --level 3 $Service on;done
  87. # Close SELINUX
  88. setenforce 0
  89. sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
  90. # initdefault
  91. sed -i 's/^id:.*$/id:3:initdefault:/' /etc/inittab
  92. init q
  93. # PS1
  94. [ -z "`cat ~/.bashrc | grep ^PS1`" ] && echo 'PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\$ "' >> ~/.bashrc
  95. # history size
  96. sed -i 's/^HISTSIZE=.*$/HISTSIZE=100/' /etc/profile
  97. [ -z "`cat ~/.bashrc | grep history-timestamp`" ] && 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'" >> ~/.bashrc
  98. # /etc/security/limits.conf
  99. [ -z "`cat /etc/security/limits.conf | grep 'nproc 65535'`" ] && cat >> /etc/security/limits.conf <<EOF
  100. * soft nproc 65535
  101. * hard nproc 65535
  102. * soft nofile 65535
  103. * hard nofile 65535
  104. EOF
  105. [ -z "`cat /etc/rc.local | grep 'ulimit -SH 65535'`" ] && echo "ulimit -SH 65535" >> /etc/rc.local
  106. # /etc/hosts
  107. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  108. # Set timezone
  109. rm -rf /etc/localtime
  110. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  111. # Set DNS
  112. #cat > /etc/resolv.conf << EOF
  113. #nameserver 114.114.114.114
  114. #nameserver 8.8.8.8
  115. #EOF
  116. # Wrong password five times locked 180s
  117. [ -z "`cat /etc/pam.d/system-auth | grep 'pam_tally2.so'`" ] && sed -i '4a auth required pam_tally2.so deny=5 unlock_time=180' /etc/pam.d/system-auth
  118. # alias vi
  119. [ -z "`cat ~/.bashrc | grep 'alias vi='`" ] && sed -i "s@alias mv=\(.*\)@alias mv=\1\nalias vi=vim@" ~/.bashrc && echo 'syntax on' >> /etc/vimrc
  120. # /etc/sysctl.conf
  121. sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf
  122. [ -z "`cat /etc/sysctl.conf | grep 'fs.file-max'`" ] && cat >> /etc/sysctl.conf << EOF
  123. fs.file-max=65535
  124. net.ipv4.tcp_fin_timeout = 30
  125. net.ipv4.tcp_tw_reuse = 1
  126. net.ipv4.tcp_tw_recycle = 1
  127. net.ipv4.ip_local_port_range = 1024 65000
  128. net.ipv4.tcp_max_syn_backlog = 65536
  129. net.ipv4.tcp_max_tw_buckets = 20000
  130. net.ipv4.route.gc_timeout = 100
  131. net.ipv4.tcp_syn_retries = 1
  132. net.ipv4.tcp_synack_retries = 1
  133. net.core.somaxconn = 65535
  134. net.core.netdev_max_backlog = 262144
  135. net.ipv4.tcp_timestamps = 0
  136. net.ipv4.tcp_max_orphans = 262144
  137. EOF
  138. sysctl -p
  139. if [ -n "`grep ' 5\.' /etc/redhat-release`" ];then
  140. sed -i 's@^[3-6]:2345:respawn@#&@g' /etc/inittab
  141. sed -i 's@^ca::ctrlaltdel@#&@' /etc/inittab
  142. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  143. elif [ -n "`grep ' 6\.' /etc/redhat-release`" ];then
  144. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init
  145. sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
  146. fi
  147. init q
  148. # Update time
  149. ntpdate pool.ntp.org
  150. [ -z "`grep 'pool.ntp.org' /var/spool/cron/root`" ] && { echo "*/20 * * * * `which ntpdate` pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root;chmod 600 /var/spool/cron/root; }
  151. service crond restart
  152. # iptables
  153. if [ -e '/etc/sysconfig/iptables' ] && [ -n "`grep ':INPUT DROP' /etc/sysconfig/iptables`" -a -n "`grep 'NEW -m tcp --dport 22 -j ACCEPT' /etc/sysconfig/iptables`" -a -n "`grep 'NEW -m tcp --dport 80 -j ACCEPT' /etc/sysconfig/iptables`" ];then
  154. IPTABLES_STATUS=yes
  155. else
  156. IPTABLES_STATUS=no
  157. fi
  158. if [ "$IPTABLES_STATUS" == 'no' ];then
  159. cat > /etc/sysconfig/iptables << EOF
  160. # Firewall configuration written by system-config-securitylevel
  161. # Manual customization of this file is not recommended.
  162. *filter
  163. :INPUT DROP [0:0]
  164. :FORWARD ACCEPT [0:0]
  165. :OUTPUT ACCEPT [0:0]
  166. :syn-flood - [0:0]
  167. -A INPUT -i lo -j ACCEPT
  168. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  169. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  170. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  171. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  172. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  173. -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  174. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  175. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  176. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  177. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  178. COMMIT
  179. EOF
  180. fi
  181. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/sysconfig/iptables`
  182. [ -z "$FW_PORT_FLAG" -a "$SSH_PORT" != '22' ] && sed -i "s@dport 22 -j ACCEPT@&\n-A INPUT -p tcp -m state --state NEW -m tcp --dport $SSH_PORT -j ACCEPT@" /etc/sysconfig/iptables
  183. service iptables restart
  184. service sshd restart
  185. # install tmux
  186. if [ ! -e "`which tmux`" ];then
  187. src_url=http://downloads.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz && Download_src
  188. src_url=http://downloads.sourceforge.net/project/tmux/tmux/tmux-2.0/tmux-2.0.tar.gz && Download_src
  189. tar xzf libevent-2.0.22-stable.tar.gz
  190. cd libevent-2.0.22-stable
  191. ./configure
  192. make && make install
  193. cd ..
  194. tar xzf tmux-2.0.tar.gz
  195. cd tmux-2.0
  196. CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
  197. make && make install
  198. cd ..
  199. if [ `getconf WORD_BIT` == 32 ] && [ `getconf LONG_BIT` == 64 ];then
  200. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
  201. else
  202. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
  203. fi
  204. fi
  205. # install htop
  206. if [ ! -e "`which htop`" ];then
  207. src_url=http://hisham.hm/htop/releases/1.0.3/htop-1.0.3.tar.gz && Download_src
  208. tar xzf htop-1.0.3.tar.gz
  209. cd htop-1.0.3
  210. ./configure
  211. make && make install
  212. cd ..
  213. fi
  214. cd ..
  215. . /etc/profile
  216. . ~/.bashrc