進程打開文件描述符 (FD) 數量超過系統/用戶限制。Linux 默認單進程軟限制 1024,硬限制 4096 (可用 ulimit -n 查看)。
ulimit -a
# open files (-n) 1024
ulimit -Sn # 軟限制
ulimit -Hn # 硬限制

ulimit -n 65535
# 或分別設置
ulimit -Sn 65535
ulimit -Hn 65535
注意:非 root 用戶只能調低硬限制,不能超過硬限制設置軟限制。
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
* 代表所有用戶,nofile 為最大打開文件數。
echo "ulimit -SHn 65535" >> /etc/profile
需確保 /etc/profile 中無衝突的 ulimit -S -c 0 等語句覆蓋。
# /etc/systemd/system/myservice.service.d/limits.conf
[Service]
LimitNOFILE=65535
然後 systemctl daemon-reload && systemctl restart myservice。
fs.file-max = 2097152
# 生效
sysctl -p
此為**全系統**最大 FD 總和,需大於所有進程限制之和。
# 重新登錄或 su - user
ulimit -n
# 應顯示 65535
# 查看進程實際限制
cat /proc//limits | grep "Max open files"
lsof -p 統計數量持續增長)。net.core.somaxconn, net.ipv4.tcp_max_syn_backlog 等聯合調優。