init_CentOS.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. sed -i 's@^exclude@#exclude@' /etc/yum.conf
  11. yum clean all
  12. yum makecache
  13. if [ "$CentOS_RHEL_version" == '7' ];then
  14. yum -y install iptables-services
  15. systemctl mask firewalld.service
  16. systemctl enable iptables.service
  17. elif [ "$CentOS_RHEL_version" == '6' ];then
  18. 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"
  19. elif [ "$CentOS_RHEL_version" == '5' ];then
  20. 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"
  21. fi
  22. yum check-update
  23. # check upgrade OS
  24. [ "$upgrade_yn" == 'y' ] && yum -y upgrade
  25. # Install needed packages
  26. for Package in deltarpm 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
  27. do
  28. yum -y install $Package
  29. done
  30. yum -y update bash openssl glibc
  31. # use gcc-4.4
  32. if [ -n "`gcc --version | head -n1 | grep '4\.1\.'`" ];then
  33. yum -y install gcc44 gcc44-c++ libstdc++44-devel
  34. export CC="gcc44" CXX="g++44"
  35. fi
  36. # check sendmail
  37. #[ "$sendmail_yn" == 'y' ] && yum -y install sendmail && service sendmail restart
  38. # closed Unnecessary services and remove obsolete rpm package
  39. for Service in `chkconfig --list | grep 3:on | awk '{print $1}'`;do chkconfig --level 3 $Service off;done
  40. for Service in sshd network crond iptables messagebus irqbalance syslog rsyslog sendmail;do chkconfig --level 3 $Service on;done
  41. # Close SELINUX
  42. setenforce 0
  43. sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
  44. # initdefault
  45. sed -i 's/^id:.*$/id:3:initdefault:/' /etc/inittab
  46. init q
  47. # PS1
  48. [ -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
  49. # history size
  50. sed -i 's/^HISTSIZE=.*$/HISTSIZE=100/' /etc/profile
  51. [ -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
  52. # /etc/security/limits.conf
  53. [ -z "`cat /etc/security/limits.conf | grep 'nproc 65535'`" ] && cat >> /etc/security/limits.conf <<EOF
  54. * soft nproc 65535
  55. * hard nproc 65535
  56. * soft nofile 65535
  57. * hard nofile 65535
  58. EOF
  59. [ -z "`cat /etc/rc.local | grep 'ulimit -SH 65535'`" ] && echo "ulimit -SH 65535" >> /etc/rc.local
  60. # /etc/hosts
  61. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  62. # Set timezone
  63. rm -rf /etc/localtime
  64. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  65. # Set DNS
  66. #cat > /etc/resolv.conf << EOF
  67. #nameserver 114.114.114.114
  68. #nameserver 8.8.8.8
  69. #EOF
  70. # Wrong password five times locked 180s
  71. [ -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
  72. # alias vi
  73. [ -z "`cat ~/.bashrc | grep 'alias vi='`" ] && sed -i "s@alias mv=\(.*\)@alias mv=\1\nalias vi=vim@" ~/.bashrc && echo 'syntax on' >> /etc/vimrc
  74. # /etc/sysctl.conf
  75. sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf
  76. [ -z "`cat /etc/sysctl.conf | grep 'fs.file-max'`" ] && cat >> /etc/sysctl.conf << EOF
  77. fs.file-max=65535
  78. net.ipv4.tcp_fin_timeout = 30
  79. net.ipv4.tcp_tw_reuse = 1
  80. net.ipv4.tcp_tw_recycle = 1
  81. net.ipv4.ip_local_port_range = 1024 65000
  82. net.ipv4.tcp_max_syn_backlog = 65536
  83. net.ipv4.tcp_max_tw_buckets = 20000
  84. net.ipv4.route.gc_timeout = 100
  85. net.ipv4.tcp_syn_retries = 1
  86. net.ipv4.tcp_synack_retries = 1
  87. net.core.somaxconn = 65535
  88. net.core.netdev_max_backlog = 262144
  89. net.ipv4.tcp_timestamps = 0
  90. net.ipv4.tcp_max_orphans = 262144
  91. EOF
  92. sysctl -p
  93. if [ "$CentOS_RHEL_version" == '5' ];then
  94. sed -i 's@^[3-6]:2345:respawn@#&@g' /etc/inittab
  95. sed -i 's@^ca::ctrlaltdel@#&@' /etc/inittab
  96. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  97. elif [ "$CentOS_RHEL_version" == '6' ];then
  98. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init
  99. sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
  100. fi
  101. init q
  102. # Update time
  103. ntpdate pool.ntp.org
  104. [ -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; }
  105. service crond restart
  106. # iptables
  107. 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
  108. IPTABLES_STATUS=yes
  109. else
  110. IPTABLES_STATUS=no
  111. fi
  112. if [ "$IPTABLES_STATUS" == 'no' ];then
  113. cat > /etc/sysconfig/iptables << EOF
  114. # Firewall configuration written by system-config-securitylevel
  115. # Manual customization of this file is not recommended.
  116. *filter
  117. :INPUT DROP [0:0]
  118. :FORWARD ACCEPT [0:0]
  119. :OUTPUT ACCEPT [0:0]
  120. :syn-flood - [0:0]
  121. -A INPUT -i lo -j ACCEPT
  122. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  123. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  124. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  125. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  126. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  127. -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  128. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  129. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  130. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  131. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  132. COMMIT
  133. EOF
  134. fi
  135. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/sysconfig/iptables`
  136. [ -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
  137. service iptables restart
  138. service sshd restart
  139. # install tmux
  140. if [ ! -e "`which tmux`" ];then
  141. cd src
  142. src_url=http://downloads.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz && Download_src
  143. src_url=http://downloads.sourceforge.net/project/tmux/tmux/tmux-2.0/tmux-2.0.tar.gz && Download_src
  144. tar xzf libevent-2.0.22-stable.tar.gz
  145. cd libevent-2.0.22-stable
  146. ./configure
  147. make && make install
  148. cd ..
  149. tar xzf tmux-2.0.tar.gz
  150. cd tmux-2.0
  151. CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
  152. make && make install
  153. cd ../../
  154. if [ `getconf WORD_BIT` == 32 ] && [ `getconf LONG_BIT` == 64 ];then
  155. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
  156. else
  157. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
  158. fi
  159. fi
  160. # install htop
  161. if [ ! -e "`which htop`" ];then
  162. cd src
  163. src_url=http://hisham.hm/htop/releases/1.0.3/htop-1.0.3.tar.gz && Download_src
  164. tar xzf htop-1.0.3.tar.gz
  165. cd htop-1.0.3
  166. ./configure
  167. make && make install
  168. cd ../../
  169. fi
  170. . /etc/profile
  171. . ~/.bashrc