在 Debian 里 ulimit 是可以生效的,但要注意生效范围、配置位置和持久化方式,否则很容易“看起来没生效”。
ulimit 是 shell 内建命令,用来限制:
⚠️ 关键点:
ulimit 只对“当前 shell 及其子进程”生效。
ulimit -n 65535
ulimit -n
这会立即生效(仅当前会话)。
.bashrc 不一定对所有程序生效例如:
这些 不会读取你的 shell 配置。
/etc/security/limits.conf(最常用)sudo vim /etc/security/limits.conf
示例:
* soft nofile 65535
* hard nofile 65535
* soft nproc 4096
* hard nproc 4096
⚠️ 限制:
pam_limits.so)✅ 验证:
ssh localhost
ulimit -n
如果你用的是 Debian 8+(systemd):
limits.conf 对 systemd 服务无效
[Service]
LimitNOFILE=65535
LimitNPROC=4096
systemctl daemon-reload
systemctl restart xxx
sudo systemctl edit --runtime --system
或在:
/etc/systemd/system.conf
/etc/systemd/user.conf
容器内:
ulimit 由 宿主机 + docker 启动参数 控制docker run --ulimit nofile=65535:65535
| 原因 | 说明 |
|---|---|
| systemd 服务 | limits.conf 无效 |
| 非登录 shell | bashrc 不加载 |
| root 特殊限制 | 某些内核参数 |
| 内核参数限制 | fs.file-max |
| 修改后没重登 | shell 未重新初始化 |
cat /proc/$(pidof xxx)/limits
Debian 上 ulimit 是生效的,但“怎么配”比“配不配”更重要。
如果你能告诉我:
我可以直接给你最合适的一份配置。