在 Debian(以及大多数 Linux 发行版)中,ulimit 的修改生效方式取决于你修改的位置和场景。下面按常见情况说明。
如果你只是在当前终端里修改:
ulimit -n 65535
ulimit -u 4096
✅ 立即生效
❌ 只对该 shell 及其子进程有效
❌ 关闭终端或重新登录后失效
查看当前限制:
ulimit -a
/etc/security/limits.conf编辑:
sudo vim /etc/security/limits.conf
添加或修改:
* soft nofile 65535
* hard nofile 65535
* soft nproc 4096
* hard nproc 4096
说明:
soft:当前生效值hard:上限nofile:打开文件数nproc:进程数⚠️ 注意:
* 对所有用户生效root 可能不生效,需单独写:root soft nofile 65535
root hard nofile 65535
检查:
grep pam_limits /etc/pam.d/common-session
如果没有,添加:
session required pam_limits.so
修改后 必须重新登录:
logout
# 或
ssh user@host
⚠️ 重启服务 / 重启系统不是必须,但重新登录是必须的
如果你发现:
修改了 limits.conf,但服务(如 Nginx、MySQL)不生效
原因是:systemd 不读取 limits.conf
sudo systemctl edit nginx
写入:
[Service]
LimitNOFILE=65535
LimitNPROC=4096
然后:
sudo systemctl daemon-reload
sudo systemctl restart nginx
编辑:
sudo vim /etc/systemd/system.conf
DefaultLimitNOFILE=65535
DefaultLimitNPROC=4096
然后重启系统。
Docker 默认限制较低,需显式指定:
docker run --ulimit nofile=65535:65535 --ulimit nproc=4096:4096
或在 docker-compose.yml:
ulimits:
nofile:
soft: 65535
hard: 65535
ulimit -n
ulimit -u
cat /proc/$(pgrep nginx | head -1)/limits
| 问题 | 原因 |
|---|---|
| limits.conf 改了不生效 | 没重新登录 |
| 服务不生效 | systemd 忽略 limits.conf |
| root 不生效 | 需单独写 root |
| Docker 不生效 | Docker 默认 ulimit |
如果你愿意,可以告诉我:
nofile 还是 nproc我可以给你精确到命令级别的配置方案。