温馨提示×

debian ulimit系统级在哪改

小樊
38
2026-09-25 06:02:42
栏目: 智能运维

在 Debian 里,系统级(全局)ulimit 并不是在某一行 ulimit 命令里改,而是通过 PAM + systemd/limits 配置 来做的。常见位置和方式如下:


一、最常用:/etc/security/limits.conf(系统级软硬限制)

这是 最标准的系统级配置方式。

/etc/security/limits.conf

示例(对所有用户生效):

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

说明:

  • * 表示所有用户(也可写用户名、组 @group)
  • soft:软限制(用户可临时超过,但会警告)
  • hard:硬限制(不能超过)
  • nofile:打开文件数
  • nproc:进程数

⚠️ 注意:

  • 该配置 只对通过 PAM 登录的会话生效
  • 对 systemd 服务不生效

二、Debian 10+(使用 systemd):/etc/systemd/system.conf

如果是 系统服务 / 守护进程,limits.conf 不生效,需要改 systemd:

/etc/systemd/system.conf

或:

/etc/systemd/user.conf

示例:

DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535

修改后必须重启:

systemctl daemon-reexec
# 或重启系统

三、单个 systemd 服务单独限制

如果你只想给某个服务改:

/etc/systemd/system/xxx.service
[Service]
LimitNOFILE=65535
LimitNPROC=65535

然后:

systemctl daemon-reload
systemctl restart xxx

四、确认是否生效

ulimit -a
cat /proc/<pid>/limits

五、容易踩的坑(很重要)

  1. limits.conf 不影响 root 启动的 systemd 服务
  2. Docker / Kubernetes 容器要单独在容器层面设
  3. SSH 登录才走 PAM,cron / 服务不走

如果你能说清楚:

  • 是 用户登录 还是 系统服务
  • 想改的是 nofile / nproc / 内存 / 栈

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

0 踩