init_Debian.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. # http://oneinstack.com
  9. # https://github.com/lj2007331/oneinstack
  10. for Package in apache2 apache2-doc apache2-utils apache2.2-common apache2.2-bin apache2-mpm-prefork apache2-doc apache2-mpm-worker mysql-client mysql-server mysql-common libmysqlclient18 php5 php5-common php5-cgi php5-mysql php5-curl php5-gd libmysql* mysql-*
  11. do
  12. apt-get -y remove --purge $Package
  13. done
  14. dpkg -l | grep ^rc | awk '{print $2}' | xargs dpkg -P
  15. apt-get -y update
  16. # check upgrade OS
  17. [ "$upgrade_yn" == 'y' ] && apt-get -y upgrade
  18. # Install needed packages
  19. for Package in gcc g++ make cmake autoconf libjpeg8 libjpeg8-dev libjpeg-dev libpng12-0 libpng12-dev libpng3 libfreetype6 libfreetype6-dev libxml2 libxml2-dev zlib1g zlib1g-dev libc6 libc6-dev libglib2.0-0 libglib2.0-dev bzip2 libzip-dev libbz2-1.0 libncurses5 libncurses5-dev libaio1 libaio-dev libreadline-dev curl libcurl3 libcurl4-openssl-dev e2fsprogs libkrb5-3 libkrb5-dev libltdl-dev libidn11 libidn11-dev openssl libssl-dev libtool libevent-dev bison re2c libsasl2-dev libxslt1-dev locales libcloog-ppl0 patch vim zip unzip tmux htop wget bc expect rsync git
  20. do
  21. apt-get -y install $Package
  22. done
  23. # PS1
  24. [ -z "`cat ~/.bashrc | grep ^PS1`" ] && echo "PS1='\${debian_chroot:+(\$debian_chroot)}\\[\\e[1;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '" >> ~/.bashrc
  25. # history size
  26. [ -z "`cat ~/.bashrc | grep ^HISTSIZE`" ] && echo 'HISTSIZE=100' >> ~/.bashrc
  27. [ -z "`cat ~/.bashrc | grep history-timestamp`" ] && echo "export 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. [ -z "`cat /etc/security/limits.conf | grep 'nproc 65535'`" ] && cat >> /etc/security/limits.conf <<EOF
  30. * soft nproc 65535
  31. * hard nproc 65535
  32. * soft nofile 65535
  33. * hard nofile 65535
  34. EOF
  35. [ -z "`cat /etc/rc.local | grep 'ulimit -SH 65535'`" ] && echo "ulimit -SH 65535" >> /etc/rc.local
  36. # /etc/hosts
  37. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  38. # Set timezone
  39. rm -rf /etc/localtime
  40. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  41. # Set DNS
  42. #cat > /etc/resolv.conf << EOF
  43. #nameserver 114.114.114.114
  44. #nameserver 8.8.8.8
  45. #EOF
  46. # alias vi
  47. [ -z "`cat ~/.bashrc | grep 'alias vi='`" ] && sed -i "s@^alias l=\(.*\)@alias l=\1\nalias vi='vim'@" ~/.bashrc
  48. [ -z "`cat /etc/vim/vimrc | grep 'syntax on'`" ] && echo 'syntax on' >> /etc/vim/vimrc
  49. sed -i 's@^# export LS_OPTIONS@export LS_OPTIONS@' ~/.bashrc
  50. sed -i 's@^# alias@alias@g' ~/.bashrc
  51. # /etc/sysctl.conf
  52. [ -z "`cat /etc/sysctl.conf | grep 'fs.file-max'`" ] && cat >> /etc/sysctl.conf << EOF
  53. fs.file-max=65535
  54. net.ipv4.tcp_syncookies = 1
  55. net.ipv4.tcp_fin_timeout = 30
  56. net.ipv4.tcp_tw_reuse = 1
  57. net.ipv4.tcp_tw_recycle = 1
  58. net.ipv4.ip_local_port_range = 1024 65000
  59. net.ipv4.tcp_max_syn_backlog = 65536
  60. net.ipv4.tcp_max_tw_buckets = 20000
  61. net.ipv4.route.gc_timeout = 100
  62. net.ipv4.tcp_syn_retries = 1
  63. net.ipv4.tcp_synack_retries = 1
  64. net.core.somaxconn = 65535
  65. net.core.netdev_max_backlog = 262144
  66. net.ipv4.tcp_timestamps = 0
  67. net.ipv4.tcp_max_orphans = 262144
  68. EOF
  69. sysctl -p
  70. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES="/dev/tty[1-2]"@' /etc/default/console-setup
  71. sed -i 's@^[3-6]:23:respawn@#&@g' /etc/inittab
  72. sed -i "s@^ctrlaltdel@#&@" /etc/inittab
  73. sed -i 's@^# en_US.UTF-8@en_US.UTF-8@' /etc/locale.gen
  74. init q
  75. # Update time
  76. ntpdate pool.ntp.org
  77. [ -z "`grep 'pool.ntp.org' /var/spool/crontabs/root`" ] && { echo "*/20 * * * * `which ntpdate` pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/crontabs/root;chmod 600 /var/spool/cron/crontabs/root; }
  78. service cron restart
  79. # iptables
  80. 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
  81. IPTABLES_STATUS=yes
  82. else
  83. IPTABLES_STATUS=no
  84. fi
  85. if [ "$IPTABLES_STATUS" == 'no' ];then
  86. cat > /etc/iptables.up.rules << EOF
  87. # Firewall configuration written by system-config-securitylevel
  88. # Manual customization of this file is not recommended.
  89. *filter
  90. :INPUT DROP [0:0]
  91. :FORWARD ACCEPT [0:0]
  92. :OUTPUT ACCEPT [0:0]
  93. :syn-flood - [0:0]
  94. -A INPUT -i lo -j ACCEPT
  95. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  96. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  97. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  98. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  99. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  100. -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  101. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  102. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  103. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  104. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  105. COMMIT
  106. EOF
  107. fi
  108. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/iptables.up.rules`
  109. [ -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
  110. iptables-restore < /etc/iptables.up.rules
  111. echo 'pre-up iptables-restore < /etc/iptables.up.rules' >> /etc/network/interfaces
  112. service ssh restart
  113. . ~/.bashrc