init_Debian.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. # Custom profile
  11. cat > /etc/profile.d/oneinstack.sh << EOF
  12. HISTSIZE=10000
  13. PS1='\${debian_chroot:+(\$debian_chroot)}\\[\\e[1;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '
  14. HISTTIMEFORMAT="%F %T \$(whoami) "
  15. alias l='ls -AFhlt --color=auto'
  16. alias lh='l | head'
  17. alias ll='ls -l --color=auto'
  18. alias ls='ls --color=auto'
  19. alias vi=vim
  20. GREP_OPTIONS="--color=auto"
  21. alias grep='grep --color'
  22. alias egrep='egrep --color'
  23. alias fgrep='fgrep --color'
  24. EOF
  25. sed -i 's@^"syntax on@syntax on@' /etc/vim/vimrc
  26. # history
  27. [ -z "$(grep history-timestamp ~/.bashrc)" ] && echo "PROMPT_COMMAND='{ msg=\$(history 1 | { read x y; echo \$y; });user=\$(whoami); echo \$(date \"+%Y-%m-%d %H:%M:%S\"):\$user:\`pwd\`/:\$msg ---- \$(who am i); } >> /tmp/\`hostname\`.\`whoami\`.history-timestamp'" >> ~/.bashrc
  28. # /etc/security/limits.conf
  29. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  30. [ -z "$(grep 'session required pam_limits.so' /etc/pam.d/common-session)" ] && echo "session required pam_limits.so" >> /etc/pam.d/common-session
  31. sed -i '/^# End of file/,$d' /etc/security/limits.conf
  32. cat >> /etc/security/limits.conf <<EOF
  33. # End of file
  34. * soft nproc 1000000
  35. * hard nproc 1000000
  36. * soft nofile 1000000
  37. * hard nofile 1000000
  38. root soft nproc 1000000
  39. root hard nproc 1000000
  40. root soft nofile 1000000
  41. root hard nofile 1000000
  42. EOF
  43. # /etc/hosts
  44. [ "$(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
  45. # Set timezone
  46. rm -rf /etc/localtime
  47. ln -s /usr/share/zoneinfo/${timezone} /etc/localtime
  48. # Set DNS
  49. #cat > /etc/resolv.conf << EOF
  50. #nameserver 114.114.114.114
  51. #nameserver 8.8.8.8
  52. #EOF
  53. # /etc/sysctl.conf
  54. [ -z "$(grep 'fs.file-max' /etc/sysctl.conf)" ] && cat >> /etc/sysctl.conf << EOF
  55. fs.file-max = 1000000
  56. fs.inotify.max_user_instances = 8192
  57. net.ipv4.tcp_syncookies = 1
  58. net.ipv4.tcp_fin_timeout = 30
  59. net.ipv4.tcp_tw_reuse = 1
  60. net.ipv4.ip_local_port_range = 1024 65000
  61. net.ipv4.tcp_max_syn_backlog = 16384
  62. net.ipv4.tcp_max_tw_buckets = 6000
  63. net.ipv4.route.gc_timeout = 100
  64. net.ipv4.tcp_syn_retries = 1
  65. net.ipv4.tcp_synack_retries = 1
  66. net.core.somaxconn = 32768
  67. net.core.netdev_max_backlog = 32768
  68. net.ipv4.tcp_timestamps = 0
  69. net.ipv4.tcp_max_orphans = 32768
  70. EOF
  71. sysctl -p
  72. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES="/dev/tty[1-2]"@' /etc/default/console-setup
  73. sed -i 's@^# en_US.UTF-8@en_US.UTF-8@' /etc/locale.gen
  74. init q
  75. # Update time
  76. if [ -e "$(which ntpdate)" ]; then
  77. ntpdate -u pool.ntp.org
  78. [ ! -e "/var/spool/cron/crontabs/root" -o -z "$(grep ntpdate /var/spool/cron/crontabs/root 2>/dev/null)" ] && { echo "*/20 * * * * $(which ntpdate) -u pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/crontabs/root;chmod 600 /var/spool/cron/crontabs/root; }
  79. fi
  80. # iptables
  81. if [ "${iptables_flag}" == 'y' ]; then
  82. apt-get -y install debconf-utils
  83. echo iptables-persistent iptables-persistent/autosave_v4 boolean true | debconf-set-selections
  84. echo iptables-persistent iptables-persistent/autosave_v6 boolean true | debconf-set-selections
  85. apt-get -y install iptables-persistent
  86. if [ -e "/etc/iptables/rules.v4" ] && [ -n "$(grep '^:INPUT DROP' /etc/iptables/rules.v4)" -a -n "$(grep 'NEW -m tcp --dport 22 -j ACCEPT' /etc/iptables/rules.v4)" -a -n "$(grep 'NEW -m tcp --dport 80 -j ACCEPT' /etc/iptables/rules.v4)" ]; then
  87. IPTABLES_STATUS=yes
  88. else
  89. IPTABLES_STATUS=no
  90. fi
  91. if [ "${IPTABLES_STATUS}" == "no" ]; then
  92. cat > /etc/iptables/rules.v4 << EOF
  93. # Firewall configuration written by system-config-securitylevel
  94. # Manual customization of this file is not recommended.
  95. *filter
  96. :INPUT DROP [0:0]
  97. :FORWARD ACCEPT [0:0]
  98. :OUTPUT ACCEPT [0:0]
  99. :syn-flood - [0:0]
  100. -A INPUT -i lo -j ACCEPT
  101. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  102. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  103. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  104. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  105. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  106. COMMIT
  107. EOF
  108. fi
  109. FW_PORT_FLAG=$(grep -ow "dport ${ssh_port}" /etc/iptables/rules.v4)
  110. [ -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/iptables/rules.v4
  111. iptables-restore < /etc/iptables/rules.v4
  112. /bin/cp /etc/iptables/rules.v{4,6}
  113. sed -i 's@icmp@icmpv6@g' /etc/iptables/rules.v6
  114. ip6tables-restore < /etc/iptables/rules.v6
  115. ip6tables-save > /etc/iptables/rules.v6
  116. fi
  117. service rsyslog restart
  118. service ssh restart
  119. . /etc/profile
  120. . ~/.bashrc