温馨提示×

debian ulimit文件数怎么改

小樊
38
2026-09-25 05:54:41
栏目: 智能运维

在 Debian 里“文件数限制(open files / nofile)”通常分 临时生效 和 永久生效 两种改法,下面按常见场景说明。


一、临时修改(当前会话 / 当前进程)

1. 查看当前限制

ulimit -n

2. 临时修改(仅当前 shell)

ulimit -n 65535

⚠️ 只在当前终端有效,重启或新登录失效


二、永久修改(推荐)

方式 1:修改 /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

方式 2:systemd 服务(重要!)

如果是 systemd 管理的服务(如 nginx、mysql、docker),limits.conf 不会生效。

针对某个服务

sudo systemctl edit nginx

写入:

[Service]
LimitNOFILE=65535

然后:

sudo systemctl daemon-reload
sudo systemctl restart nginx

全局 systemd 限制

sudo vim /etc/systemd/system.conf
DefaultLimitNOFILE=65535
sudo systemctl daemon-reexec

三、验证是否生效

当前 shell

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 / 服务器高并发场景,我可以给你更精准的配置。

0 踩