1. #!/bin/bash
    2. #author junxi by
    3. #this script is only for CentOS 7.x
    4. #check the OS
    5. platform=`uname -i`
    6. if [ $platform != "x86_64" ];then
    7. echo "this script is only for 64bit Operating System !"
    8. exit 1
    9. fi
    10. echo "the platform is ok"
    11. cat << EOF
    12. +---------------------------------------+
    13. | your system is CentOS 7 x86_64 |
    14. | start optimizing....... |
    15. +---------------------------------------
    16. EOF
    17. #添加公网DNS地址
    18. cat >> /etc/resolv.conf << EOF
    19. nameserver 114.114.114.114
    20. EOF
    21. #Yum源更换为国内阿里源
    22. yum install wget telnet -y
    23. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    24. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    25. #添加阿里的epel源
    26. #add the epel
    27. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    28. # rpm -ivh http://dl.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
    29. #yum重新建立缓存
    30. yum clean all
    31. yum makecache
    32. #同步时间
    33. yum -y install ntp
    34. /usr/sbin/ntpdate cn.pool.ntp.org
    35. echo "* 4 * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null 2>&1" >> /var/spool/cron/root
    36. systemctl restart crond.service
    37. #安装vim
    38. #yum -y install vim
    39. #设置最大打开文件描述符数
    40. echo "ulimit -SHn 102400" >> /etc/rc.local
    41. cat >> /etc/security/limits.conf << EOF
    42. * soft nofile 655350
    43. * hard nofile 655350
    44. EOF
    45. ### * 表示对所有用户有限,soft指的是当前系统生效的设置,hard表明系统中所能设定的最大值,nofile表示所限制的资源是打开文件的最大数目,65535就是限制的数量
    46. #禁用selinux
    47. sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    48. setenforce 0
    49. #关闭防火墙
    50. systemctl disable firewalld.service
    51. systemctl stop firewalld.service
    52. #set ssh
    53. sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
    54. sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
    55. systemctl restart sshd.service
    56. #内核参数优化
    57. cat >> /etc/sysctl.conf << EOF
    58. vm.overcommit_memory = 1
    59. net.ipv4.ip_local_port_range = 1024 65536
    60. net.ipv4.tcp_fin_timeout = 1
    61. net.ipv4.tcp_mem = 94500000 915000000 927000000
    62. #tcp连接重用
    63. net.ipv4.tcp_tw_reuse = 1
    64. #tcp连接快速回收
    65. net.ipv4.tcp_tw_recycle = 1
    66. #262144允许等待中的监听
    67. net.core.somaxconn = 262144
    68. #这个参数应该设置足够大,以便能在一个共享内存段下容纳下整个的Innodb缓冲池的大小
    69. kernel.shmmax = 4294967295
    70. #告诉linux内核除非虚拟内存完全满了,否则不要使用交换区
    71. #vm.swappiness = 0
    72. net.ipv4.tcp_timestamps = 0
    73. net.ipv4.tcp_synack_retries = 1
    74. net.ipv4.tcp_syn_retries = 1
    75. net.ipv4.tcp_abort_on_overflow = 0
    76. #TCP接收和发送缓冲区默认大小和最大值
    77. net.core.wmem_default = 8388608
    78. net.core.rmem_default = 8388608
    79. net.core.rmem_max = 16777216
    80. net.core.wmem_max = 16777216
    81. #用于减少失效tcp所占用资源的时间
    82. net.ipv4.tcp_keepalive_time = 1200
    83. net.ipv4.tcp_keepalive_probes = 3
    84. net.ipv4.tcp_keepalive_intvl = 30
    85. net.core.netdev_max_backlog = 262144
    86. net.ipv4.tcp_max_orphans = 3276800
    87. net.ipv4.tcp_max_syn_backlog = 262144
    88. net.ipv4.netfilter.ip_conntrack_max = 2097152
    89. net.nf_conntrack_max = 655360
    90. net.netfilter.nf_conntrack_tcp_timeout_established = 1200
    91. ulimit -n 30000
    92. EOF
    93. /sbin/sysctl -p
    94. #vim定义退格键可删除最后一个字符类型
    95. echo 'alias vi=vim' >> /etc/profile
    96. echo 'stty erase ^H' >> /etc/profile
    97. cat >> /root/.vimrc << EOF
    98. set tabstop=4
    99. set shiftwidth=4
    100. set expandtab
    101. syntax on
    102. "set number
    103. EOF
    104. #update soft
    105. yum -y update
    106. yum -y install vim tree net-tools lrzsz gcc*
    107. cat << EOF
    108. +-------------------------------------------------+
    109. | optimizer is done |
    110. | it's recommond to restart this server ! |
    111. +-------------------------------------------------+
    112. EOF