温馨提示×

怎样检查debian ulimit

小樊
39
2026-09-12 20:59:57
栏目: 智能运维

在 Debian 系统中,可以通过多种方式来查看和检查 ulimit(资源限制)。下面按常见使用场景说明。


一、查看当前 shell 的 ulimit

1. 查看所有资源限制

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) 1024
virtual memory          (kbytes, -v) unlimited

2. 查看单项限制

ulimit -n   # 打开文件数
ulimit -u   # 最大用户进程数
ulimit -s   # 栈大小

注意:ulimit 默认显示的是当前 shell 进程的限制。


二、查看某个进程的实际 ulimit

方法 1:通过 /proc

cat /proc/<PID>/limits

例如:

cat /proc/1234/limits

可以看到该进程生效的:

  • Max open files
  • Max processes
  • Max memory 等

方法 2:使用 prlimit

prlimit --pid <PID>

或查看某一项:

prlimit --nofile --pid <PID>

三、查看系统级限制(不是 ulimit)

1. 系统最大打开文件数

cat /proc/sys/fs/file-max

2. 当前已用文件句柄

cat /proc/sys/fs/file-nr

四、检查 Debian 中 ulimit 的配置来源

1. /etc/security/limits.conf

cat /etc/security/limits.conf

示例:

* soft nofile 65535
* hard nofile 65535

2. /etc/security/limits.d/

ls /etc/security/limits.d/
cat /etc/security/limits.d/*.conf

3. systemd 服务限制

如果是 systemd 管理的服务:

systemctl show <service> | grep Limit

例如:

systemctl show nginx | grep LimitNOFILE

或修改:

[Service]
LimitNOFILE=65535

五、临时修改 ulimit(仅当前会话)

ulimit -n 65535

重启或新 shell 后失效。


六、常见检查清单(排错用)

  • [ ] ulimit -a 当前 shell
  • [ ] /proc/<PID>/limits 实际进程
  • [ ] /etc/security/limits.conf
  • [ ] systemd 服务 Limit 配置
  • [ ] PAM 是否启用 pam_limits.so
grep pam_limits /etc/pam.d/common-session

如果你愿意,可以告诉我:

  • 普通用户 / root
  • shell / 服务 / Docker / 容器
  • 想检查 nofile / nproc / 内存 哪一项

我可以给你更精确的命令。

0