在 Debian 系统中,ulimit 用于控制用户进程可使用的系统资源上限(如打开文件数、进程数、栈大小等)。优化 ulimit 通常是为了:
下面按使用场景系统性说明如何优化。
ulimit -a
常见关键项:
-n:最大打开文件数(open files)-u:最大用户进程数(max user processes)-s:栈大小-c:core 文件大小-v:虚拟内存ulimit -n 65535
ulimit -u 4096
⚠️ 重启或新登录后失效。
/etc/security/limits.conf编辑:
vi /etc/security/limits.conf
示例(针对所有用户或特定用户):
* soft nofile 65535
* hard nofile 65535
* soft nproc 4096
* hard nproc 4096
说明:
soft:警告值(可临时超过)hard:硬性上限nofile:打开文件数nproc:进程数✅ 生效条件:
pam_login 启动Debian 9+ 默认使用 systemd,
limits.conf 对 systemd 服务不一定生效!
vi /etc/systemd/system.conf
DefaultLimitNOFILE=65535
DefaultLimitNPROC=4096
然后:
systemctl daemon-reexec
systemctl edit nginx
[Service]
LimitNOFILE=65535
LimitNPROC=4096
重载:
systemctl daemon-reload
systemctl restart nginx
nofile 65535~100000
nproc 4096+
nofile 65535
nproc 4096
nofile 100000
ulimit 受内核参数限制:
sysctl -a | grep fs.file
修改:
vi /etc/sysctl.conf
fs.file-max = 1000000
生效:
sysctl -p
cat /proc/<pid>/limits
或:
systemctl show nginx | grep Limit
❌ limits.conf 对 cron / docker 不生效
✅ Docker 需在 docker.service 中设置
❌ sudo 后 ulimit 可能重置
✅ 使用 sudo -i 或 systemd 管理
* soft nofile 100000
* hard nofile 100000
* soft nproc 8192
* hard nproc 8192
# systemd
DefaultLimitNOFILE=100000
如果你能说明:
我可以给你精准配置方案。