温馨提示×

ubuntu ulimit默认值多少

小樊
38
2026-10-14 11:14:11
栏目: 智能运维

在 Ubuntu(以及大多数 Linux 发行版) 中,ulimit 的“默认值”取决于 资源类型 和 限制类型(软限制 / 硬限制),并且会受到以下因素影响:

  • 系统版本(systemd / pam / kernel)
  • 是否是 交互式 shell
  • 是否通过 systemd 服务 启动
  • /etc/security/limits.conf
  • /etc/systemd/system.conf、/etc/systemd/user.conf

一、最常见默认值(交互式登录 shell)

在 Ubuntu 20.04 / 22.04 / 24.04 的普通用户登录 shell 中,常见默认值如下:

ulimit -a

典型输出示例(非 root 用户):

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 15495
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 15495
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

重点说明

资源 默认值
打开文件数 (-n) 1024(非常常见限制)
最大进程数 (-u) 由 nproc 决定(通常 ≈ 内存/线程数)
core 文件 0(不生成)
stack size 8192 KB
最大文件大小 unlimited

二、root 用户默认值

root 用户的默认值通常 更宽松:

  • open files:可能是 1024 或更高
  • max user processes:更高
  • 很多硬限制是 unlimited

但 systemd 服务中的 root 进程并不等于登录 shell


三、systemd 服务的默认值(非常重要)

如果是 通过 systemd 启动的服务(如 nginx、mysql、docker):

systemctl show <service> | grep -i limit

常见默认值:

项目 默认值
LimitNOFILE 1024(soft) / 4096(hard,旧版本)
LimitNPROC 见 UserTasksMax
TasksMax 默认 v251+ ≈ 10813

⚠️ 很多“线上问题”都是因为这玩意儿,而不是 shell 的 ulimit。


四、如何查看当前值

# 当前 shell
ulimit -a

# 某进程
cat /proc/<pid>/limits

五、如何修改默认值(简要)

1️⃣ 登录用户

/etc/security/limits.conf

* soft nofile 65535
* hard nofile 65535

需要 PAM:

/etc/pam.d/common-session
session required pam_limits.so

2️⃣ systemd 服务

[Service]
LimitNOFILE=65535

或全局:

# /etc/systemd/system.conf
DefaultLimitNOFILE=65535

六、一句话总结

Ubuntu 默认 ulimit -n 最常见是 1024,systemd 服务可能更低;很多“默认值”其实不是内核决定,而是 systemd / PAM 决定的。

如果你愿意,可以告诉我:

  • Ubuntu 版本
  • 是 shell 还是 服务
  • 具体是哪一种资源(open files / processes / core)

我可以直接给你精确默认值 + 修改方案。

0 踩