温馨提示×

centos ulimit生效了吗

小樊
40
2026-10-17 17:18:35
栏目: 智能运维

要判断 CentOS 上 ulimit 是否生效,可以从下面几个方面来确认。


一、当前会话是否生效(临时设置)

1. 查看当前用户的 ulimit

ulimit -a

或单独看某一项,例如:

ulimit -n   # 文件描述符
ulimit -u   # 最大进程数

✅ 如果这里显示的值和你设置的一致,说明 当前 shell 已生效。

⚠️ 注意:ulimit 只对 当前 shell 及其子进程 生效。


二、永久配置是否生效(系统级)

2. 检查 /etc/security/limits.conf

grep -v '^#' /etc/security/limits.conf

示例:

* soft nofile 65535
* hard nofile 65535

3. 检查 /etc/security/limits.d/

CentOS 会优先读取:

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

三、验证某个进程是否真正生效(最关键)

4. 查看运行中进程的 ulimit

cat /proc/<PID>/limits

例如 nginx:

ps aux | grep nginx
cat /proc/1234/limits

✅ 如果这里的值正确,说明 对进程真正生效了。


四、常见“不生效”的原因(重点)

❌ 1. 修改后没重新登录

exit
ssh user@host

❌ 2. systemd 服务不读 limits.conf

CentOS 7+ 的 systemd 服务需要:

[Service]
LimitNOFILE=65535

然后:

systemctl daemon-reload
systemctl restart xxx

❌ 3. 使用 su 而不是 login

su - user   # 正确
su user     # 可能不加载 limits

五、快速自检清单 ✅

  • [ ] ulimit -a 显示正确
  • [ ] /etc/security/limits.conf 配置正确
  • [ ] 重新登录或重启服务
  • [ ] /proc/PID/limits 验证通过

如果你愿意,可以把:

  • CentOS 版本
  • 你改的是哪一项(nofile / nproc / core)
  • 是普通用户还是 systemd 服务

发出来,我可以帮你精确判断 为什么没生效。

0 踩