在 Debian(以及大多数 Linux 发行版)里,ulimit 用来查看或设置 shell/进程资源限制。下面按常见检查方式给你一个完整说明。
ulimit -a
示例输出:
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
stack size (kbytes, -s) 8192
...
✅ 这是当前 shell 会话生效的限制。
例如:
ulimit -n # 打开文件数
ulimit -u # 最大进程数
ulimit -c # core dump 大小
ulimit -s # 栈大小
ulimit -Sn # 软限制
ulimit -Hn # 硬限制
ulimit 不是系统全局配置,而是由以下顺序决定:
/etc/profile
/etc/bash.bashrc
~/.bashrc
~/.profile
如果你在这里设置了 ulimit,只对该 shell 生效。
Debian 使用 PAM,绝大多数服务/登录限制来自:
/etc/security/limits.conf
/etc/security/limits.d/*.conf
✅ 这是最推荐的检查方式
cat /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
⚠️ 注意:
session required pam_limits.so
在:/etc/pam.d/common-session
/etc/pam.d/common-session-noninteractive
如果程序是通过 systemd 运行的,比如:
systemctl start nginx
那么 ulimit 配置不会生效,要使用:
systemctl show nginx | grep Limit
或:
cat /lib/systemd/system/nginx.service
[Service]
LimitNOFILE=65535
LimitNPROC=65535
然后:
systemctl daemon-reload
systemctl restart nginx
/proccat /proc/<PID>/limits
示例:
cat /proc/1234/limits
✅ 这是最真实、最准确的方式
ps aux | grep nginx
ulimit -n 65535
⚠️ 退出 shell 后失效
| 问题 | 检查点 |
|---|---|
| 修改 ulimit 不生效 | 是否通过 systemd? |
| 普通用户限制低 | /etc/security/limits.conf |
| 服务不生效 | systemctl show xxx |
| 登录 shell 生效,cron 不生效 | PAM / systemd |
ulimit -a
ulimit -n
ulimit -Sn
ulimit -Hn
cat /etc/security/limits.conf
systemctl show <service> | grep Limit
cat /proc/$(pgrep nginx)/limits
如果你是 某个具体场景(比如 Docker / Nginx / MySQL / Java / 高并发服务),可以告诉我,我可以给你一份 Debian 专用最佳实践配置。