在 Debian 上遇到 ulimit 不生效 / 失效,通常不是“系统 bug”,而是作用范围或配置位置不对。下面按最常见原因系统说明。
ulimit 是 shell 内建命令,作用是:
修改当前 shell 及其子进程的资源限制
❌ 它不能影响:
✅ 它只能:
例如:
[Service]
ExecStart=/usr/bin/myapp
你在终端执行:
ulimit -n 65535
服务根本不受影响
✅ 正确方式(systemd):
[Service]
LimitNOFILE=65535
然后:
systemctl daemon-reload
systemctl restart myapp
例如:
这些不读取 /etc/profile
✅ 解决:
--ulimitLimit*常见于:
原因:
limits.conf依赖 PAM
检查:
grep pam_limits /etc/pam.d/login
grep pam_limits /etc/pam.d/sshd
必须有:
session required pam_limits.so
否则 limits.conf 完全无效。
⚠️ 重点:
limits.conf对 root 用户不生效(除非显式指定)
错误示例:
* soft nofile 65535
✅ 正确:
root soft nofile 65535
root hard nofile 65535
例如:
ulimit -Hn
1024
你执行:
ulimit -Sn 65535
❌ 会被拒绝
✅ 必须先提高 hard limit(root):
ulimit -Hn 65535
Debian 11/12:
10241048576你设 65535,但:
用以下命令确认:
cat /proc/<pid>/limits
/etc/security/limits.conf
LimitNOFILE=65535
LimitNPROC=65535
docker run --ulimit nofile=65535:65535
ulimit -n 65535
./your_program
# 当前 shell
ulimit -a
# 某进程
cat /proc/$(pidof xxx)/limits
# PAM
grep pam_limits /etc/pam.d/*
# systemd
systemctl show xxx | grep Limit
如果你愿意,可以告诉我:
我可以直接给你精确可用的配置。