在 Debian(以及大多数 Linux 发行版)中,ulimit 用于限制单个 shell 进程及其子进程所能使用的系统资源。它并不限制整个系统,而是针对用户会话 / 进程级别。
下面按常见类别说明 ulimit 能限制什么,以及 Debian 中的典型表现。
可通过 ulimit -a 查看当前限制。
| 项目 | 说明 |
|---|---|
-u (max user processes) |
用户可创建的最大进程/线程数 |
-t (cpu time) |
进程最多使用的 CPU 时间(秒) |
-e (scheduling priority) |
允许设置的最高优先级(nice 值下限) |
-r (real-time priority) |
实时调度优先级上限 |
| 项目 | 说明 |
|---|---|
-v (virtual memory) |
虚拟内存上限(KB) |
-m (resident set size) |
常驻内存(很多系统已忽略) |
-s (stack size) |
线程栈大小 |
-l (locked memory) |
可锁定的内存(如 mlock) |
| 项目 | 说明 |
|---|---|
-f (file size) |
单文件最大大小(blocks) |
-n (open files) |
最多打开的文件描述符数 |
-c (core file size) |
core dump 文件大小 |
-d (data seg size) |
数据段大小 |
| 项目 | 说明 |
|---|---|
-p (pipe size) |
管道缓冲区大小(通常只读) |
-x (file locks) |
文件锁数量 |
ulimit -n 1024
./my_program # 受限制
ulimit -Sn # soft
ulimit -Hn # hard
Debian 通常通过以下方式设置:
/etc/security/limits.conf* soft nofile 1024
* hard nofile 65535
# /etc/pam.d/common-session
session required pam_limits.so
⚠️ 对 systemd 服务不生效
ulimit 对 systemd 服务无效,需用:
[Service]
LimitNOFILE=65535
LimitNPROC=512
❌ 不限制:
fs.file-max)ulimit -a
ulimit -n 65535
cat /proc/<pid>/limits
如果你关心的是:
可以告诉我具体场景,我可以给更针对性的建议。