init_Ubuntu.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. # https://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. # critical security updates
  17. grep security /etc/apt/sources.list > /tmp/security.sources.list
  18. apt-get -y upgrade -o Dir::Etc::SourceList=/tmp/security.sources.list
  19. # Install needed packages
  20. 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 libicu-dev patch vim zip unzip tmux htop bc expect rsync git lsof lrzsz ntpdate
  21. do
  22. apt-get -y install $Package
  23. done
  24. if [[ "$Ubuntu_version" =~ ^14$|^15$ ]]; then
  25. apt-get -y install libcloog-ppl1
  26. apt-get -y remove bison
  27. pushd ${oneinstack_dir}/src
  28. tar xzf bison-${bison_version}.tar.gz
  29. pushd bison-${bison_version}
  30. ./configure
  31. make -j ${THREAD} && make install
  32. popd
  33. rm -rf bison-${bison_version}
  34. ln -sf /usr/include/freetype2 /usr/include/freetype2/freetype
  35. elif [ "$Ubuntu_version" == '13' ]; then
  36. apt-get -y install bison libcloog-ppl1
  37. elif [ "$Ubuntu_version" == '12' ]; then
  38. apt-get -y install bison libcloog-ppl0
  39. fi
  40. # Custom profile
  41. cat > /etc/profile.d/oneinstack.sh << EOF
  42. HISTSIZE=10000
  43. HISTTIMEFORMAT="%F %T \`whoami\` "
  44. alias l='ls -AFhlt --color=auto'
  45. alias lh='l | head'
  46. alias ll='ls -l --color=auto'
  47. alias ls='ls --color=auto'
  48. alias vi=vim
  49. GREP_OPTIONS="--color=auto"
  50. alias grep='grep --color'
  51. alias egrep='egrep --color'
  52. alias fgrep='fgrep --color'
  53. EOF
  54. sed -i 's@^"syntax on@syntax on@' /etc/vim/vimrc
  55. # PS1
  56. [ -z "`grep ^PS1 ~/.bashrc`" ] && echo "PS1='\${debian_chroot:+(\$debian_chroot)}\\[\\e[1;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '" >> ~/.bashrc
  57. # history
  58. [ -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
  59. # /etc/security/limits.conf
  60. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  61. [ -z "`grep 'session required pam_limits.so' /etc/pam.d/common-session`" ] && echo 'session required pam_limits.so' >> /etc/pam.d/common-session
  62. sed -i '/^# End of file/,$d' /etc/security/limits.conf
  63. cat >> /etc/security/limits.conf <<EOF
  64. # End of file
  65. * soft nproc 65535
  66. * hard nproc 65535
  67. * soft nofile 65535
  68. * hard nofile 65535
  69. root soft nproc 65535
  70. root hard nproc 65535
  71. root soft nofile 65535
  72. root hard nofile 65535
  73. EOF
  74. # /etc/hosts
  75. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  76. # Set timezone
  77. rm -rf /etc/localtime
  78. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  79. # Set DNS
  80. #cat > /etc/resolv.conf << EOF
  81. #nameserver 114.114.114.114
  82. #nameserver 8.8.8.8
  83. #EOF
  84. # /etc/sysctl.conf
  85. [ -z "`grep 'fs.file-max' /etc/sysctl.conf`" ] && cat >> /etc/sysctl.conf << EOF
  86. fs.file-max=65535
  87. fs.inotify.max_user_instances = 8192
  88. net.ipv4.tcp_syncookies = 1
  89. net.ipv4.tcp_fin_timeout = 30
  90. net.ipv4.tcp_tw_reuse = 1
  91. net.ipv4.tcp_tw_recycle = 1
  92. net.ipv4.ip_local_port_range = 1024 65000
  93. net.ipv4.tcp_max_syn_backlog = 65536
  94. net.ipv4.tcp_max_tw_buckets = 6000
  95. net.ipv4.route.gc_timeout = 100
  96. net.ipv4.tcp_syn_retries = 1
  97. net.ipv4.tcp_synack_retries = 1
  98. net.core.somaxconn = 65535
  99. net.core.netdev_max_backlog = 262144
  100. net.ipv4.tcp_timestamps = 0
  101. net.ipv4.tcp_max_orphans = 262144
  102. EOF
  103. sysctl -p
  104. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES="/dev/tty[1-2]"@' /etc/default/console-setup
  105. sed -i 's@^@#@g' /etc/init/tty[3-6].conf
  106. echo 'en_US.UTF-8 UTF-8' > /var/lib/locales/supported.d/local
  107. sed -i 's@^@#@g' /etc/init/control-alt-delete.conf
  108. # Update time
  109. ntpdate pool.ntp.org
  110. [ ! -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; }
  111. # iptables
  112. 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
  113. IPTABLES_STATUS=yes
  114. else
  115. IPTABLES_STATUS=no
  116. fi
  117. if [ "$IPTABLES_STATUS" == 'no' ]; then
  118. [ -e '/etc/iptables.up.rules' ] && /bin/mv /etc/iptables.up.rules{,_bk}
  119. cat > /etc/iptables.up.rules << EOF
  120. # Firewall configuration written by system-config-securitylevel
  121. # Manual customization of this file is not recommended.
  122. *filter
  123. :INPUT DROP [0:0]
  124. :FORWARD ACCEPT [0:0]
  125. :OUTPUT ACCEPT [0:0]
  126. :syn-flood - [0:0]
  127. -A INPUT -i lo -j ACCEPT
  128. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  129. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  130. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  131. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  132. -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
  133. -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  134. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  135. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  136. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  137. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  138. COMMIT
  139. EOF
  140. fi
  141. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/iptables.up.rules`
  142. [ -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
  143. iptables-restore < /etc/iptables.up.rules
  144. cat > /etc/network/if-pre-up.d/iptables << EOF
  145. #!/bin/bash
  146. /sbin/iptables-restore < /etc/iptables.up.rules
  147. EOF
  148. chmod +x /etc/network/if-pre-up.d/iptables
  149. service ssh restart
  150. . /etc/profile
  151. . ~/.bashrc
  152. popd