在 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
max user processes (-u) 256
...
| 资源 | 查看命令 |
|---|---|
| 最大打开文件数 | ulimit -n |
| 最大进程数 | ulimit -u |
| core dump 大小 | ulimit -c |
| 栈大小 | ulimit -s |
| 虚拟内存 | ulimit -v |
示例:
ulimit -n
ulimit -S -n
ulimit -H -n
/proccat /proc/<PID>/limits
示例:
cat /proc/1234/limits
输出非常详细,包括:
✅ 这是最准确的方法
cat /proc/sys/fs/file-max
cat /proc/sys/fs/file-nr
ulimit -n 65535
⚠️ 重启或新 shell 失效
编辑:
/etc/security/limits.conf
示例:
* soft nofile 65535
* hard nofile 65535
或指定用户:
www-data soft nproc 4096
www-data hard nproc 4096
⚠️ 需要 PAM 支持,通常 Debian 默认已启用
如果是 systemd 启动的服务,ulimit 不生效,需要:
systemctl edit <service>
添加:
[Service]
LimitNOFILE=65535
LimitNPROC=4096
然后:
systemctl restart <service>
ulimit 只影响 当前 shell需要在宿主机或 Docker 启动参数中设置:
docker run --ulimit nofile=65535:65535
| 需求 | 命令 |
|---|---|
| 查看当前 shell | ulimit -a |
| 查看进程限制 | cat /proc/PID/limits |
| 查看系统最大文件数 | cat /proc/sys/fs/file-max |
| 永久修改 | /etc/security/limits.conf |
| systemd 服务 | systemctl edit |
如果你愿意,可以告诉我:
我可以给你更精确的配置方案。