在 Debian(以及大多数 Linux 发行版)中,ulimit 的永久生效取决于你想对谁、在什么场景下生效。下面按常见场景详细说明。
/etc/security/limits.conf这是最标准、最常用的方式。
sudo nano /etc/security/limits.conf
在文件末尾添加,例如:
* soft nofile 65535
* hard nofile 65535
说明:
*:所有用户(也可指定用户名)soft:软限制(用户可自行修改)hard:硬限制(不能超过)nofile:最大打开文件数其他常见项:
* soft nproc 65535
* hard nproc 65535
检查 /etc/pam.d/common-session 和 /etc/pam.d/common-session-noninteractive 中是否包含:
session required pam_limits.so
一般 Debian 默认已启用。
logout
# 或
reboot
验证:
ulimit -n
⚠️ systemd 不会读取 limits.conf
sudo nano /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
nano ~/.bashrc
添加:
ulimit -n 65535
ulimit -u 65535
⚠️ 注意:
Debian 中 root 默认不受 limits.conf 限制,除非:
root soft nofile 65535
root hard nofile 65535
ulimit -a
ulimit -n
ulimit -u
或查看进程限制:
cat /proc/<PID>/limits
| 问题 | 原因 |
|---|---|
| 修改 limits.conf 不生效 | 没重新登录 / systemd 服务 |
| systemd 服务不生效 | systemd 不读 limits.conf |
| su / sudo 后不生效 | PAM 配置问题 |
| Docker 容器不生效 | 宿主机或 systemd 限制 |
| 场景 | 推荐方式 |
|---|---|
| 普通用户 | /etc/security/limits.conf |
| 系统服务 | systemctl edit |
| 全局服务 | /etc/systemd/system.conf |
| Shell 脚本 | ~/.bashrc(仅辅助) |
如果你能说明:
我可以给你精确到命令级别的配置方案。