温馨提示×

debian ulimit在哪配置

小樊
40
2026-10-12 18:40:35
栏目: 智能运维

在 Debian 系统中,ulimit(用户资源限制)可以在多个地方配置,具体取决于你是想临时生效、用户级生效还是系统/服务级生效。下面按常见场景说明。


一、临时生效(当前 shell)

ulimit -n 65535

只对当前 shell 及其子进程有效,重启或新开终端失效。

查看当前限制:

ulimit -a

二、用户级配置(推荐)

1. /etc/security/limits.conf

这是最常用的配置位置。

示例:

*               soft    nofile          65535
*               hard    nofile          65535
www-data        soft    nproc           4096
www-data        hard    nproc           4096

字段说明:

<用户/组>  <soft/hard>  <资源类型>  <值>

常见资源类型:

  • nofile:打开文件数
  • nproc:进程数
  • stack:栈大小
  • memlock:锁内存

⚠️ 注意:

  • 修改后需要重新登录才生效
  • 对 systemd 服务不生效

2. /etc/security/limits.d/*.conf

Debian 推荐把配置放这里,便于管理:

/etc/security/limits.d/99-custom.conf

内容和 limits.conf 一样,优先级更高。


三、PAM 是否启用(很重要)

确保登录时会加载 limits:

检查:

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

应有类似:

session required pam_limits.so

如果没有,手动加上。


四、systemd 服务(重点)

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

方法 1:服务级配置

systemctl edit nginx

添加:

[Service]
LimitNOFILE=65535
LimitNPROC=4096

然后:

systemctl daemon-reload
systemctl restart nginx

查看:

systemctl show nginx | grep Limit

方法 2:全局 systemd 限制

编辑:

/etc/systemd/system.conf

例如:

DefaultLimitNOFILE=65535

然后重启系统或:

systemctl daemon-reexec

五、Docker 容器

ulimit 需要在宿主机或 docker 启动参数中设置:

docker run --ulimit nofile=65535:65535 ...

或在 docker-compose.yml:

ulimits:
  nofile:
    soft: 65535
    hard: 65535

六、快速排查顺序(实用)

  1. 是否用 systemd 服务?→ 用 systemctl
  2. 是否普通用户?→ 用 limits.conf
  3. 是否临时测试?→ ulimit 命令
  4. 修改后是否重新登录 / 重启服务?

如果你能告诉我:

  • 是 哪个服务(nginx / mysql / 自定义程序)
  • 是 用户登录 还是 后台服务
  • Debian 版本

我可以给你精确到文件的配置示例。

0 踩