1. Linux 操作系统参数
1.1 file-max
说明:所有进程最大的文件数
| sysctl -w fs.file-max=2097152 |
| vim /etc/sysctl.conf |
| fs.file-max=2097152 |
| |
| sysctl -p |
1.2 ulimit
说明:用户最大句柄数
或
| vim /etc/security/limits.conf |
| * soft nofile 1048576 |
| * hard nofile 1048576 |
说明:在Centos7系统中,/etc/security/limits.conf
文件的配置作用域缩小了,其只适用于通过PAM认证登录用户的资源限制。而systemd service的资源设置,则需修改全局配置 /etc/systemd/system.conf
| # vim /etc/systemd/system.conf |
| DefaultLimitNOFILE=1048576 |
1.3 nr_open
说明:单个进程可分配的最大文件数
| vim /etc/sysctl.conf |
| fs.nr_open=2097152 |
| |
| sysctl -p |
| sysctl -w fs.nr_open=2097152 |
或者
| echo 2097152 > /proc/sys/fs/nr_open |
2. TCP 协议栈网络参数
在 /etc/sysctl.conf
中添加如下参数:
2.1 并发连接 backlog 设置
| net.ipv4.tcp_max_syn_backlog = 65536 |
| net.core.netdev_max_backlog = 32768 |
| net.core.somaxconn = 32768 |
2.2 可用端口范围
| net.ipv4.ip_local_port_range = 1024 65535 |
2.3 TCP Socket 读写 Buffer 设置
| net.core.wmem_default = 8388608 |
| net.core.rmem_default = 8388608 |
| net.core.rmem_max = 16777216 |
| net.core.wmem_max = 16777216 |
| net.core.optmem_max=16777216 |
| #net.ipv4.tcp_mem = 94500000 915000000 927000000 |
| net.ipv4.tcp_rmem=1024 4096 16777216 |
| net.ipv4.tcp_wmem=1024 4096 16777216 |
2.4 TCP 连接追踪设置
| net.nf_conntrack_max=1000000 |
| net.netfilter.nf_conntrack_max=1000000 |
| net.netfilter.nf_conntrack_tcp_timeout_time_wait=30 |
2.5 TIME-WAIT Socket 最大数量、回收与重用设置
| net.ipv4.tcp_max_tw_buckets=1048576 |
| #以下两个不建议开启該设置,NAT 模式下可能引起连接 RST |
| #net.ipv4.tcp_tw_recycle = 1 |
| #net.ipv4.tcp_tw_reuse = 1 |
说明:
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
2.6 FIN-WAIT-2 Socket 超时设置
| net.ipv4.tcp_max_tw_buckets=1048576 |
2.7 立即生效
同样执行如下命令立即生效:
3. 总结
| file-max=2097152 |
| fs.nr_open=2097152 |
| net.ipv4.tcp_max_syn_backlog = 65536 |
| net.core.netdev_max_backlog = 32768 |
| net.core.somaxconn = 32768 |
| net.ipv4.ip_local_port_range = 1024 65535 |
| net.core.wmem_default = 8388608 |
| net.core.rmem_default = 8388608 |
| net.core.rmem_max = 16777216 |
| net.core.wmem_max = 16777216 |
| net.core.optmem_max=16777216 |
| |
| net.ipv4.tcp_rmem=1024 4096 16777216 |
| net.ipv4.tcp_wmem=1024 4096 16777216 |
| net.nf_conntrack_max=1000000 |
| net.netfilter.nf_conntrack_max=1000000 |
| net.netfilter.nf_conntrack_tcp_timeout_time_wait=30 |
| net.ipv4.tcp_max_tw_buckets=1048576 |
| net.ipv4.tcp_max_tw_buckets=1048576 |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?