init_CentOS.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. sed -i 's@^exclude@#exclude@' /etc/yum.conf
  11. yum clean all
  12. yum makecache
  13. if [ "$CentOS_RHEL_version" == '7' ];then
  14. yum -y groupremove "Basic Web Server" "MySQL Database server" "MySQL Database client" "File and Print Server"
  15. yum -y install iptables-services
  16. systemctl mask firewalld.service
  17. systemctl enable iptables.service
  18. elif [ "$CentOS_RHEL_version" == '6' ];then
  19. yum -y groupremove "FTP Server" "PostgreSQL Database client" "PostgreSQL Database server" "MySQL Database server" "MySQL Database client" "Web Server" "Office Suite and Productivity" "E-mail server" "Ruby Support" "Printing client"
  20. elif [ "$CentOS_RHEL_version" == '5' ];then
  21. yum -y groupremove "FTP Server" "Windows File Server" "PostgreSQL Database" "News Server" "MySQL Database" "DNS Name Server" "Web Server" "Dialup Networking Support" "Mail Server" "Ruby" "Office/Productivity" "Sound and Video" "Printing Support" "OpenFabrics Enterprise Distribution"
  22. fi
  23. yum check-update
  24. # Install needed packages
  25. for Package in deltarpm gcc gcc-c++ make cmake autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel libaio readline-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel libxslt-devel libicu-devel libevent-devel libtool libtool-ltdl bison gd-devel vim-enhanced pcre-devel zip unzip ntpdate sysstat patch bc expect rsync git lsof lrzsz
  26. do
  27. yum -y install $Package
  28. done
  29. yum -y update bash openssl glibc
  30. # use gcc-4.4
  31. if [ -n "`gcc --version | head -n1 | grep '4\.1\.'`" ];then
  32. yum -y install gcc44 gcc44-c++ libstdc++44-devel
  33. export CC="gcc44" CXX="g++44"
  34. fi
  35. # check sendmail
  36. #[ "$sendmail_yn" == 'y' ] && yum -y install sendmail && service sendmail restart
  37. # closed Unnecessary services and remove obsolete rpm package
  38. [ "$CentOS_RHEL_version" == '7' ] && [ "`systemctl is-active NetworkManager.service`" == 'active' ] && NM_flag=1
  39. for Service in `chkconfig --list | grep 3:on | awk '{print $1}' | grep -vE 'nginx|httpd|tomcat|mysqld|php-fpm|pureftpd|redis-server|memcached|supervisord|aegis|NetworkManager'`;do chkconfig --level 3 $Service off;done
  40. [ "$NM_flag" == '1' ] && systemctl enable NetworkManager.service
  41. for Service in sshd network crond iptables messagebus irqbalance syslog rsyslog sendmail;do chkconfig --level 3 $Service on;done
  42. # Close SELINUX
  43. setenforce 0
  44. sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
  45. # PS1
  46. [ -z "`grep ^PS1 ~/.bashrc`" ] && echo 'PS1="\[\e[37;40m\][\[\e[32;40m\]\u\[\e[37;40m\]@\h \[\e[35;40m\]\W\[\e[0m\]]\\$ "' >> ~/.bashrc
  47. # history size
  48. sed -i 's/^HISTSIZE=.*$/HISTSIZE=100/' /etc/profile
  49. [ -z "`grep history-timestamp ~/.bashrc`" ] && 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
  50. # /etc/security/limits.conf
  51. [ -e /etc/security/limits.d/*nproc.conf ] && rename nproc.conf nproc.conf_bk /etc/security/limits.d/*nproc.conf
  52. sed -i '/^# End of file/,$d' /etc/security/limits.conf
  53. cat >> /etc/security/limits.conf <<EOF
  54. # End of file
  55. * soft nproc 65535
  56. * hard nproc 65535
  57. * soft nofile 65535
  58. * hard nofile 65535
  59. EOF
  60. # /etc/hosts
  61. [ "$(hostname -i | awk '{print $1}')" != "127.0.0.1" ] && sed -i "s@^127.0.0.1\(.*\)@127.0.0.1 `hostname` \1@" /etc/hosts
  62. # Set timezone
  63. rm -rf /etc/localtime
  64. ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  65. # Set DNS
  66. #cat > /etc/resolv.conf << EOF
  67. #nameserver 114.114.114.114
  68. #nameserver 8.8.8.8
  69. #EOF
  70. # alias vi
  71. [ -z "`grep 'alias vi=' ~/.bashrc`" ] && sed -i "s@alias mv=\(.*\)@alias mv=\1\nalias vi=vim@" ~/.bashrc && echo 'syntax on' >> /etc/vimrc
  72. # /etc/sysctl.conf
  73. sed -i 's/net.ipv4.tcp_syncookies.*$/net.ipv4.tcp_syncookies = 1/g' /etc/sysctl.conf
  74. [ -z "`grep 'fs.file-max' /etc/sysctl.conf`" ] && cat >> /etc/sysctl.conf << EOF
  75. fs.file-max=65535
  76. fs.inotify.max_user_instances = 8192
  77. net.ipv4.tcp_fin_timeout = 30
  78. net.ipv4.tcp_tw_reuse = 1
  79. net.ipv4.tcp_tw_recycle = 1
  80. net.ipv4.ip_local_port_range = 1024 65000
  81. net.ipv4.tcp_max_syn_backlog = 65536
  82. net.ipv4.tcp_max_tw_buckets = 6000
  83. net.ipv4.route.gc_timeout = 100
  84. net.ipv4.tcp_syn_retries = 1
  85. net.ipv4.tcp_synack_retries = 1
  86. net.core.somaxconn = 65535
  87. net.core.netdev_max_backlog = 262144
  88. net.ipv4.tcp_timestamps = 0
  89. net.ipv4.tcp_max_orphans = 262144
  90. EOF
  91. sysctl -p
  92. if [ "$CentOS_RHEL_version" == '5' ];then
  93. sed -i 's@^[3-6]:2345:respawn@#&@g' /etc/inittab
  94. sed -i 's@^ca::ctrlaltdel@#&@' /etc/inittab
  95. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  96. elif [ "$CentOS_RHEL_version" == '6' ];then
  97. sed -i 's@^ACTIVE_CONSOLES.*@ACTIVE_CONSOLES=/dev/tty[1-2]@' /etc/sysconfig/init
  98. sed -i 's@^start@#start@' /etc/init/control-alt-delete.conf
  99. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/sysconfig/i18n
  100. [ -z "`grep net.netfilter.nf_conntrack_max /etc/sysctl.conf`" ] && cat >> /etc/sysctl.conf << EOF
  101. net.netfilter.nf_conntrack_max = 1048576
  102. net.netfilter.nf_conntrack_tcp_timeout_established = 1200
  103. EOF
  104. elif [ "$CentOS_RHEL_version" == '7' ];then
  105. sed -i 's@LANG=.*$@LANG="en_US.UTF-8"@g' /etc/locale.conf
  106. [ -z "`grep net.netfilter.nf_conntrack_max /etc/sysctl.conf`" ] && cat >> /etc/sysctl.conf << EOF
  107. net.netfilter.nf_conntrack_max = 1048576
  108. net.netfilter.nf_conntrack_tcp_timeout_established = 1200
  109. EOF
  110. fi
  111. init q
  112. # Update time
  113. ntpdate pool.ntp.org
  114. [ ! -e "/var/spool/cron/root" -o -z "`grep 'ntpdate' /var/spool/cron/root`" ] && { echo "*/20 * * * * `which ntpdate` pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root;chmod 600 /var/spool/cron/root; }
  115. service crond restart
  116. # iptables
  117. if [ -e '/etc/sysconfig/iptables' ] && [ -n "`grep ':INPUT DROP' /etc/sysconfig/iptables`" -a -n "`grep 'NEW -m tcp --dport 22 -j ACCEPT' /etc/sysconfig/iptables`" -a -n "`grep 'NEW -m tcp --dport 80 -j ACCEPT' /etc/sysconfig/iptables`" ];then
  118. IPTABLES_STATUS=yes
  119. else
  120. IPTABLES_STATUS=no
  121. fi
  122. if [ "$IPTABLES_STATUS" == 'no' ];then
  123. [ -e '/etc/sysconfig/iptables' ] && /bin/mv /etc/sysconfig/iptables{,_bk}
  124. cat > /etc/sysconfig/iptables << EOF
  125. # Firewall configuration written by system-config-securitylevel
  126. # Manual customization of this file is not recommended.
  127. *filter
  128. :INPUT DROP [0:0]
  129. :FORWARD ACCEPT [0:0]
  130. :OUTPUT ACCEPT [0:0]
  131. :syn-flood - [0:0]
  132. -A INPUT -i lo -j ACCEPT
  133. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  134. -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
  135. -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
  136. -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
  137. -A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
  138. -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
  139. -A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
  140. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  141. -A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
  142. -A syn-flood -j REJECT --reject-with icmp-port-unreachable
  143. COMMIT
  144. EOF
  145. fi
  146. FW_PORT_FLAG=`grep -ow "dport $SSH_PORT" /etc/sysconfig/iptables`
  147. [ -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/sysconfig/iptables
  148. service iptables restart
  149. service sshd restart
  150. # install tmux
  151. if [ ! -e "`which tmux`" ];then
  152. cd src
  153. src_url=http://mirrors.linuxeye.com/oneinstack/src/libevent-2.0.22-stable.tar.gz && Download_src
  154. src_url=http://mirrors.linuxeye.com/oneinstack/src/tmux-2.2.tar.gz && Download_src
  155. tar xzf libevent-2.0.22-stable.tar.gz
  156. cd libevent-2.0.22-stable
  157. ./configure
  158. make -j ${THREAD} && make install
  159. cd ..
  160. tar xzf tmux-2.2.tar.gz
  161. cd tmux-2.2
  162. CFLAGS="-I/usr/local/include" LDFLAGS="-L//usr/local/lib" ./configure
  163. make -j ${THREAD} && make install
  164. cd ../../
  165. if [ "$OS_BIT" == '64' ];then
  166. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
  167. else
  168. ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
  169. fi
  170. fi
  171. # install htop
  172. if [ ! -e "`which htop`" ];then
  173. cd src
  174. src_url=http://hisham.hm/htop/releases/2.0.2/htop-2.0.2.tar.gz && Download_src
  175. tar xzf htop-2.0.2.tar.gz
  176. cd htop-2.0.2
  177. ./configure
  178. make -j ${THREAD} && make install
  179. cd ../../
  180. fi
  181. . /etc/profile
  182. . ~/.bashrc