在 CentOS 中,文件数限制(open files / file descriptors)通常是通过 ulimit 来查看和设置的,分为临时生效和永久生效两种方式。
ulimit -n
查看某用户的限制(如 mysql):
su - mysql -c 'ulimit -n'
查看系统级限制:
cat /proc/sys/fs/file-max
ulimit -n 65535
⚠️ 只对当前 shell 及子进程有效,重启或重新登录后失效。
/etc/security/limits.confvi /etc/security/limits.conf
添加以下内容(示例):
* soft nofile 65535
* hard nofile 65535
或针对指定用户:
nginx soft nofile 65535
nginx hard nofile 65535
说明:
soft:软限制(可临时超过,但会警告)hard:硬限制(不能超过)nofile:打开文件数检查:
grep pam_limits /etc/pam.d/login
应有:
session required pam_limits.so
如果是 systemd 管理的服务(如 nginx、mysql),limits.conf 可能不生效,需要单独配置:
systemctl edit nginx
添加:
[Service]
LimitNOFILE=65535
然后:
systemctl daemon-reload
systemctl restart nginx
vi /etc/systemd/system.conf
DefaultLimitNOFILE=65535
systemctl daemon-reexec
# 查看
cat /proc/sys/fs/file-max
# 临时修改
sysctl -w fs.file-max=1000000
# 永久修改
vi /etc/sysctl.conf
fs.file-max = 1000000
sysctl -p
# 当前进程
cat /proc/<pid>/limits | grep "Max open files"
# 服务
systemctl show nginx | grep LimitNOFILE
如果你有 具体场景(如 Nginx、MySQL、Docker、高并发服务),可以告诉我,我可以给你更精确的配置方案。