温馨提示×

debian ulimit怎么调大

小樊
47
2026-09-09 23:18:05
栏目: 智能运维

在 Debian 里调大 ulimit(资源限制,比如打开文件数、进程数等),通常分临时永久两种方式,下面按常见场景说明。


一、临时调整(当前 shell / 当前会话)

直接在终端执行:

ulimit -n 65535      # 打开文件数
ulimit -u 65535      # 最大进程数
ulimit -s unlimited  # 栈大小

查看当前限制:

ulimit -a

⚠️ 只在当前终端和该终端启动的进程中有效,重启或新开 shell 失效。


二、永久调整(推荐)

1️⃣ 修改 /etc/security/limits.conf

编辑文件:

sudo vim /etc/security/limits.conf

示例(针对所有用户):

* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

或指定用户:

www-data soft nofile 100000
www-data hard nofile 100000

字段说明:

  • soft:软限制(可临时超过,但会报警)
  • hard:硬限制(不能超过)
  • nofile:打开文件数
  • nproc:进程数

✅ 需要 重新登录 或重启才能生效。


2️⃣ 确保 PAM 生效(很重要)

Debian 默认需要 PAM 支持:

grep pam_limits /etc/pam.d/common-session

如果没有,添加:

session required pam_limits.so

同样检查:

/etc/pam.d/common-session-noninteractive

3️⃣ systemd 服务(重点!)

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

方法 A:服务级配置

sudo systemctl edit nginx

写入:

[Service]
LimitNOFILE=65535
LimitNPROC=65535

然后:

sudo systemctl daemon-reload
sudo systemctl restart nginx

方法 B:全局 systemd 限制

sudo vim /etc/systemd/system.conf
DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535

重启系统生效。


三、内核级限制(很少需要)

查看系统最大文件数:

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

四、常见坑总结

ulimit 改了但服务没生效
✅ systemd 服务必须单独配

❌ 非 root 用户改 hard limit 失败
✅ 只有 root 能提 hard limit

❌ Docker 容器限制没变
✅ 需要在宿主机或 docker 启动参数里改


如果你能说一下具体场景(比如:Nginx、MySQL、Docker、普通用户、SSH 登录),我可以给你更精确的配置。

0