init_CentOS.sh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://blog.linuxeye.cn
  4. #
  5. # Notes: OneinStack for CentOS/RadHat 6+ Debian 6+ and Ubuntu 12+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. # closed Unnecessary services and remove obsolete rpm package
  11. [ "${CentOS_ver}" == '7' ] && [ "$(systemctl is-active NetworkManager.service)" == 'active' ] && NM_flag=1
  12. for Service in $(chkconfig --list | grep 3:on | awk '{print $1}' | grep -viE 'nginx|httpd|tomcat|mysqld|php-fpm|pureftpd|redis-server|memcached|supervisord|aegis|agentwatch|NetworkManager|iptables|cloud');do chkconfig --level 3 ${Service} off;done
  13. [ "${NM_flag}" == '1' ] && systemctl enable NetworkManager.service
  14. for Service in sshd network crond messagebus irqbalance syslog rsyslog;do chkconfig --level 3 ${Service} on;done
  15. # Close SELINUX
  16. setenforce 0
  17. sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
  18. # Custom profile
  19. cat > /etc/profile.d/oneinstack.sh << EOF
  20. HISTSIZE=10000
  21. PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\\\$ "
  22. HISTTIMEFORMAT="%F %T \$(whoami) "
  23. alias l='ls -AFhlt'
  24. alias lh='l | head'
  25. alias vi=vim
  26. GREP_OPTIONS="--color=auto"
  27. alias grep='grep --color'
  28. alias egrep='egrep --color'
  29. alias fgrep='fgrep --color'
  30. EOF
  31. [ -z "$(grep ^'PROMPT_COMMAND=' /etc/bashrc)" ] && cat >> /etc/bashrc << EOF
  32. PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });logger "[euid=\$(whoami)]":\$(who am i):[\`pwd\`]"\$msg"; }'
  33. EOF
  34. # /etc/security/limits.conf
  35. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  36. sed -i '/^# End of file/,$d' /etc/security/limits.conf
  37. cat >> /etc/security/limits.conf <<EOF
  38. # End of file
  39. * soft nproc 1000000
  40. * hard nproc 1000000
  41. * soft nofile 1000000
  42. * hard nofile 1000000
  43. EOF
  44. # /etc/hosts
  45. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@127.0.0.1.*localhost@&\n127.0.0.1 $(hostname)@g" /etc/hosts
  46. # Set timezone
  47. rm -rf /etc/localtime
  48. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  49. # Set DNS
  50. #cat > /etc/resolv.conf << EOF
  51. #nameserver 114.114.114.114
  52. #nameserver 8.8.8.8
  53. #EOF
  54. # ip_conntrack table full dropping packets
  55. [ ! -e "/etc/sysconfig/modules/iptables.modules" ] && { echo -e "modprobe nf_conntrack\nmodprobe nf_conntrack_ipv4" > /etc/sysconfig/modules/iptables.modules; chmod +x /etc/sysconfig/modules/iptables.modules; }
  56. modprobe nf_conntrack
  57. modprobe nf_conntrack_ipv4
  58. echo options nf_conntrack hashsize=131072 > /etc/modprobe.d/nf_conntrack.conf
  59. # /etc/sysctl.conf
  60. [ ! -e "/etc/sysctl.conf_bk" ] && /bin/mv /etc/sysctl.conf{,_bk}
  61. cat > /etc/sysctl.conf << EOF
  62. fs.file-max=1000000
  63. net.ipv4.tcp_max_tw_buckets = 6000
  64. net.ipv4.tcp_sack = 1
  65. net.ipv4.tcp_window_scaling = 1
  66. net.ipv4.tcp_rmem = 4096 87380 4194304
  67. net.ipv4.tcp_wmem = 4096 16384 4194304
  68. net.ipv4.tcp_max_syn_backlog = 16384
  69. net.core.netdev_max_backlog = 32768
  70. net.core.somaxconn = 32768
  71. net.core.wmem_default = 8388608
  72. net.core.rmem_default = 8388608
  73. net.core.rmem_max = 16777216
  74. net.core.wmem_max = 16777216
  75. net.ipv4.tcp_timestamps = 1
  76. net.ipv4.tcp_fin_timeout = 20
  77. net.ipv4.tcp_synack_retries = 2
  78. net.ipv4.tcp_syn_retries = 2
  79. net.ipv4.tcp_syncookies = 1
  80. #net.ipv4.tcp_tw_len = 1
  81. net.ipv4.tcp_tw_reuse = 1
  82. net.ipv4.tcp_mem = 94500000 915000000 927000000
  83. net.ipv4.tcp_max_orphans = 3276800
  84. net.ipv4.ip_local_port_range = 1024 65000
  85. net.nf_conntrack_max = 6553500
  86. net.netfilter.nf_conntrack_max = 6553500
  87. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
  88. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
  89. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
  90. net.netfilter.nf_conntrack_tcp_timeout_established = 3600
  91. EOF
  92. sysctl -p
  93. if [ "${CentOS_ver}" == '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_ver}" == '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. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  101. elif [ "${CentOS_ver}" == '7' ]; then
  102. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/locale.conf
  103. fi
  104. # Update time
  105. ntpdate pool.ntp.org
  106. [ ! -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; }
  107. # iptables
  108. if [ "${iptables_yn}" == 'y' ]; then
  109. 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
  110. IPTABLES_STATUS=yes
  111. else
  112. IPTABLES_STATUS=no
  113. fi
  114. if [ "$IPTABLES_STATUS" == "no" ]; then
  115. [ -e "/etc/sysconfig/iptables" ] && /bin/mv /etc/sysconfig/iptables{,_bk}
  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 1/sec --limit-burst 10 -j ACCEPT
  130. -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -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. chkconfig --level 3 iptables on
  141. service iptables restart
  142. fi
  143. service rsyslog restart
  144. service sshd restart
  145. . /etc/profile