在 Debian(以及大多数 Linux 发行版)中,用户级 ulimit 通常通过 PAM + limits.conf 或 systemd 用户/服务配置 来设置。下面按常见场景说明。
/etc/security/limits.conf(最常用)编辑:
sudo nano /etc/security/limits.conf
<domain> <type> <item> <value>
示例(用户 www):
www soft nofile 65535
www hard nofile 65535
常用项:
soft:软限制(用户可自己改)hard:硬限制(不能超过)nofile:打开文件数nproc:进程数stack:栈大小memlock:锁内存示例(所有用户):
* soft nofile 10240
* hard nofile 65535
Debian 需要 PAM 模块支持:
检查以下文件是否包含:
grep pam_limits /etc/pam.d/login
grep pam_limits /etc/pam.d/sshd
通常应有:
session required pam_limits.so
如果没有,手动添加。
logout
或
su - 用户名
验证:
ulimit -a
ulimit -n
如果是服务(如 nginx、mysql):
systemctl edit nginx
[Service]
LimitNOFILE=65535
LimitNPROC=4096
systemctl daemon-reload
systemctl restart nginx
ulimit -n 65535
⚠️ 重启或退出 shell 后失效。
如果是 systemctl --user:
systemctl --user edit xxx
[Service]
LimitNOFILE=65535
limits.conf 对 root 默认不限制,需要显式写:
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
* soft nproc 4096
* hard nproc 4096
如果你能告诉我:
我可以给你更精确的配置。