温馨提示×

debian ulimit如何调大

小樊
52
2026-09-01 00:39:06
栏目: 智能运维

在 Debian 里 ulimit 通常是 shell 级别的限制,要“调大”一般分三种场景。下面按最常见 → 最彻底来说明。


一、当前 shell 临时调大(立刻生效,重启失效)

ulimit -n 65535        # 打开文件数
ulimit -u 65535        # 最大进程/线程数
ulimit -s 8192         # 栈大小(KB)
ulimit -a              # 查看当前所有限制

⚠️ 只能调小或调到不超过系统硬限制 如果报错:

ulimit: open files: cannot modify limit: Operation not permitted

说明超过 hard limit,需要下面方法。


二、用户级永久生效(推荐)

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

sudo vim /etc/security/limits.conf

示例(对 all 用户或指定用户):

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

说明:

  • soft:当前限制
  • hard:最大可设值
  • nofile:文件描述符
  • nproc:进程数

重新登录或重启后生效


2️⃣ 确保 PAM 生效(Debian 默认有)

检查:

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

应有:

session required pam_limits.so

三、systemd 服务调大(非常重要!)

Debian 10+ 默认用 systemd,limits.conf 对服务可能无效

方法 1:单个服务

sudo systemctl edit nginx

写入:

[Service]
LimitNOFILE=65535
LimitNPROC=65535

然后:

sudo systemctl daemon-reload
sudo systemctl restart nginx

方法 2:全局 systemd 限制

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

四、内核级硬上限(一般不用改)

查看:

cat /proc/sys/fs/file-max

临时:

sudo sysctl -w fs.file-max=1000000

永久:

sudo vim /etc/sysctl.conf
fs.file-max = 1000000
sudo sysctl -p

五、快速排查清单 ✅

ulimit -a
cat /proc/<pid>/limits
systemctl show <service> | grep Limit

六、常见坑 ⚠️

  • ssh 登录才生效(非 systemd 启动)
  • Docker 容器要单独设 --ulimit
  • root 用户也可能被 limits.conf 限制
  • su 不会重新加载 limits

如果你能告诉我:

  • Debian 版本
  • 普通用户 / 服务 / Docker
  • 想调 nofile / nproc / 内存

我可以给你精确配置

0