在 Debian(以及大多数 Linux 发行版)里,改了 ulimit 却不生效,通常不是“命令写错了”,而是改错地方 / 改的层级不对。下面按最常见原因给你梳理。
ulimit 是 shell 内建命令,不是系统全局配置。
ulimit -n 65535
✅ 只在:
中有效。
❌ 对以下情况完全无效:
ulimit -n 100000
退出终端再连,失效。
写进用户 shell 配置(注意 shell 类型):
# ~/.bashrc 或 ~/.profile
ulimit -n 65535
# ~/.bash_profile
ulimit -n 65535
⚠️ 注意:
.bashrc/etc/security/limits.conf
示例:
* soft nofile 65535
* hard nofile 65535
或针对用户:
www-data soft nofile 65535
www-data hard nofile 65535
/etc/pam.d/common-session
/etc/pam.d/common-session-noninteractive
需要有:session required pam_limits.so
这是 Debian 10+ 最常见“改了无效”的原因。
比如 nginx / mysql / java 服务:
systemctl restart nginx
# /etc/systemd/system/nginx.service.d/limits.conf
[Service]
LimitNOFILE=65535
然后:
systemctl daemon-reload
systemctl restart nginx
查看是否生效:
cat /proc/<pid>/limits
容器里:
docker run --ulimit nofile=65535:65535 ...
或 docker-compose:
ulimits:
nofile:
soft: 65535
hard: 65535
sysctl fs.file-max
太小就改:
# /etc/sysctl.conf
fs.file-max = 1000000
sysctl -p
| 场景 | 正确方式 |
|---|---|
| 当前 shell | ulimit |
| 登录用户 | limits.conf |
| systemd 服务 | service.d/*.conf |
| Docker | --ulimit |
| 已运行进程 | 无法改,只能重启 |
我可以直接告诉你“哪里错了,怎么改一行生效”。