在 Debian 系统中,ulimit 修改失败通常是因为 systemd、PAM、Shell 类型、配置位置 等因素共同作用导致的。下面按最常见原因 → 排查与解决方法来说明。
| 原因 | 说明 |
|---|---|
使用 systemd 管理服务 |
普通 ulimit 对 systemd 服务无效 |
| 修改了错误的配置文件 | /etc/security/limits.conf 不是万能的 |
| 非登录 shell | PAM limits 不会生效 |
用 su / sudo |
可能绕过 PAM |
| 权限不足 | 硬限制(hard limit)不能随便改 |
| 内核参数限制 | 如 fs.nr_open |
/etc/security/limits.conf 没生效* soft nofile 65535
* hard nofile 65535
systemd 启动的服务su、sudo 默认可能不加载 PAM✅ 确认 PAM 启用
grep pam_limits /etc/pam.d/common-session
应有类似:
session required pam_limits.so
✅ 使用登录 shell
bash -l
ulimit -n
ulimit -n 65535
对 systemd 服务 完全无效
编辑 service 文件:
[Service]
LimitNOFILE=65535
然后:
systemctl daemon-reexec
systemctl restart your_service
✅ 查看是否生效:
cat /proc/$(pidof your_service)/limits
su 默认 不重新加载 PAM limitssudo 可能使用 secure_path 或不同 PAM 配置✅ 使用:
su - username
或:
sudo -i
operation not permittedbash: ulimit: open files: cannot modify limit: Operation not permitted
sudo bash
ulimit -Hn 65535
ulimit -Sn 65535
/etc/security/limits.conf 在容器中几乎无效docker run --ulimit nofile=65535:65535
或 systemd 服务中设置 LimitNOFILE
# /etc/security/limits.conf
username soft nofile 65535
username hard nofile 65535
并确保:
grep pam_limits /etc/pam.d/common-session
[Service]
LimitNOFILE=65535
LimitNPROC=65535
ulimit -n 65535
⚠️ 仅对当前 shell 有效,重启失效
/proc/PID/limits?cat /proc/$$/limits | grep "open files"
如果你愿意,可以把 具体场景 发我,比如:
我可以直接给你精确可用的配置方案。