ubuntu内核参数优化

#Controls source route verification
net.ipv4.conf.default.rp_filter = 1 #启用源路由核查功能
net.ipv4.ip_nonlocal_bind = 1  #允许非本地IP地址socket监听
net.ipv4.ip_forward = 1 #开启IPV4转发
# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0 #禁用所有IP源路由
# Controls the System Request debugging functionality of the kernel
kernel.sysrq = 0 #关闭sysrq组合键
# Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications
kernel.core_uses_pid = 1 #控制core文件的文件名是否添加pid作为扩展
# Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = 1  #与性能无关。用于解决TCP的SYN攻击
# Controls the default maxmimum size of a mesage queue, in bytes
kernel.msgmnb = 65536 #每个消息队列的大小限制(单位:字节)
# Controls the maximum size of a message, in bytes
kernel.msgmax = 65536 #整个系统最大消息队列数量限制
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736 #单个共享内存段大小(单位:字节)
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296  #所有内存大小(单位:页)
# TCP kernel paramater
net.ipv4.tcp_mem = 786432 1048576 1572864 #1st低于此值,TCP没有内存压力,2nd进入内存压力阶段,3rd TCP拒绝分配socket(单位:内存页)
net.ipv4.tcp_rmem = 4096 87380 4194304  #定义TCP接受socket请求缓存的内存最小值、默认值、较大值
net.ipv4.tcp_wmem = 4096 16384 4194304  #定义TCP发送缓存的最小值、默认值、最大值
net.ipv4.tcp_window_scaling = 1 #支持更大的TCP窗口,如果TCP窗口最大超过65535(64K),必须设置该数值为1
net.ipv4.tcp_sack = 1 #开启有选择应答
# socket buffer
net.core.wmem_default = 8388608 #表示内核套接字接受缓存区默认大小
net.core.rmem_default = 8388608 #表示内核套接字发送缓存区默认大小
net.core.rmem_max = 16777216  #表示内核套接字接受缓存区较大大小
net.core.wmem_max = 16777216  #表示内核套接字发送缓存区较大大小
net.core.netdev_max_backlog = 262144 #当网卡接收数据包的速度大于内核处理速度时,会有一个队列保存这些数据。这个参数表示该队列的较大值
net.core.somaxconn = 20480 #默认值128,这个参数用于调节系统同时发起TCP连接数,在高并发的请求中,默认值可能会导致连接超时或者重传,因此需要结合高并发请求数来调整此值
net.core.optmem_max = 81920
# TCP conn
net.ipv4.tcp_max_syn_backlog = 262144 #这个参数表示TCP三次握手建立阶段接受SYN请求队列的较大长度,默认1024,将其设置大一些可使出现nginx繁忙来不及accept新连接,linux不至于丢失客户端发起的连接请求
net.ipv4.tcp_retries1 = 3 #该变量设置放弃回应一个tcp连接请求前,需要进行多少次重试。
net.ipv4.tcp_retries2 = 15 #控制内核向已经建立连接的远程主机重新发送数据的次数,低值可以更早的检测到与远程主机失效的连接,因此服务器可以更快的释放该连接,可以修改为5
net.ipv4.tcp_tw_recycle = 0 #快速回收TIME_WAIT状态,用于大量TIME_OUT场景
# tcp conn reuse
net.ipv4.tcp_timestamps = 0 #是否开启数据包时间戳
net.ipv4.tcp_tw_reuse = 0  #端口复用,参数值设置为1,表示允许将TIME_WAIT的状态socket重新用于新的TCP连接,这对于服务器来说意义重大,因为总是有大量TIME_WAIT状态的连接存在
net.ipv4.tcp_fin_timeout = 1 #当服务器主动关闭连接时,socket保持在FIN_WAIT_2状态的较大时间

net.ipv4.tcp_max_tw_buckets = 20000 #表示操作系统允许TIME_WAIT套接字数量的最大值,如果超过此值,TIME_WAIT套接字将立刻被清除并打印警告信息默认为8000,过多的TIME_WAIT套接字会使web服务变慢
net.ipv4.tcp_max_orphans = 3276800 #选项用于设置系统中最多有多少个TCP套接字不被关联到任何一个用户句柄上。如果超过这个数字,孤立连接数将立即被复位并输出警告信息。这个限制只是为了防止简单DOS攻击不用过分依靠这个限制,甚至认为减小这个值 ,更多的情况是增加这个值
net.ipv4.tcp_synack_retries = 1 #表示应用程序进行connect()系统调用时,在对方不返回SYN + ACK的情况下(也就是超时的情况下),第一次发送之后,内核最多重试几次发送SYN包;
net.ipv4.tcp_syncookies = 1  #开启SYN Cookies,当出现SYN等待队列溢出时,启用cookies来处理
# keepalive conn
net.ipv4.tcp_keepalive_time = 300 #当keepalive启动时,TCP发送keepalive消息的频度,默认2小时,将其设置为5分钟,可更快的清理无效连接
net.ipv4.tcp_keepalive_intvl = 30 #当探测没有确认时,重新发送探测的频度。缺省是75秒。
net.ipv4.tcp_keepalive_probes = 3 #在认定连接失效之前,发送多少个TCP的keepalive探测包。缺省值是9。这个值乘以tcp_keepalive_intvl之后决定了,一个连接发送了keepalive之后可以有多少时间没有回应
net.ipv4.ip_local_port_range = 10001 65000 #定义UDP和TCP连接的本地端口的取值范围
# swap
vm.overcommit_memory = 0 #0, 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。1, 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。2, 表示内核允许分配超过所有物理内存和交换空间总和的内存
vm.swappiness = 10 #数值可设置范围在0到100之间。 低参数值会让内核尽量少用交换,更高参数值会使内核更多的去使用交换空间。默认值为60。当系统存在足够内存时,推荐设置为该值以提高性能。

fs.file-max = 1000000 # 表示单个进程可以打开的句柄数
cat >> /etc/sysctl.conf << EOF
net.core.netdev_max_backlog = 32768
net.core.rmem_default = 8388608
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.lo.arp_announce = 0
net.ipv4.conf.lo.arp_ignore = 0
net.ipv4.ip_local_port_range = 5000 65000
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 65536
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_tw_reuse = 1
vm.max_map_count = 655360
vm.overcommit_memory = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
net.ipv6.conf.all.disable_ipv6 = 1
kernel.unknown_nmi_panic = 0
kernel.sysrq = 1
fs.file-max = 1000000
vm.swappiness = 10
fs.inotify.max_user_watches = 10000000
net.core.wmem_max = 327679
net.core.rmem_max = 327679
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.secure_redirects = 0
net.ipv4.conf.default.secure_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
fs.inotify.max_queued_events = 327679
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.neigh.default.gc_thresh1 = 2048
net.ipv4.neigh.default.gc_thresh2 = 4096
net.ipv4.neigh.default.gc_thresh3 = 8192
#net.netfilter.nf_conntrack_tcp_timeout_time_wait = 30
#net.nf_conntrack_max = 1000000
#net.netfilter.nf_conntrack_max = 1000000
EOF
posted @ 2021-10-15 11:14  小吉猫  阅读(1544)  评论(0编辑  收藏  举报