1
0

init_CentOS.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. # https://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 groupremove "Basic Web Server" "MySQL Database server" "MySQL Database client" "File and Print Server"
  15. yum -y install iptables-services
  16. systemctl mask firewalld.service
  17. systemctl enable iptables.service
  18. elif [ "$CentOS_RHEL_version" == '6' ];then
  19. 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"
  20. elif [ "$CentOS_RHEL_version" == '5' ];then
  21. 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"
  22. fi
  23. yum check-update
  24. # Install needed packages
  25. 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 libicu-devel libevent-devel libtool libtool-ltdl bison gd-devel vim-enhanced pcre-devel zip unzip ntpdate sysstat patch bc expect rsync git lsof lrzsz
  26. do
  27. yum -y install $Package
  28. done
  29. yum -y update bash openssl glibc
  30. # use gcc-4.4
  31. if [ -n "`gcc --version | head -n1 | grep '4\.1\.'`" ];then
  32. yum -y install gcc44 gcc44-c++ libstdc++44-devel
  33. export CC="gcc44" CXX="g++44"
  34. fi
  35. # closed Unnecessary services and remove obsolete rpm package
  36. [ "$CentOS_RHEL_version" == '7' ] && [ "`systemctl is-active NetworkManager.service`" == 'active' ] && NM_flag=1
  37. 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|NetworkManager'`;do chkconfig --level 3 $Service off;done
  38. [ "$NM_flag" == '1' ] && systemctl enable NetworkManager.service
  39. for Service in sshd network crond iptables messagebus irqbalance syslog rsyslog;do chkconfig --level 3 $Service on;done
  40. # Close SELINUX
  41. setenforce 0
  42. sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
  43. # Custom profile
  44. cat > /etc/profile.d/oneinstack.sh << EOF
  45. HISTSIZE=10000
  46. PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\\\$ "
  47. HISTTIMEFORMAT="%F %T \`whoami\` "
  48. alias l='ls -AFhlt'
  49. alias lh='l | head'
  50. alias vi=vim
  51. GREP_OPTIONS="--color=auto"
  52. alias grep='grep --color'
  53. alias egrep='egrep --color'
  54. alias fgrep='fgrep --color'
  55. EOF
  56. [ -z "`grep ^'PROMPT_COMMAND=' /etc/bashrc`" ] && cat >> /etc/bashrc << EOF
  57. PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });logger "[euid=\$(whoami)]":\$(who am i):[\`pwd\`]"\$msg"; }'
  58. EOF
  59. # /etc/security/limits.conf
  60. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  61. sed -i '/^# End of file/,$d' /etc/security/limits.conf
  62. cat >> /etc/security/limits.conf <<EOF
  63. # End of file
  64. * soft nproc 65535
  65. * hard nproc 65535
  66. * soft nofile 65535
  67. * hard nofile 65535
  68. EOF
  69. # /etc/hosts
  70. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  71. # Set timezone
  72. rm -rf /etc/localtime
  73. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  74. # Set DNS
  75. #cat > /etc/resolv.conf << EOF
  76. #nameserver 114.114.114.114
  77. #nameserver 8.8.8.8
  78. #EOF
  79. # ip_conntrack table full dropping packets
  80. [ ! -e "/etc/sysconfig/modules/iptables.modules" ] && { echo modprobe ip_conntrack > /etc/sysconfig/modules/iptables.modules; chmod +x /etc/sysconfig/modules/iptables.modules; }
  81. modprobe ip_conntrack
  82. echo options nf_conntrack hashsize=131072 > /etc/modprobe.d/nf_conntrack.conf
  83. # /etc/sysctl.conf
  84. [ ! -e "/etc/sysctl.conf_bk" ] && /bin/mv /etc/sysctl.conf{,_bk}
  85. cat > /etc/sysctl.conf << EOF
  86. fs.file-max=65535
  87. net.ipv4.tcp_max_tw_buckets = 60000
  88. net.ipv4.tcp_sack = 1
  89. net.ipv4.tcp_window_scaling = 1
  90. net.ipv4.tcp_rmem = 4096 87380 4194304
  91. net.ipv4.tcp_wmem = 4096 16384 4194304
  92. net.ipv4.tcp_max_syn_backlog = 65536
  93. net.core.netdev_max_backlog = 32768
  94. net.core.somaxconn = 32768
  95. net.core.wmem_default = 8388608
  96. net.core.rmem_default = 8388608
  97. net.core.rmem_max = 16777216
  98. net.core.wmem_max = 16777216
  99. net.ipv4.tcp_timestamps = 0
  100. net.ipv4.tcp_synack_retries = 2
  101. net.ipv4.tcp_syn_retries = 2
  102. net.ipv4.tcp_tw_recycle = 1
  103. #net.ipv4.tcp_tw_len = 1
  104. net.ipv4.tcp_tw_reuse = 1
  105. net.ipv4.tcp_mem = 94500000 915000000 927000000
  106. net.ipv4.tcp_max_orphans = 3276800
  107. net.ipv4.tcp_tw_recycle = 1
  108. net.ipv4.ip_local_port_range = 1024 65000
  109. net.nf_conntrack_max = 6553500
  110. net.netfilter.nf_conntrack_max = 6553500
  111. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
  112. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
  113. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
  114. net.netfilter.nf_conntrack_tcp_timeout_established = 3600
  115. EOF
  116. sysctl -p
  117. if [ "$CentOS_RHEL_version" == '5' ];then
  118. sed -i 's@^[3-6]:2345:respawn@#&@g' /etc/inittab
  119. sed -i 's@^ca::ctrlaltdel@#&@' /etc/inittab
  120. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  121. elif [ "$CentOS_RHEL_version" == '6' ];then
  122. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init
  123. sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
  124. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  125. elif [ "$CentOS_RHEL_version" == '7' ];then
  126. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/locale.conf
  127. fi
  128. # Update time
  129. ntpdate pool.ntp.org
  130. [ ! -e "/var/spool/cron/root" -o -z "`grep 'ntpdate' /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; }
  131. # iptables
  132. 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
  133. IPTABLES_STATUS=yes
  134. else
  135. IPTABLES_STATUS=no
  136. fi
  137. if [ "$IPTABLES_STATUS" == 'no' ];then
  138. [ -e '/etc/sysconfig/iptables' ] && /bin/mv /etc/sysconfig/iptables{,_bk}
  139. cat > /etc/sysconfig/iptables << EOF
  140. # Firewall configuration written by system-config-securitylevel
  141. # Manual customization of this file is not recommended.
  142. *filter
  143. :INPUT DROP [0:0]
  144. :FORWARD ACCEPT [0:0]
  145. :OUTPUT ACCEPT [0:0]
  146. :syn-flood - [0:0]
  147. -A INPUT -i lo -j ACCEPT
  148. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  149. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  150. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  151. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  152. -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
  153. -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  154. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  155. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  156. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  157. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  158. COMMIT
  159. EOF
  160. fi
  161. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/sysconfig/iptables`
  162. [ -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
  163. service iptables restart
  164. service sshd restart
  165. # install tmux
  166. if [ ! -e "`which tmux`" ];then
  167. cd src
  168. tar xzf libevent-${libevent_version}.tar.gz
  169. cd libevent-${libevent_version}
  170. ./configure
  171. make -j ${THREAD} && make install
  172. cd ..
  173. tar xzf tmux-${tmux_version}.tar.gz
  174. cd tmux-${tmux_version}
  175. CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
  176. make -j ${THREAD} && make install
  177. cd ../../
  178. if [ "$OS_BIT" == '64' ];then
  179. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
  180. else
  181. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
  182. fi
  183. fi
  184. # install htop
  185. if [ ! -e "`which htop`" ];then
  186. cd src
  187. tar xzf htop-${htop_version}.tar.gz
  188. cd htop-${htop_version}
  189. ./configure
  190. make -j ${THREAD} && make install
  191. cd ../../
  192. fi
  193. . /etc/profile