init_CentOS.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. # check sendmail
  36. #[ "$sendmail_yn" == 'y' ] && yum -y install sendmail && service sendmail restart
  37. # closed Unnecessary services and remove obsolete rpm package
  38. [ "$CentOS_RHEL_version" == '7' ] && [ "`systemctl is-active NetworkManager.service`" == 'active' ] && NM_flag=1
  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|NetworkManager'`;do chkconfig --level 3 $Service off;done
  40. [ "$NM_flag" == '1' ] && systemctl enable NetworkManager.service
  41. for Service in sshd network crond iptables messagebus irqbalance syslog rsyslog sendmail;do chkconfig --level 3 $Service on;done
  42. # Close SELINUX
  43. setenforce 0
  44. sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
  45. # Custom profile
  46. cat > /etc/profile.d/oneinstack.sh << EOF
  47. HISTSIZE=10000
  48. PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\\\$ "
  49. HISTTIMEFORMAT="%F %T \`whoami\` "
  50. PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });logger "[euid=\$(whoami)]":\$(who am i):[\`pwd\`]"\$msg"; }'
  51. alias l='ls -AFhlt'
  52. alias lh='l | head'
  53. alias vi=vim
  54. GREP_OPTIONS="--color=auto"
  55. alias grep='grep --color'
  56. alias egrep='egrep --color'
  57. alias fgrep='fgrep --color'
  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. # /etc/sysctl.conf
  80. sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf
  81. [ -z "`grep 'fs.file-max' /etc/sysctl.conf`" ] && cat >> /etc/sysctl.conf << EOF
  82. fs.file-max=65535
  83. fs.inotify.max_user_instances = 8192
  84. net.ipv4.tcp_fin_timeout = 30
  85. net.ipv4.tcp_tw_reuse = 1
  86. net.ipv4.tcp_tw_recycle = 1
  87. net.ipv4.ip_local_port_range = 1024 65000
  88. net.ipv4.tcp_max_syn_backlog = 65536
  89. net.ipv4.tcp_max_tw_buckets = 6000
  90. net.ipv4.route.gc_timeout = 100
  91. net.ipv4.tcp_syn_retries = 1
  92. net.ipv4.tcp_synack_retries = 1
  93. net.core.somaxconn = 65535
  94. net.core.netdev_max_backlog = 262144
  95. net.ipv4.tcp_timestamps = 0
  96. net.ipv4.tcp_max_orphans = 262144
  97. EOF
  98. sysctl -p
  99. if [ "$CentOS_RHEL_version" == '5' ];then
  100. sed -i 's@^[3-6]:2345:respawn@#&@g' /etc/inittab
  101. sed -i 's@^ca::ctrlaltdel@#&@' /etc/inittab
  102. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  103. elif [ "$CentOS_RHEL_version" == '6' ];then
  104. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init
  105. sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
  106. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  107. [ -z "`grep net.netfilter.nf_conntrack_max /etc/sysctl.conf`" ] && cat >> /etc/sysctl.conf << EOF
  108. net.nf_conntrack_max = 6553500
  109. net.netfilter.nf_conntrack_max = 6553500
  110. net.netfilter.nf_conntrack_tcp_timeout_established = 180
  111. EOF
  112. elif [ "$CentOS_RHEL_version" == '7' ];then
  113. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/locale.conf
  114. [ -z "`grep net.netfilter.nf_conntrack_max /etc/sysctl.conf`" ] && cat >> /etc/sysctl.conf << EOF
  115. net.nf_conntrack_max = 6553500
  116. net.netfilter.nf_conntrack_max = 6553500
  117. net.netfilter.nf_conntrack_tcp_timeout_established = 180
  118. EOF
  119. fi
  120. init q
  121. # Update time
  122. ntpdate pool.ntp.org
  123. [ ! -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; }
  124. # iptables
  125. 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
  126. IPTABLES_STATUS=yes
  127. else
  128. IPTABLES_STATUS=no
  129. fi
  130. if [ "$IPTABLES_STATUS" == 'no' ];then
  131. [ -e '/etc/sysconfig/iptables' ] && /bin/mv /etc/sysconfig/iptables{,_bk}
  132. cat > /etc/sysconfig/iptables << EOF
  133. # Firewall configuration written by system-config-securitylevel
  134. # Manual customization of this file is not recommended.
  135. *filter
  136. :INPUT DROP [0:0]
  137. :FORWARD ACCEPT [0:0]
  138. :OUTPUT ACCEPT [0:0]
  139. :syn-flood - [0:0]
  140. -A INPUT -i lo -j ACCEPT
  141. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  142. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  143. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  144. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  145. -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
  146. -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  147. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  148. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  149. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  150. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  151. COMMIT
  152. EOF
  153. fi
  154. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/sysconfig/iptables`
  155. [ -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
  156. service iptables restart
  157. service sshd restart
  158. # install tmux
  159. if [ ! -e "`which tmux`" ];then
  160. cd src
  161. tar xzf libevent-${libevent_version}.tar.gz
  162. cd libevent-${libevent_version}
  163. ./configure
  164. make -j ${THREAD} && make install
  165. cd ..
  166. tar xzf tmux-${tmux_version}.tar.gz
  167. cd tmux-${tmux_version}
  168. CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
  169. make -j ${THREAD} && make install
  170. cd ../../
  171. if [ "$OS_BIT" == '64' ];then
  172. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
  173. else
  174. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
  175. fi
  176. fi
  177. # install htop
  178. if [ ! -e "`which htop`" ];then
  179. cd src
  180. tar xzf htop-${htop_version}.tar.gz
  181. cd htop-${htop_version}
  182. ./configure
  183. make -j ${THREAD} && make install
  184. cd ../../
  185. fi
  186. . /etc/profile