balabala一个内核优化的脚本

#!/bin/bash
#技术管理 作者:张浩
#centos7 系统内核优化
#启用ip转发功能   0不启用  1启用
sed -i "s/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g" '/etc/sysctl.conf'
#选项默认值是128,这个参数用于调节系统同时发起的tcp连接数,在高并发请求中,默认的值可能会导致连接超时或重传,因此,需要结合并发请求数来调节此值。该参数对应系统路径为:/proc/sys/net/core/somaxconn 128
echo -e "net.core.somaxconn = 65535" >> /etc/sysctl.conf
#参数net.core.netdev_max_backlog表示当每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许发送到队列的数据包的最大数目,一般默认值为128(可能不同的linux系统该数值也不同)。nginx服务器中定义的NGX_LISTEN_BACKLOG默认为511
echo -e "net.core.netdev_max_backlog = 262144" >> /etc/sysctl.conf
#该参数指定了发送套接字缓冲区大小的缺省值(以字节为单位)
echo -e "net.core.wmem_default = 8388608" >> /etc/sysctl.conf
#该参数指定了接收套接字缓冲区大小的缺省值(以字节为单位)
echo -e "net.core.rmem_default = 8388608" >> /etc/sysctl.conf
#该参数指定了接收套接字缓冲区大小的最大值
echo -e "net.core.rmem_max = 16777216" >> /etc/sysctl.conf
#该参数指定了发送套接字缓冲区大小的最大值
echo -e "net.core.wmem_max = 16777216" >> /etc/sysctl.conf
#转发最大值
echo -e "net.ipv4.route.max_size = 5242880" >> /etc/sysctl.conf
#gc超时时间
echo -e "net.ipv4.route.gc_timeout = 20" >> /etc/sysctl.conf
#设置本地端口范围,缺省情况下:32768到61000,现在改为 1025 到 65535,最小值不能设置太低,否则占用了正常端口
echo -e "net.ipv4.ip_local_port_range = 1025 65535" >> /etc/sysctl.conf
#TCP失败重传次数,默认值15,意味着重传15次才彻底放弃,可减少到5,以尽早释放内核资源
echo -e "net.ipv4.tcp_retries2 = 5" >> /etc/sysctl.conf
#设置TCP三次请求的fin状态超时
echo -e "net.ipv4.tcp_fin_timeout = 30" >> /etc/sysctl.conf
#对于一个新建连接,内核要发送多少个SYN连接请求才决定放弃。不应该大于255,默认值是5,对应于180秒左右时间。(对于大负载而物理通信良好的网络而言,这个值偏高,可修改为2.这个值仅仅是针对对外的连接,对进来的连接,是由tcp_retries1 决定的)
echo -e "net.ipv4.tcp_syn_retries = 3" >> /etc/sysctl.conf
#对于远端的连接请求SYN,内核会发送SYN+ACK数据报,以确认收到上一个SYN连接请求包。这是所谓的三次握手(threeway handshake)机制的第二个步骤。这里决定内核在放弃连接之前所送出的SYN+ACK数目。不应该大于255,默认值是5,对应于180秒左右时间。(可以根据tcp_syn_retries来决定这个值)
echo -e "net.ipv4.tcp_synack_retries = 3" >> /etc/sysctl.conf
#默认为1
echo -e "net.ipv4.tcp_timestamps = 0" >> /etc/sysctl.conf
#允许将TIME_WAIT sockets快速回收以便利用,默认为0,表示关闭,需要同时开启 net.ipv4.tcp_timestamps 才能生效
echo -e "net.ipv4.tcp_tw_recycle = 0" >> /etc/sysctl.conf
#允许将TIME_WAIT sockets重新用于新的TCP连接,默认为0,表示关闭
echo -e "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
#设置TCP 发送keepalive的频度,默认的缺省为2小时,600秒表示10分钟,表示服务器以10分钟发送keepalive消息
echo -e "net.ipv4.tcp_keepalive_time = 120" >> /etc/sysctl.conf
#如果对方不给予应答,探测包发送的次数,默认9次
echo -e "net.ipv4.tcp_keepalive_probes = 3" >> /etc/sysctl.conf
#探测包发送的时间间隔设置为2秒,默认75秒
echo -e "net.ipv4.tcp_keepalive_intvl = 15" >> /etc/sysctl.conf
#设置保持TIME_WAIT的最大数量,如果超过这个数量,TIME_WAIT将立刻清楚并打印警告信息,默认为180000,改为5000.此项参数可以控制TIME_WAIT的最大数量,避免Squid服务器被大量的TIME_WAIT拖死
echo -e "net.ipv4.tcp_max_tw_buckets = 200000" >> /etc/sysctl.conf
#限制仅仅是为了防止简单的DoS 攻击
echo -e "net.ipv4.tcp_max_orphans = 3276800" >> /etc/sysctl.conf
#表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多的网络连接数
echo -e "net.ipv4.tcp_max_syn_backlog = 262144" >> /etc/sysctl.conf
#增加tcp缓冲区大小,tcp_rmem表示接受数据缓冲区范围,tcp_wmem表示发送数据缓冲区范围,单位Byte,最大64M
echo -e "net.ipv4.tcp_wmem = 8192 131072 16777216" >> /etc/sysctl.conf
echo -e "net.ipv4.tcp_rmem = 32768 131072 16777216" >> /etc/sysctl.conf

