在 Debian 系统中,ulimit 设置失败(不生效或提示 operation not permitted)通常不是单一原因,而是权限机制 + 继承规则 + systemd/服务管理器限制共同作用的结果。下面按最常见 → 较少见逐一说明。
ulimit -n 65536
如果当前 hard limit 是 1024,而你不是 root,就会失败。
✅ 查看当前限制:
ulimit -n
ulimit -Hn
ulimit -Sn
✅ 正确顺序:
ulimit -Hn 65536
ulimit -Sn 65536
⚠️ 普通用户不能提高 hard limit
ulimit 只对当前 shell 进程及其子进程有效
bash -c "ulimit -n 65536"
退出后立刻失效。
✅ 正确方式:
ulimit -n 65536
./your_program
sudo 但 ulimit 没有继承sudo ulimit -n 65536
❌ 这是错的,ulimit 是 shell 内建命令,sudo 无法传递。
✅ 正确方式:
sudo bash -c "ulimit -n 65536 && ./app"
在 Debian 9+ 中,systemd 忽略 /etc/security/limits.conf
✅ 服务必须这样设置:
# /etc/systemd/system/xxx.service
[Service]
LimitNOFILE=65536
然后:
systemctl daemon-reexec
systemctl restart xxx
/etc/security/limits.conf 不生效原因:
✅ 正确写法:
* soft nofile 65536
* hard nofile 65536
⚠️ 注意:
* 不包括 rootroot soft nofile 65536
root hard nofile 65536
检查:
grep UsePAM /etc/ssh/sshd_config
✅ 应为:
UsePAM yes
然后:
systemctl restart sshd
cat /proc/sys/fs/nr_open
如果你设置:
ulimit -n 2000000
但 nr_open = 1048576 → 失败
✅ 提高内核限制(需 root):
sysctl -w fs.nr_open=2097152
永久生效:
# /etc/sysctl.conf
fs.nr_open=2097152
容器里 ulimit 由宿主机控制
✅ Docker 示例:
docker run --ulimit nofile=65536:65536
K8s:
securityContext:
limits:
nofile: 65536
# 1. 当前限制
ulimit -a
# 2. hard limit
ulimit -Hn
# 3. 是否 systemd 服务
systemctl status 你的服务
# 4. 内核限制
cat /proc/sys/fs/nr_open
# 5. 是否 PAM 登录
echo $SHELL
Debian 上 ulimit 设置失败,99% 是因为:
- 普通用户改 hard limit
- systemd 覆盖 limits.conf
- 在脚本 / sudo / 子 shell 中设置
- 没重启服务或会话
如果你愿意,可以把 **具体报错 + 使用场景(SSH / systemd / Docker / 脚本)**贴出来,我可以直接帮你定位是哪一条。