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

Max*_*ler 7 linux ulimit max-file-descriptors

在 linux 上运行的服务器应用程序通常需要大量的打开文件处理程序,例如。HBase ulimit , Hadoop 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
Run Code Online (Sandbox Code Playgroud)

Ist*_*van 9

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

# vi /etc/sysctl.conf

fs.file-max = 32000

# vi /etc/security/limits.conf

youruser       soft    nofile   10000
youruser       hard    nofile   30000
Run Code Online (Sandbox Code Playgroud)

硬限制和软限制:

人 5 限制.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.
Run Code Online (Sandbox Code Playgroud)

HTH