init_CentOS.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 lsof lrzsz
  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}' | grep -vE 'nginx|httpd|tomcat|mysqld|php-fpm|pureftpd|redis-server|memcached|supervisord|aegis'`;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. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  54. sed -i '/^# End of file/,$d' /etc/security/limits.conf
  55. cat >> /etc/security/limits.conf <<EOF
  56. # End of file
  57. * soft nproc 65535
  58. * hard nproc 65535
  59. * soft nofile 65535
  60. * hard nofile 65535
  61. EOF
  62. [ -z "`grep 'ulimit -SH 65535' /etc/rc.local`" ] && echo "ulimit -SH 65535" >> /etc/rc.local
  63. # /etc/hosts
  64. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  65. # Set timezone
  66. rm -rf /etc/localtime
  67. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  68. # Set DNS
  69. #cat > /etc/resolv.conf << EOF
  70. #nameserver 114.114.114.114
  71. #nameserver 8.8.8.8
  72. #EOF
  73. # Wrong password five times locked 180s
  74. [ -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
  75. # alias vi
  76. [ -z "`cat ~/.bashrc | grep 'alias vi='`" ] && sed -i "s@alias mv=\(.*\)@alias mv=\1\nalias vi=vim@" ~/.bashrc && echo 'syntax on' >> /etc/vimrc
  77. # /etc/sysctl.conf
  78. sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf
  79. [ -z "`cat /etc/sysctl.conf | grep 'fs.file-max'`" ] && cat >> /etc/sysctl.conf << EOF
  80. fs.file-max=65535
  81. net.ipv4.tcp_fin_timeout = 30
  82. net.ipv4.tcp_tw_reuse = 1
  83. net.ipv4.tcp_tw_recycle = 1
  84. net.ipv4.ip_local_port_range = 1024 65000
  85. net.ipv4.tcp_max_syn_backlog = 65536
  86. net.ipv4.tcp_max_tw_buckets = 20000
  87. net.ipv4.route.gc_timeout = 100
  88. net.ipv4.tcp_syn_retries = 1
  89. net.ipv4.tcp_synack_retries = 1
  90. net.core.somaxconn = 65535
  91. net.core.netdev_max_backlog = 262144
  92. net.ipv4.tcp_timestamps = 0
  93. net.ipv4.tcp_max_orphans = 262144
  94. EOF
  95. sysctl -p
  96. if [ "$CentOS_RHEL_version" == '5' ];then
  97. sed -i 's@^[3-6]:2345:respawn@#&@g' /etc/inittab
  98. sed -i 's@^ca::ctrlaltdel@#&@' /etc/inittab
  99. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  100. elif [ "$CentOS_RHEL_version" == '6' ];then
  101. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init
  102. sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
  103. fi
  104. init q
  105. # Update time
  106. ntpdate pool.ntp.org
  107. [ -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; }
  108. service crond restart
  109. # iptables
  110. 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
  111. IPTABLES_STATUS=yes
  112. else
  113. IPTABLES_STATUS=no
  114. fi
  115. if [ "$IPTABLES_STATUS" == 'no' ];then
  116. cat > /etc/sysconfig/iptables << EOF
  117. # Firewall configuration written by system-config-securitylevel
  118. # Manual customization of this file is not recommended.
  119. *filter
  120. :INPUT DROP [0:0]
  121. :FORWARD ACCEPT [0:0]
  122. :OUTPUT ACCEPT [0:0]
  123. :syn-flood - [0:0]
  124. -A INPUT -i lo -j ACCEPT
  125. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  126. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  127. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  128. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  129. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  130. -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  131. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  132. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  133. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  134. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  135. COMMIT
  136. EOF
  137. fi
  138. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/sysconfig/iptables`
  139. [ -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
  140. service iptables restart
  141. service sshd restart
  142. # install tmux
  143. if [ ! -e "`which tmux`" ];then
  144. cd src
  145. src_url=http://downloads.sourceforge.net/project/levent/libevent/libevent-2.0/libevent-2.0.22-stable.tar.gz && Download_src
  146. src_url=http://mirrors.linuxeye.com/oneinstack/src/tmux-2.1.tar.gz && Download_src
  147. tar xzf libevent-2.0.22-stable.tar.gz
  148. cd libevent-2.0.22-stable
  149. ./configure
  150. make && make install
  151. cd ..
  152. tar xzf tmux-2.1.tar.gz
  153. cd tmux-2.1
  154. CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
  155. make && make install
  156. cd ../../
  157. if [ `getconf WORD_BIT` == 32 ] && [ `getconf LONG_BIT` == 64 ];then
  158. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
  159. else
  160. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
  161. fi
  162. fi
  163. # install htop
  164. if [ ! -e "`which htop`" ];then
  165. cd src
  166. src_url=http://hisham.hm/htop/releases/1.0.3/htop-1.0.3.tar.gz && Download_src
  167. tar xzf htop-1.0.3.tar.gz
  168. cd htop-1.0.3
  169. ./configure
  170. make && make install
  171. cd ../../
  172. fi
  173. . /etc/profile
  174. . ~/.bashrc