echo -e "net.ipv4.tcp_mem = 94500000 915000000 927000000" >> /etc/sysctl.conf

echo -e "net.nf_conntrack_max = 25000000" >> /etc/sysctl.conf
#解决系统丢包的问题
echo -e "net.netfilter.nf_conntrack_max = 25000000" >> /etc/sysctl.conf
#设置tcp确认超时时间 300秒,默认 432000 秒(5天)
echo -e "net.netfilter.nf_conntrack_tcp_timeout_established = 180" >> /etc/sysctl.conf
#设置tcp等待时间 12秒,超过12秒自动放弃,默认120秒
echo -e "net.netfilter.nf_conntrack_tcp_timeout_time_wait = 12" >> /etc/sysctl.conf
#设置tcp关闭等待时间60秒,超过60秒自动关闭,默认60秒
echo -e "net.netfilter.nf_conntrack_tcp_timeout_close_wait = 60" >> /etc/sysctl.conf
#设置tcp fin状态的超时时间为120秒,超过该时间自动关闭,默认120秒
echo -e "net.netfilter.nf_conntrack_tcp_timeout_fin_wait = 120" >> /etc/sysctl.conf

echo -e "net.unix.max_dgram_qlen = 655360" >> /etc/sysctl.conf

echo -e "kernel.msgmnb = 655360" >> /etc/sysctl.conf

echo -e "kernel.msgmax = 655360" >> /etc/sysctl.conf

echo -e "kernel.msgmni = 20480" >> /etc/sysctl.conf

echo -e "* soft nproc unlimited" >> /etc/security/limits.conf

echo -e "* hard nproc unlimited" >> /etc/security/limits.conf

echo -e "* soft nofile 65535" >> /etc/security/limits.conf

echo -e "* hard nofile 65535" >> /etc/security/limits.conf

echo -e "ulimit -SHn 65535" >> /etc/profile

echo -e "ulimit -SHu unlimited" >> /etc/profile

echo -e "ulimit -SHd unlimited" >> /etc/profile

echo -e "ulimit -SHm unlimited" >> /etc/profile

echo -e "ulimit -SHs unlimited" >> /etc/profile

echo -e "ulimit -SHt unlimited"  >> /etc/profile

echo -e "ulimit -SHv unlimited" >> /etc/profile

source /etc/profile

ulimit -a 

sysctl -p

cat /var/log/secure | grep error
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值