在 Debian(以及大多数 Linux 发行版)中,ulimit 用于控制单个进程可使用的系统资源上限,如文件描述符数、进程数、内存、栈大小等。调优 ulimit 通常是为了:
下面按使用场景系统说明如何在 Debian 上调优 ulimit。
ulimit -a
常用项说明:
-n open files
-u max user processes
-s stack size
-c core file size
-m resident set size (常忽略)
-v virtual memory
查看某一项:
ulimit -n
ulimit -u
ulimit -n 65536
ulimit -u 4096
⚠️ 只在当前 shell 生效,重启或新登录失效。
/etc/security/limits.confsudo vim /etc/security/limits.conf
示例(针对用户或组):
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
说明:
soft:警告值(可临时超过)hard:硬上限(不可超过)nofile:打开文件数nproc:进程数* 表示所有用户,也可写具体用户名✅ 需要 PAM 支持(Debian 默认支持)
确保以下文件包含:
session required pam_limits.so
通常在:
/etc/pam.d/common-session/etc/pam.d/sshdlimits.conf 对 systemd 启动的服务无效!
systemctl show nginx | grep Limit
编辑 service 文件:
sudo systemctl edit nginx
写入:
[Service]
LimitNOFILE=65536
LimitNPROC=4096
重新加载:
sudo systemctl daemon-reload
sudo systemctl restart nginx
即使 ulimit -n 调大,系统总 fd 也有限制。
cat /proc/sys/fs/file-max
sudo vim /etc/sysctl.conf
fs.file-max = 2097152
生效:
sudo sysctl -p
nofile 65535
nproc 4096
nofile 100000
nproc 8192
nofile 1048576
# 登录后
ulimit -n
# 查看某进程
cat /proc/<pid>/limits
❌ limits.conf 不生效
→ 服务由 systemd 启动
❌ 改了不生效
→ 没重新登录 / 没重启服务
❌ “Too many open files”
→ 检查 nofile + fs.file-max
如果你愿意,可以告诉我:
我可以给你一套针对性的 ulimit 调优方案。