在 Debian 里 ulimit “不生效”是非常常见的问题,通常不是命令本身的问题,而是作用范围/配置位置不对。下面按「最常见原因 → 解决办法」给你一套排查清单。
ulimit 只对“当前 shell 及其子进程”有效
下面这些都不会生效:
ulimit -n 65535
✅ 解决:写进 shell 配置(仅交互 shell)
# 对当前用户
echo "ulimit -n 65535" >> ~/.bashrc
⚠️ 注意:
这是 90% 的人踩坑点
/etc/profile
/etc/bash.bashrc
❌ 对 systemd 启动的服务(nginx / mysql / redis)完全无效
✅ 正确方式(见第 5 点)
你做了:
* soft nofile 65535
* hard nofile 65535
但还是不生效?
✅ 必须满足 3 个条件:
grep pam_limits /etc/pam.d/common-session
grep pam_limits /etc/pam.d/login
没有就加:
session required pam_limits.so
logout
验证:
ulimit -n
比如:
systemctl restart nginx
✅ 必须改 service 文件
systemctl edit nginx
写入:
[Service]
LimitNOFILE=65535
LimitNPROC=65535
然后:
systemctl daemon-reload
systemctl restart nginx
验证:
cat /proc/$(pgrep nginx | head -1)/limits
即使你改了 service,也可能被系统限制。
查看:
systemctl show --property=DefaultLimitNOFILE
修改:
/etc/systemd/system.conf
DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535
然后:
systemctl daemon-reexec
Docker 默认继承宿主,但可覆盖
docker run --ulimit nofile=65535:65535 ...
或 compose:
ulimits:
nofile:
soft: 65535
hard: 65535
# 当前 shell
ulimit -a
# 某个进程
cat /proc/<pid>/limits
# systemd 服务
systemctl show <service> | grep Limit
| 场景 | 正确方式 |
|---|---|
| 当前用户 | ~/.bashrc |
| 登录用户 | /etc/security/limits.conf |
| systemd 服务 | systemctl edit |
| 全局 | systemd/system.conf |
| Docker | --ulimit |
如果你愿意,可以告诉我:
我可以直接给你可复制的配置。