在 Debian(以及大多数 Linux 发行版)中,ulimit 用来限制进程资源使用。要“解除限制”,本质上是调大或取消某些资源上限。下面按常见场景说明。
ulimit -a
常用单项查看:
ulimit -n # 打开文件数
ulimit -u # 最大进程数
ulimit -s # 栈大小
ulimit -c # core dump
ulimit -n 65535
如果是 root:
ulimit -n unlimited
⚠️ 普通用户不能设置超过 /proc/sys/fs/nr_open(默认 1048576)
ulimit -u unlimited
ulimit -c unlimited
/etc/security/limits.conf编辑:
sudo vim /etc/security/limits.conf
在末尾添加:
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
root soft nofile unlimited
root hard nofile unlimited
说明:
| 项目 | 含义 |
|---|---|
soft |
当前生效限制 |
hard |
最大可设置上限 |
* |
所有用户 |
root |
root 用户 |
⚠️ 需要重新登录才生效
如果你是在 systemd 启动的服务(如 Nginx、Docker、Java)中发现 ulimit 无效,那是因为:
systemd 不读取 limits.conf
sudo vim /etc/systemd/system.conf
添加或修改:
DefaultLimitNOFILE=65535
DefaultLimitNPROC=65535
然后:
sudo systemctl daemon-reexec
sudo reboot
sudo systemctl edit nginx
写入:
[Service]
LimitNOFILE=65535
LimitNPROC=65535
然后:
sudo systemctl restart nginx
cat /proc/sys/fs/file-max
临时修改:
sudo sysctl -w fs.file-max=1000000
永久生效:
sudo vim /etc/sysctl.conf
添加:
fs.file-max = 1000000
生效:
sudo sysctl -p
❌ ulimit 只对当前 shell 有效
❌ su / sudo 不会继承 ulimit
❌ SSH 登录才加载 limits.conf(PAM)
❌ Docker 容器默认继承宿主机限制
ulimit -n
cat /proc/$(pidof 进程名)/limits
✅ 普通用户 / 开发环境:
* soft nofile 65535
* hard nofile 65535
✅ 服务器 / 高并发:
fs.file-max = 1000000
systemd DefaultLimitNOFILE
如果你能说明 具体场景(如 Docker、Nginx、Java、MySQL、Debian 版本),我可以给你精确配置方案。