在 Debian(以及大多数 Linux 发行版)里,ulimit 受限是设计使然,并不是“Bug”,而是为了系统稳定性、安全和多用户隔离。下面分层说明「为什么会受限」以及「受限在哪里」。
ulimit 是 shell 级别 的资源限制,用来限制:
⚠️ 注意:
ulimit 只对当前 shell 及其子进程生效/proc 或 systemd 的活)Debian 沿用了 UNIX 的“多用户服务器”理念:
常见默认值:
ulimit -n # 1024(很常见)
ulimit -u # 普通用户也有限制
Debian 使用 PAM:
/etc/security/limits.conf
/etc/security/limits.d/*.conf
示例:
* soft nofile 1024
* hard nofile 4096
如果你没改这里:
ulimit -n 65535 会报错:bash: ulimit: open files: cannot modify limit: Operation not permitted
这是 最容易踩坑的地方 ⚠️
如果你在:
/etc/profile
~/.bashrc
里改了 ulimit,对 systemd 服务完全无效
systemd 服务限制由:
/etc/systemd/system.conf
/etc/systemd/user.conf
service unit 文件
控制,例如:
[Service]
LimitNOFILE=65535
否则你 systemctl restart nginx 后:
cat /proc/<pid>/limits
会发现 nofile 还是 1024
在:
ulimit 经常被:
即使你容器内 ulimit -n 100000,也可能被宿主直接拒绝。
# /etc/security/limits.conf
www-data soft nofile 65535
www-data hard nofile 65535
重新登录后:
ulimit -n
[Service]
LimitNOFILE=65535
LimitNPROC=65535
然后:
systemctl daemon-reexec
systemctl restart xxx
ulimit -n 65535
Debian 的 ulimit 受限,是为了防止资源滥用; 它不是“不能用大”,而是“不能随便用大”。
如果你愿意,可以告诉我:
我可以直接给你一份「最小可用配置」。