在 Debian 里调大 ulimit(资源限制,比如打开文件数、进程数等),通常分临时和永久两种方式,下面按常见场景说明。
直接在终端执行:
ulimit -n 65535 # 打开文件数
ulimit -u 65535 # 最大进程数
ulimit -s unlimited # 栈大小
查看当前限制:
ulimit -a
⚠️ 只在当前终端和该终端启动的进程中有效,重启或新开 shell 失效。
/etc/security/limits.conf编辑文件:
sudo vim /etc/security/limits.conf
示例(针对所有用户):
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
或指定用户:
www-data soft nofile 100000
www-data hard nofile 100000
字段说明:
soft:软限制(可临时超过,但会报警)hard:硬限制(不能超过)nofile:打开文件数nproc:进程数✅ 需要 重新登录 或重启才能生效。
Debian 默认需要 PAM 支持:
grep pam_limits /etc/pam.d/common-session
如果没有,添加:
session required pam_limits.so
同样检查:
/etc/pam.d/common-session-noninteractive
如果是 systemd 管理的服务(如 nginx、mysql、docker),limits.conf 不会生效!
sudo systemctl edit nginx
写入:
[Service]
LimitNOFILE=65535
LimitNPROC=65535
然后:
sudo systemctl daemon-reload
sudo systemctl restart nginx
sudo vim /etc/systemd/system.conf
DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535
重启系统生效。
查看系统最大文件数:
cat /proc/sys/fs/file-max
临时改:
sudo sysctl -w fs.file-max=1000000
永久:
echo "fs.file-max=1000000" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
❌ ulimit 改了但服务没生效
✅ systemd 服务必须单独配
❌ 非 root 用户改 hard limit 失败
✅ 只有 root 能提 hard limit
❌ Docker 容器限制没变
✅ 需要在宿主机或 docker 启动参数里改
如果你能说一下具体场景(比如:Nginx、MySQL、Docker、普通用户、SSH 登录),我可以给你更精确的配置。