在 Debian 系统中,ulimit(资源限制)可以通过多个地方进行配置,具体取决于你是想临时设置、用户级设置还是系统/服务级设置。下面按常见场景说明配置文件位置。
ulimit -n 65535
只对当前 shell 及其子进程有效,重启或退出后失效。
/etc/security/limits.conf
示例:
* soft nofile 65535
* hard nofile 65535
www-data soft nproc 4096
确保以下文件中包含:
/etc/pam.d/common-session
/etc/pam.d/common-session-noninteractive
包含这一行:
session required pam_limits.so
⚠️ 注意:
如果是服务(如 Nginx、MySQL),limits.conf 可能不生效。
/etc/systemd/system/xxx.service.d/limits.conf
示例:
[Service]
LimitNOFILE=65535
LimitNPROC=4096
然后执行:
systemctl daemon-reload
systemctl restart xxx
/etc/systemd/system.conf
/etc/systemd/user.conf
示例:
DefaultLimitNOFILE=65535
/etc/sysctl.conf
例如:
fs.file-max = 1000000
生效:
sysctl -p
~/.bashrc
~/.profile
/etc/profile
可写 ulimit,但不如 limits.conf 规范。
| 场景 | 配置文件 |
|---|---|
| 临时 | shell 命令 |
| 用户登录 | /etc/security/limits.conf |
| systemd 服务 | xxx.service.d/*.conf |
| 全局 systemd | /etc/systemd/system.conf |
| 内核级 | /etc/sysctl.conf |
如果你能说明是 哪个用户 / 哪个服务 / 哪种资源(nofile、nproc 等),我可以给你更精确的配置示例。