在 Debian 里“文件数限制(open files / nofile)”通常分 临时生效 和 永久生效 两种改法,下面按常见场景说明。
ulimit -n
ulimit -n 65535
⚠️ 只在当前终端有效,重启或新登录失效
/etc/security/limits.conf(最常用)编辑文件:
sudo vim /etc/security/limits.conf
在末尾添加(示例):
* soft nofile 65535
* hard nofile 65535
或指定用户:
www-data soft nofile 65535
www-data hard nofile 65535
参数说明:
soft:软限制(可超过,但会告警)hard:硬限制(不能超过)nofile:最大打开文件数✅ 生效条件:
pam_limits.so确认:
grep pam_limits /etc/pam.d/common-session
应有:
session required pam_limits.so
如果是 systemd 管理的服务(如 nginx、mysql、docker),limits.conf 不会生效。
sudo systemctl edit nginx
写入:
[Service]
LimitNOFILE=65535
然后:
sudo systemctl daemon-reload
sudo systemctl restart nginx
sudo vim /etc/systemd/system.conf
DefaultLimitNOFILE=65535
sudo systemctl daemon-reexec
ulimit -n
cat /proc/<PID>/limits | grep "Max open files"
查看系统级最大文件数:
cat /proc/sys/fs/file-max
临时改:
sudo sysctl -w fs.file-max=1000000
永久:
echo "fs.file-max=1000000" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
❌ limits.conf 对 systemd 服务无效
❌ 改完不重新登录 / 不重启服务
❌ Docker 容器需额外在 docker 配置中改
如果你能说下是 普通用户 / 某个服务 / Docker / 服务器高并发场景,我可以给你更精准的配置。