init_Debian.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. # Author: yeho <lj2007331 AT gmail.com>
  3. # Blog: http://blog.linuxeye.com
  4. # add swapfile
  5. Mem=`free -m | awk '/Mem:/{print $2}'`
  6. Swap=`free -m | awk '/Swap:/{print $2}'`
  7. if [ "$Swap" == '0' ] ;then
  8. if [ $Mem -le 1024 ];then
  9. dd if=/dev/zero of=/swapfile count=1024 bs=1M
  10. mkswap /swapfile
  11. swapon /swapfile
  12. chmod 600 /swapfile
  13. elif [ $Mem -gt 1024 -a $Mem -le 2048 ];then
  14. dd if=/dev/zero of=/swapfile count=2048 bs=1M
  15. mkswap /swapfile
  16. swapon /swapfile
  17. chmod 600 /swapfile
  18. fi
  19. cat >> /etc/fstab << EOF
  20. /swapfile swap swap defaults 0 0
  21. EOF
  22. fi
  23. 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-*
  24. do
  25. apt-get -y remove --purge $Package
  26. done
  27. dpkg -l | grep ^rc | awk '{print $2}' | xargs dpkg -P
  28. apt-get -y update
  29. # check upgrade OS
  30. [ "$upgrade_yn" == 'y' ] && apt-get -y upgrade
  31. # Install needed packages
  32. 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
  33. do
  34. apt-get -y install $Package
  35. done
  36. # PS1
  37. [ -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
  38. # history size
  39. [ -z "`cat ~/.bashrc | grep ^HISTSIZE`" ] && echo 'HISTSIZE=100' >> ~/.bashrc
  40. [ -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
  41. # /etc/security/limits.conf
  42. [ -z "`cat /etc/security/limits.conf | grep 'nproc 65535'`" ] && cat >> /etc/security/limits.conf <<EOF
  43. * soft nproc 65535
  44. * hard nproc 65535
  45. * soft nofile 65535
  46. * hard nofile 65535
  47. EOF
  48. [ -z "`cat /etc/rc.local | grep 'ulimit -SH 65535'`" ] && echo "ulimit -SH 65535" >> /etc/rc.local
  49. # /etc/hosts
  50. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  51. # Set timezone
  52. rm -rf /etc/localtime
  53. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  54. # Set DNS
  55. #cat > /etc/resolv.conf << EOF
  56. #nameserver 114.114.114.114
  57. #nameserver 8.8.8.8
  58. #EOF
  59. # alias vi
  60. [ -z "`cat ~/.bashrc | grep 'alias vi='`" ] && sed -i "s@^alias l=\(.*\)@alias l=\1\nalias vi='vim'@" ~/.bashrc
  61. [ -z "`cat /etc/vim/vimrc | grep 'syntax on'`" ] && echo 'syntax on' >> /etc/vim/vimrc
  62. sed -i 's@^# export LS_OPTIONS@export LS_OPTIONS@' ~/.bashrc
  63. sed -i 's@^# alias@alias@g' ~/.bashrc
  64. # /etc/sysctl.conf
  65. [ -z "`cat /etc/sysctl.conf | grep 'fs.file-max'`" ] && cat >> /etc/sysctl.conf << EOF
  66. fs.file-max=65535
  67. net.ipv4.tcp_syncookies = 1
  68. net.ipv4.tcp_fin_timeout = 30
  69. net.ipv4.tcp_tw_reuse = 1
  70. net.ipv4.tcp_tw_recycle = 1
  71. net.ipv4.ip_local_port_range = 1024 65000
  72. net.ipv4.tcp_max_syn_backlog = 65536
  73. net.ipv4.tcp_max_tw_buckets = 20000
  74. net.ipv4.route.gc_timeout = 100
  75. net.ipv4.tcp_syn_retries = 1
  76. net.ipv4.tcp_synack_retries = 1
  77. net.core.somaxconn = 65535
  78. net.core.netdev_max_backlog = 262144
  79. net.ipv4.tcp_timestamps = 0
  80. net.ipv4.tcp_max_orphans = 262144
  81. EOF
  82. sysctl -p
  83. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES="/dev/tty[1-2]"@' /etc/default/console-setup
  84. sed -i 's@^[3-6]:23:respawn@#&@g' /etc/inittab
  85. sed -i "s@^ctrlaltdel@#&@" /etc/inittab
  86. sed -i 's@^# en_US.UTF-8@en_US.UTF-8@' /etc/locale.gen
  87. init q
  88. # Update time
  89. ntpdate pool.ntp.org
  90. [ -z "`grep 'pool.ntp.org' /var/spool/cron/root`" ] && { echo "*/20 * * * * `which ntpdate` pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/crontabs/root;chmod 600 /var/spool/cron/crontabs/root; }
  91. service cron restart
  92. # iptables
  93. 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
  94. IPTABLES_STATUS=yes
  95. else
  96. IPTABLES_STATUS=no
  97. fi
  98. if [ "$IPTABLES_STATUS" == 'no' ];then
  99. cat > /etc/iptables.up.rules << EOF
  100. # Firewall configuration written by system-config-securitylevel
  101. # Manual customization of this file is not recommended.
  102. *filter
  103. :INPUT DROP [0:0]
  104. :FORWARD ACCEPT [0:0]
  105. :OUTPUT ACCEPT [0:0]
  106. :syn-flood - [0:0]
  107. -A INPUT -i lo -j ACCEPT
  108. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  109. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  110. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  111. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  112. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  113. -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  114. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  115. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  116. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  117. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  118. COMMIT
  119. EOF
  120. fi
  121. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/iptables.up.rules`
  122. [ -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
  123. iptables-restore < /etc/iptables.up.rules
  124. echo 'pre-up iptables-restore < /etc/iptables.up.rules' >> /etc/network/interfaces
  125. service ssh restart
  126. . ~/.bashrc