在 Debian(以及绝大多数 Linux 发行版)里,“句柄”一般指 文件描述符(file descriptor,fd),常用 ulimit 来查看/限制。
下面按常见场景说明。
ulimit -n
ulimit -Sn # 软限制
ulimit -Hn # 硬限制
示例输出:
1024
表示当前用户最多能打开 1024 个文件句柄。
/proccat /proc/<PID>/limits | grep "open files"
ls 看实际句柄数ls -l /proc/<PID>/fd | wc -l
cat /proc/sys/fs/file-max
当前已用句柄数:
cat /proc/sys/fs/file-nr
输出示例:
1024 0 65536
含义:已分配 / 已释放 / 最大
ulimit -n 65536
仅对当前 shell 及其子进程有效,重启失效。
/etc/security/limits.confsudo vim /etc/security/limits.conf
添加:
* soft nofile 65536
* hard nofile 65536
或指定用户:
www-data soft nofile 65536
www-data hard nofile 65536
⚠ 需要 重新登录 或重启生效。
如果是 systemd 启动的服务,limits.conf 可能不生效,需要在 service 文件里加:
[Service]
LimitNOFILE=65536
然后:
systemctl daemon-reload
systemctl restart xxx
需要在:
--ulimit nofile=65536:65536如果你愿意,可以告诉我:
我可以给你更精确的配置方案。