温馨提示×

怎样重置debian ulimit

小樊
52
2026-10-18 15:18:11
栏目: 智能运维

在 Debian 系统中,ulimit 是用来限制用户进程资源使用的(如打开文件数、进程数等)。要“重置”或“修改” ulimit,需要分 当前会话、用户级、系统级 三种情况来看。


一、查看当前 ulimit

ulimit -a

常见参数:

  • -n:打开文件数(file descriptors)
  • -u:最大进程数
  • -s:栈大小

二、仅当前会话重置(临时)

直接执行:

ulimit -n 65535
ulimit -u 4096

⚠️ 只在当前 shell 及其子进程有效,重启或新登录失效。


三、针对用户永久修改(推荐)

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

sudo nano /etc/security/limits.conf

示例(用户 www):

www soft nofile 65535
www hard nofile 65535
www soft nproc 4096
www hard nproc 4096

参数说明:

  • soft:软限制(可临时超过)
  • hard:硬限制(不可超过)
  • nofile:文件描述符
  • nproc:进程数

2. 确保 PAM 启用 limits

检查:

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

应有:

session required pam_limits.so

若没有,可添加。

3. 重新登录用户生效

su - www
ulimit -n

四、系统级 / systemd 服务重置

1. systemd 服务的 ulimit

Debian 使用 systemd 时,limits.conf 对服务不生效。

编辑服务:

sudo systemctl edit nginx

添加:

[Service]
LimitNOFILE=65535
LimitNPROC=4096

重载:

sudo systemctl daemon-reload
sudo systemctl restart nginx

查看:

cat /proc/$(pgrep nginx | head -1)/limits

五、系统全局内核参数(可选)

sysctl -w fs.file-max=1000000

永久:

echo "fs.file-max=1000000" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

六、常见“重置失败”原因

  1. 修改了 limits.conf 但用的是 systemd 服务
  2. 用户未重新登录
  3. SSH 未加载 PAM
  4. 硬限制低于软限制

如果你能说明:

  • 是 普通用户 / root / 服务
  • 想重置 哪种资源(nofile / nproc 等)
  • 是否是 Docker / systemd / 桌面环境

我可以给你更精确的做法。

0 踩