在 Debian(以及多数 Linux 发行版)上排查 ulimit 相关问题,通常可以按下面的思路和系统步骤来进行。
常见症状包括:
Too many open filesfork: Resource temporarily unavailableulimit -a 看到的值比你预期的小ulimit -a
常用单项查看:
ulimit -n # 打开文件数
ulimit -u # 用户最大进程数
ulimit -s # 栈大小
⚠️ 注意:
ulimit 是“进程级别”的,受 shell / 父进程影响
Debian 中 ulimit 可能来自以下多层:
echo $SHELL
ulimit -n
如果是 bash:
~/.bashrc
~/.profile
/etc/profile
/etc/bash.bashrc
Debian 默认通过 PAM 设置 limits:
/etc/security/limits.conf
/etc/security/limits.d/*.conf
示例:
* soft nofile 65535
* hard nofile 65535
检查是否生效:
grep pam_limits /etc/pam.d/common-session
systemd 不会读取 limits.conf
查看服务限制:
systemctl show nginx | grep Limit
临时修改:
[Service]
LimitNOFILE=65535
永久修改:
systemctl edit nginx
然后:
systemctl daemon-reload
systemctl restart nginx
查看系统级限制:
cat /proc/sys/fs/file-max
sysctl fs.file-max
修改:
sysctl -w fs.file-max=1000000
永久:
/etc/sysctl.conf
fs.file-max=1000000
cat /proc/<pid>/limits
例如:
cat /proc/$(pidof nginx)/limits
重点看:
✅ 你是否 重新登录 了?
(limits.conf 只对“新 session”生效)
✅ 你是否用的是 systemd 服务?
(limits.conf 对它无效)
✅ 你是否设置了 soft 和 hard?
soft 可超,hard 不能超
✅ 是否被 Docker / 容器限制?
容器默认有独立 ulimit
# 1. 当前 shell
ulimit -a
# 2. 登录用户 limits
cat /etc/security/limits.conf
ls /etc/security/limits.d/
# 3. 服务进程
systemctl show <service> | grep Limit
# 4. 实际进程
cat /proc/<pid>/limits
# 5. 系统级
sysctl fs.file-max
如果你能告诉我:
cat /etc/os-release)我可以给你更精确的诊断和修复方案。