在 CentOS(以及大多数 Linux 发行版)中,ulimit 修改“失败”是非常常见的问题,通常不是命令本身的问题,而是作用范围、配置位置或系统限制导致的。下面按常见原因逐一说明。
ulimit -n 65535
✅ 只影响:
❌ 不影响:
验证:
ulimit -n
如果在新终端里还是旧值,这是“正常现象”。
很多人会写:
echo "ulimit -n 65535" >> /etc/profile
但以下情况会失败:
✅ 解决:
source /etc/profileCentOS 7/8 使用 systemd,ulimit 对服务基本无效。
例如 nginx / mysql / java 服务:
systemctl start nginx
即使你设了:
ulimit -n 65535
nginx 实际仍是默认值(通常 1024)
编辑 service 文件:
vim /etc/systemd/system/nginx.service
或:
systemctl edit nginx
添加:
[Service]
LimitNOFILE=65535
然后:
systemctl daemon-reload
systemctl restart nginx
验证:
cat /proc/$(pidof nginx)/limits
* soft nofile 65535
* hard nofile 65535
确认:
grep pam_limits /etc/pam.d/login
grep pam_limits /etc/pam.d/sshd
应有:
session required pam_limits.so
ulimit -Hn # 硬限制
ulimit -Sn # 软限制
如果:
ulimit -Hn 1024
ulimit -n 65535 # 失败
❌ 不能超过 hard limit
某些资源受内核限制:
sysctl fs.file-max
如果:
fs.file-max = 100000
你设 200000 的 nofile 也没意义。
在容器中:
ulimit 受宿主机限制docker run --ulimit nofile=65535:65535
# 当前 shell
ulimit -a
# 某进程实际限制
cat /proc/<pid>/limits
# systemd 服务
systemctl show <service> | grep Limit
# 配置文件
grep -R "nofile" /etc/security/limits.conf /etc/systemd
CentOS 上 ulimit“改了没用”,90% 是因为:
你改的是 shell,但进程是 systemd 启动的。
如果你愿意,可以告诉我:
我可以直接给你对应可用的配置方案。