init_RHEL.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # BLOG: https://linuxeye.com
  4. #
  5. # Notes: OneinStack for CentOS/RedHat 7+ Debian 9+ and Ubuntu 16+
  6. #
  7. # Project home page:
  8. # https://oneinstack.com
  9. # https://github.com/oneinstack/oneinstack
  10. # Close SELINUX
  11. setenforce 0
  12. sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
  13. # Custom profile
  14. cat > /etc/profile.d/oneinstack.sh << EOF
  15. HISTSIZE=10000
  16. PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\\\$ "
  17. HISTTIMEFORMAT="%F %T \$(whoami) "
  18. alias l='ls -AFhlt'
  19. alias lh='l | head'
  20. alias vi=vim
  21. GREP_OPTIONS="--color=auto"
  22. alias grep='grep --color'
  23. alias egrep='egrep --color'
  24. alias fgrep='fgrep --color'
  25. EOF
  26. [[ "${Platform}" =~ ^EulerOS$|^openEuler$ ]] && sed -i '/HISTTIMEFORMAT=/d' /etc/profile.d/oneinstack.sh
  27. [[ ! "${Platform}" =~ ^EulerOS$|^openEuler$ ]] && [ -z "$(grep ^'PROMPT_COMMAND=' /etc/bashrc)" ] && cat >> /etc/bashrc << EOF
  28. PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });logger "[euid=\$(whoami)]":\$(who am i):[\`pwd\`]"\$msg"; }'
  29. EOF
  30. # /etc/security/limits.conf
  31. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  32. sed -i '/^# End of file/,$d' /etc/security/limits.conf
  33. cat >> /etc/security/limits.conf <<EOF
  34. # End of file
  35. * soft nproc 1000000
  36. * hard nproc 1000000
  37. * soft nofile 1000000
  38. * hard nofile 1000000
  39. EOF
  40. # /etc/hosts
  41. [ "$(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
  42. # Set timezone
  43. rm -rf /etc/localtime
  44. ln -s /usr/share/zoneinfo/${timezone} /etc/localtime
  45. # Set DNS
  46. #cat > /etc/resolv.conf << EOF
  47. #nameserver 114.114.114.114
  48. #nameserver 8.8.8.8
  49. #EOF
  50. # ip_conntrack table full dropping packets
  51. [ ! -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; }
  52. modprobe nf_conntrack
  53. modprobe nf_conntrack_ipv4
  54. echo options nf_conntrack hashsize=131072 > /etc/modprobe.d/nf_conntrack.conf
  55. # /etc/sysctl.conf
  56. [ ! -e "/etc/sysctl.conf_bk" ] && /bin/mv /etc/sysctl.conf{,_bk}
  57. cat > /etc/sysctl.conf << EOF
  58. fs.file-max=1000000
  59. net.ipv4.tcp_max_tw_buckets = 6000
  60. net.ipv4.tcp_sack = 1
  61. net.ipv4.tcp_window_scaling = 1
  62. net.ipv4.tcp_rmem = 4096 87380 4194304
  63. net.ipv4.tcp_wmem = 4096 16384 4194304
  64. net.ipv4.tcp_max_syn_backlog = 16384
  65. net.core.netdev_max_backlog = 32768
  66. net.core.somaxconn = 32768
  67. net.core.wmem_default = 8388608
  68. net.core.rmem_default = 8388608
  69. net.core.rmem_max = 16777216
  70. net.core.wmem_max = 16777216
  71. net.ipv4.tcp_timestamps = 1
  72. net.ipv4.tcp_fin_timeout = 20
  73. net.ipv4.tcp_synack_retries = 2
  74. net.ipv4.tcp_syn_retries = 2
  75. net.ipv4.tcp_syncookies = 1
  76. #net.ipv4.tcp_tw_len = 1
  77. net.ipv4.tcp_tw_reuse = 1
  78. net.ipv4.tcp_mem = 94500000 915000000 927000000
  79. net.ipv4.tcp_max_orphans = 3276800
  80. net.ipv4.ip_local_port_range = 1024 65000
  81. net.nf_conntrack_max = 6553500
  82. net.netfilter.nf_conntrack_max = 6553500
  83. net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60
  84. net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120
  85. net.netfilter.nf_conntrack_tcp_timeout_time_wait = 120
  86. net.netfilter.nf_conntrack_tcp_timeout_established = 3600
  87. EOF
  88. sysctl -p
  89. if [ ${RHEL_ver} -ge 7 >/dev/null 2>&1 ]; then
  90. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/locale.conf
  91. fi
  92. # Update time
  93. if [ -e "$(which ntpdate)" ]; then
  94. ntpdate -u pool.ntp.org
  95. [ ! -e "/var/spool/cron/root" -o -z "$(grep 'ntpdate' /var/spool/cron/root)" ] && { echo "*/20 * * * * $(which ntpdate) -u pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root;chmod 600 /var/spool/cron/root; }
  96. fi
  97. # iptables
  98. if [ "${iptables_flag}" == 'y' ]; then
  99. 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
  100. IPTABLES_STATUS=yes
  101. else
  102. IPTABLES_STATUS=no
  103. fi
  104. if [ "$IPTABLES_STATUS" == "no" ]; then
  105. [ -e "/etc/sysconfig/iptables" ] && /bin/mv /etc/sysconfig/iptables{,_bk}
  106. cat > /etc/sysconfig/iptables << EOF
  107. # Firewall configuration written by system-config-securitylevel
  108. # Manual customization of this file is not recommended.
  109. *filter
  110. :INPUT DROP [0:0]
  111. :FORWARD ACCEPT [0:0]
  112. :OUTPUT ACCEPT [0:0]
  113. :syn-flood - [0:0]
  114. -A INPUT -i lo -j ACCEPT
  115. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  116. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  117. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  118. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  119. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  120. COMMIT
  121. EOF
  122. fi
  123. FW_PORT_FLAG=$(grep -ow "dport ${ssh_port}" /etc/sysconfig/iptables)
  124. [ -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
  125. /bin/cp /etc/sysconfig/{iptables,ip6tables}
  126. sed -i 's@icmp@icmpv6@g' /etc/sysconfig/ip6tables
  127. iptables-restore < /etc/sysconfig/iptables
  128. ip6tables-restore < /etc/sysconfig/ip6tables
  129. service iptables save
  130. service ip6tables save
  131. chkconfig --level 3 iptables on
  132. chkconfig --level 3 ip6tables on
  133. fi
  134. service rsyslog restart
  135. service sshd restart
  136. . /etc/profile