温馨提示×

debian ulimit如何检查配置

小樊
34
2026-08-22 12:36:35
栏目: 智能运维

在 Debian(以及大多数 Linux 发行版)里,ulimit 用来查看或设置 shell/进程资源限制。下面按常见检查方式给你一个完整说明。


一、查看当前 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
pipe size            (512 bytes, -p) 8
stack size              (kbytes, -s) 8192
...

✅ 这是当前 shell 会话生效的限制。


2️⃣ 查看单个资源限制

例如:

ulimit -n   # 打开文件数
ulimit -u   # 最大进程数
ulimit -c   # core dump 大小
ulimit -s   # 栈大小

3️⃣ 查看“硬限制”和“软限制”

ulimit -Sn   # 软限制
ulimit -Hn   # 硬限制
  • 软限制(soft):进程当前实际生效的限制
  • 硬限制(hard):软限制的上限(只有 root 能提高)

二、ulimit 配置从哪里来?

ulimit 不是系统全局配置,而是由以下顺序决定:

1️⃣ Shell 启动配置

/etc/profile
/etc/bash.bashrc
~/.bashrc
~/.profile

如果你在这里设置了 ulimit,只对该 shell 生效


2️⃣ PAM + limits.conf(最重要 ⭐)

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
    

3️⃣ systemd 服务(非常重要,很多人会忽略)

如果程序是通过 systemd 运行的,比如:

systemctl start nginx

那么 ulimit 配置不会生效,要使用:

systemctl show nginx | grep Limit

或:

cat /lib/systemd/system/nginx.service

systemd 设置方式:

[Service]
LimitNOFILE=65535
LimitNPROC=65535

然后:

systemctl daemon-reload
systemctl restart nginx

三、查看某个运行中进程的限制

1️⃣ 通过 /proc

cat /proc/<PID>/limits

示例:

cat /proc/1234/limits

✅ 这是最真实、最准确的方式


2️⃣ 查看某个服务的 PID

ps aux | grep nginx

四、临时修改 ulimit(仅当前 shell)

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 专用最佳实践配置

0