init_Ubuntu.sh 6.2 KB

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