init_CentOS.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. # closed Unnecessary services and remove obsolete rpm package
  11. [ "${CentOS_RHEL_version}" == '7' ] && [ "$(systemctl is-active NetworkManager.service)" == 'active' ] && NM_flag=1
  12. 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
  13. [ "${NM_flag}" == '1' ] && systemctl enable NetworkManager.service
  14. for Service in sshd network crond iptables 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 65535
  40. * hard nproc 65535
  41. * soft nofile 65535
  42. * hard nofile 65535
  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 modprobe ip_conntrack > /etc/sysconfig/modules/iptables.modules; chmod +x /etc/sysconfig/modules/iptables.modules; }
  56. modprobe ip_conntrack
  57. echo options nf_conntrack hashsize=131072 > /etc/modprobe.d/nf_conntrack.conf
  58. # /etc/sysctl.conf
  59. [ ! -e "/etc/sysctl.conf_bk" ] && /bin/mv /etc/sysctl.conf{,_bk}
  60. cat > /etc/sysctl.conf << EOF
  61. fs.file-max=65535
  62. net.ipv4.tcp_max_tw_buckets = 60000
  63. net.ipv4.tcp_sack = 1
  64. net.ipv4.tcp_window_scaling = 1
  65. net.ipv4.tcp_rmem = 4096 87380 4194304
  66. net.ipv4.tcp_wmem = 4096 16384 4194304
  67. net.ipv4.tcp_max_syn_backlog = 65536
  68. net.core.netdev_max_backlog = 32768
  69. net.core.somaxconn = 32768
  70. net.core.wmem_default = 8388608
  71. net.core.rmem_default = 8388608
  72. net.core.rmem_max = 16777216
  73. net.core.wmem_max = 16777216
  74. net.ipv4.tcp_timestamps = 0
  75. net.ipv4.tcp_synack_retries = 2
  76. net.ipv4.tcp_syn_retries = 2
  77. net.ipv4.tcp_tw_recycle = 1
  78. #net.ipv4.tcp_tw_len = 1
  79. net.ipv4.tcp_tw_reuse = 1
  80. net.ipv4.tcp_mem = 94500000 915000000 927000000
  81. net.ipv4.tcp_max_orphans = 3276800
  82. net.ipv4.tcp_tw_recycle = 1
  83. net.ipv4.ip_local_port_range = 1024 65000
  84. net.nf_conntrack_max = 6553500
  85. net.netfilter.nf_conntrack_max = 6553500
  86. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
  87. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
  88. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
  89. net.netfilter.nf_conntrack_tcp_timeout_established = 3600
  90. EOF
  91. sysctl -p
  92. if [ "${CentOS_RHEL_version}" == '5' ]; then
  93. sed -i 's@^[3-6]:2345:respawn@#&@g' /etc/inittab
  94. sed -i 's@^ca::ctrlaltdel@#&@' /etc/inittab
  95. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  96. elif [ "${CentOS_RHEL_version}" == '6' ]; then
  97. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init
  98. sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
  99. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  100. elif [ "${CentOS_RHEL_version}" == '7' ]; then
  101. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/locale.conf
  102. fi
  103. # Update time
  104. ntpdate pool.ntp.org
  105. [ ! -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; }
  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. [ -e "/etc/sysconfig/iptables" ] && /bin/mv /etc/sysconfig/iptables{,_bk}
  114. cat > /etc/sysconfig/iptables << EOF
  115. # Firewall configuration written by system-config-securitylevel
  116. # Manual customization of this file is not recommended.
  117. *filter
  118. :INPUT DROP [0:0]
  119. :FORWARD ACCEPT [0:0]
  120. :OUTPUT ACCEPT [0:0]
  121. :syn-flood - [0:0]
  122. -A INPUT -i lo -j ACCEPT
  123. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  124. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  125. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  126. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  127. -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
  128. -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  129. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  130. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  131. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  132. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  133. COMMIT
  134. EOF
  135. fi
  136. FW_PORT_FLAG=$(grep -ow "dport ${SSH_PORT}" /etc/sysconfig/iptables)
  137. [ -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
  138. service iptables restart
  139. service sshd restart
  140. . /etc/profile