如何使用 fs.file-max 和 ulimit 配置 Linux 文件描述符限制

如何使用 fs.file-max 和 ulimit 配置 Linux 文件描述符限制

在 Linux 上运行的服务器应用程序通常需要大量打开的文件处理程序,例如HBase ulimitHadoop epoll 限制

该 wiki 条目应作为 Linux 文件限制配置的记录。

  • 软限制和硬限制有什么区别?
  • 如何控制硬限制?
  • 如何控制软限制?
  • 内核 fs.file-max 和用户 ulimit -n 是否相关?

请描述您的配置在哪个 Linux 发行版下有效,因为不同的供应商的配置方式不同。


更新根据 lstvan 的回答:

对于希望自动化此操作的人来说,至少在 Ubuntu 服务器上,您可以将其放入机器安装脚本中:

echo 'fs.file-max = 65000' > /etc/sysctl.d/60-file-max.conf
echo '* soft nofile 65000' > /etc/security/limits.d/60-nofile-limit.conf
echo '* hard nofile 65000' >> /etc/security/limits.d/60-nofile-limit.conf
echo 'root soft nofile 65000' >> /etc/security/limits.d/60-nofile-limit.conf
echo 'root hard nofile 65000' >> /etc/security/limits.d/60-nofile-limit.conf

答案1

您的操作系统对主机上任何正在运行的应用程序可以打开的文件数量设置了限制。您可以通过修改 2 个配置文件轻松扩展基本值(通常为 1024):

# vi /etc/sysctl.conf

fs.file-max = 32000

# vi /etc/security/limits.conf

youruser       soft    nofile   10000
youruser       hard    nofile   30000

硬限制和软限制:

man 5 limits.conf

hard
for enforcing hard resource limits. These limits are set by the superuser and 
enforced by the Kernel. The user cannot raise his requirement of system resources
above such values.

soft
for enforcing soft resource limits. These limits are ones that the user
can move up or down within the permitted range by any pre-exisiting hard 
limits. The values specified with this token can be thought of as default values,
for normal system usage.

高血压

答案2

如果您要增加 Ubuntu 上的文件限制,您可以使用星号来表示“所有用户”的软文件限制和硬文件限制。但是,这将不是包括 root 用户,因此如果您希望增加的限制也适用于 root 用户,则必须单独定义它(因此您可以为“root 用户”和“其他所有人”设置不同的限制),如下所示:

* soft nofile 4096
* hard nofile 10240
root soft nofile 4096
root hard nofile 10240

最简洁的方法可能是将它们添加到单独的文件中,例如/etc/security/limits.d/60-nofile-limit.conf

相关内容