在 Debian 系统中,ulimit 不生效是一个非常常见的问题,通常不是命令本身的问题,而是作用范围、配置位置或系统机制导致的。下面按最常见原因 → 排查方法 → 解决方法来说明。
/etc/security/limits.conf,但 PAM 没启用ulimit 只对当前 shell 及其子进程生效limits.conf)Limit*)/etc/security/limits.conf
/etc/security/limits.d/*.conf
示例:
* soft nofile 65535
* hard nofile 65535
✅ PAM 已启用
检查:
grep pam_limits /etc/pam.d/common-session
grep pam_limits /etc/pam.d/common-session-noninteractive
应看到:
session required pam_limits.so
如果没有:
apt install libpam-modules
⚠️ 修改后 必须重新登录
SSH 默认不加载某些环境
解决:
vi /etc/ssh/sshd_config
确保:
UsePAM yes
重启:
systemctl restart ssh
这是 Debian 10+ 最常见原因
例如:
systemctl restart nginx
systemctl edit nginx
写入:
[Service]
LimitNOFILE=65535
生效:
systemctl daemon-reload
systemctl restart nginx
验证:
cat /proc/$(pgrep nginx | head -1)/limits
默认:
root 不受 limits.conf 中 * 的限制
必须显式写:
root soft nofile 65535
root hard nofile 65535
docker run --ulimit nofile=65535:65535 ...
ulimits:
nofile:
soft: 65535
hard: 65535
❌ 错误:
ulimit -n 65535
./app &
✅ 正确:
ulimit -n 65535
exec ./app
或直接:
( ulimit -n 65535; ./app )
# 当前限制
ulimit -a
# 进程实际限制
cat /proc/<pid>/limits
# PAM 是否启用
grep pam_limits /etc/pam.d/*
# systemd 服务限制
systemctl show <service> | grep Limit
Debian 下 ulimit 不生效,90% 是因为 systemd 或 PAM 没配置对,而不是 ulimit 命令本身的问题。
如果你愿意,可以告诉我:
我可以直接给你一份可复制的配置。