init_Ubuntu.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # Custom profile
  11. cat > /etc/profile.d/oneinstack.sh << EOF
  12. HISTSIZE=10000
  13. HISTTIMEFORMAT="%F %T \$(whoami) "
  14. alias l='ls -AFhlt --color=auto'
  15. alias lh='l | head'
  16. alias ll='ls -l --color=auto'
  17. alias ls='ls --color=auto'
  18. alias vi=vim
  19. GREP_OPTIONS="--color=auto"
  20. alias grep='grep --color'
  21. alias egrep='egrep --color'
  22. alias fgrep='fgrep --color'
  23. EOF
  24. sed -i 's@^"syntax on@syntax on@' /etc/vim/vimrc
  25. # PS1
  26. [ -z "$(grep ^PS1 ~/.bashrc)" ] && echo "PS1='\${debian_chroot:+(\$debian_chroot)}\\[\\e[1;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '" >> ~/.bashrc
  27. # history
  28. [ -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
  29. # /etc/security/limits.conf
  30. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  31. [ -z "$(grep 'session required pam_limits.so' /etc/pam.d/common-session)" ] && echo "session required pam_limits.so" >> /etc/pam.d/common-session
  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 65535
  36. * hard nproc 65535
  37. * soft nofile 65535
  38. * hard nofile 65535
  39. root soft nproc 65535
  40. root hard nproc 65535
  41. root soft nofile 65535
  42. root 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. # /etc/sysctl.conf
  55. [ -z "$(grep 'fs.file-max' /etc/sysctl.conf)" ] && cat >> /etc/sysctl.conf << EOF
  56. fs.file-max=65535
  57. fs.inotify.max_user_instances = 8192
  58. net.ipv4.tcp_syncookies = 1
  59. net.ipv4.tcp_fin_timeout = 30
  60. net.ipv4.tcp_tw_reuse = 1
  61. net.ipv4.ip_local_port_range = 1024 65000
  62. net.ipv4.tcp_max_syn_backlog = 16384
  63. net.ipv4.tcp_max_tw_buckets = 6000
  64. net.ipv4.route.gc_timeout = 100
  65. net.ipv4.tcp_syn_retries = 1
  66. net.ipv4.tcp_synack_retries = 1
  67. net.core.somaxconn = 32768
  68. net.core.netdev_max_backlog = 32768
  69. net.ipv4.tcp_timestamps = 0
  70. net.ipv4.tcp_max_orphans = 32768
  71. EOF
  72. sysctl -p
  73. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES="/dev/tty[1-2]"@' /etc/default/console-setup
  74. sed -i 's@^@#@g' /etc/init/tty[3-6].conf
  75. locale-gen en_US.UTF-8
  76. echo "en_US.UTF-8 UTF-8" > /var/lib/locales/supported.d/local
  77. cat > /etc/default/locale << EOF
  78. LANG=en_US.UTF-8
  79. LANGUAGE=en_US:en
  80. EOF
  81. sed -i 's@^@#@g' /etc/init/control-alt-delete.conf
  82. # Update time
  83. ntpdate pool.ntp.org
  84. [ ! -e "/var/spool/cron/crontabs/root" -o -z "$(grep ntpdate /var/spool/cron/crontabs/root 2>/dev/null)" ] && { echo "*/20 * * * * $(which ntpdate) pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/crontabs/root;chmod 600 /var/spool/cron/crontabs/root; }
  85. # iptables
  86. if [ "${iptables_yn}" == 'y' ]; then
  87. if [ -e "/etc/iptables.up.rules" ] && [ -n "$(grep '^:INPUT DROP' /etc/iptables.up.rules)" -a -n "$(grep 'NEW -m tcp --dport 22 -j ACCEPT' /etc/iptables.up.rules)" -a -n "$(grep 'NEW -m tcp --dport 80 -j ACCEPT' /etc/iptables.up.rules)" ]; then
  88. IPTABLES_STATUS=yes
  89. else
  90. IPTABLES_STATUS=no
  91. fi
  92. if [ "${IPTABLES_STATUS}" == "no" ]; then
  93. [ -e "/etc/iptables.up.rules" ] && /bin/mv /etc/iptables.up.rules{,_bk}
  94. cat > /etc/iptables.up.rules << EOF
  95. # Firewall configuration written by system-config-securitylevel
  96. # Manual customization of this file is not recommended.
  97. *filter
  98. :INPUT DROP [0:0]
  99. :FORWARD ACCEPT [0:0]
  100. :OUTPUT ACCEPT [0:0]
  101. :syn-flood - [0:0]
  102. -A INPUT -i lo -j ACCEPT
  103. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  104. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  105. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  106. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  107. -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
  108. -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  109. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  110. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  111. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  112. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  113. COMMIT
  114. EOF
  115. fi
  116. FW_PORT_FLAG=$(grep -ow "dport ${ssh_port}" /etc/iptables.up.rules)
  117. [ -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.up.rules
  118. iptables-restore < /etc/iptables.up.rules
  119. cat > /etc/network/if-pre-up.d/iptables << EOF
  120. #!/bin/bash
  121. /sbin/iptables-restore < /etc/iptables.up.rules
  122. EOF
  123. chmod +x /etc/network/if-pre-up.d/iptables
  124. fi
  125. service rsyslog restart
  126. service ssh restart
  127. . /etc/profile
  128. . ~/.bashrc