用 ulimit -n 提高打开文件数上限
ulimit -n 可查看或修改当前 shell 的文件描述符上限,解决从该 shell 启动的服务报“Too many open files”的问题。
运行 `ulimit -n` 查看当前软限制,`ulimit -n 4096` 可为当前 shell 会话提高上限(不能超过 `ulimit -Hn` 显示的硬限制)。
01命令
命令
$ ulimit -n 409602作用说明
RLIMIT_NOFILE 比最大可用文件描述符编号大 1,而每个 socket、管道或打开的文件都占一个,因此默认 1024 的软限制很容易被长期运行的守护进程耗尽。ulimit -n 只设置软限制,且不能超过硬限制(权限允许时可用 `ulimit -n 4096 -Hn` 同时调高两者)。针对单个 shell 的修改不影响已运行的服务——systemd 服务应在单元中设置 LimitNOFILE=,登录会话则用 /etc/security/limits.conf。
03风险
04撤销
撤销
$ ulimit -n 102405来源
- getrlimit(2)/setrlimit(2) - get/set resource limits依据: RLIMIT_NOFILE specifies a value one greater than the maximum file descriptor number that can be opened by this process; limits have a soft value (enforced) and a hard value (ceiling for the soft).
最后验证: 2026-09-17 · 分类: 系统与设置