How to increase the open file limits on Ubuntu,fs.file-max = 2097152

How to increase the open file limits on Ubuntu (linuxhint.com)

On a Linux-based system such as Ubuntu, we occasionally face problems such as “too many open files,” especially when you install and work in platforms like Apache web server and Oracle database. The encountered error signifies that our server has opened the files up to the default maximum number. This issue occurs when the system imposes resource constraints on any user or session. For instance, the maximum size that can be locked into memory, the maximum size of created file, the maximum CPU time that can be utilized, the maximum number of processes that are allowed to execute, and the maximum virtual memory size that can be used; these are all examples of resource limitations in an Ubuntu system.

 

What are the limits types in Ubuntu

Limits in an Ubuntu system are of two types:

  • Soft limit: This type of limit indicates the current value of the session or user.. An Ubuntu user has the option to increase its value to the hard limit.
  • Hard Limit: The superuser/root sets the maximum allowable limit for a user or session, known as the hard limit.

Now, we will explain the procedure to increase the open file limits on Ubuntu. So let’s start!

How to increase the open file limits on Ubuntu

First of all, open your Ubuntu terminal by pressing “CTRL+ALT+T”. Now, we will check out the current limits of the resources. For this, we will execute the “unlimit” command.

What is unlimit command in Ubuntu

The “unlimit” is a Linux shell command utilized to set, view, or limit the current user’s resources. It also extracts information about how many open file descriptors each process has. Many Ubuntu users execute this command to limit the resources which are in the use of a process.

How to check current limits in Ubuntu

To view the current limits set, add the “-a” option in the “unlimit” command:

ulimit -a

Execution of the above-given command will show you the following output:

How to check the soft and hard open file limits in Ubuntu

To view the current soft limit, execute the unlimit command with the “-Sn” option:

ulimit -Sn

From the below-given output, you can see that “1024” is the soft limits of the open files in our Ubuntu system:

The “-Hn” option is added to the “unlimit” command for retrieving the hard limit of the open files:

ulimit -Hn

How to increase the open file limits for the current session in Ubuntu

As mentioned earlier, you can utilize the “unlimit” command for setting the current session limits. If you want to increase the open file limits for your current Ubuntu session, choose a value between the soft and hard limits and add that specific numerical value with the “-n” option. Adding the “-n” option to the “unlimit” command lets you increase the open file limits:

ulimit -n 100000

How to increase per-user open file limits in Ubuntu

You can also increase the open file limits for a specific user. To do so, open the limits configuration file “/etc/security/limits.conf” in the “nano” editor:

sudo nano /etc/security/limits.conf

In the “nano” editor, the limit configuration file will look like this:

Now, add the following content in the “/etc/security/limits.conf” file:

* soft nproc 65535

* hard nproc 65535

* soft nofile 65535

* hard nofile 65535

linuxhint soft nproc 100000

linuxhint hard nproc 100000

linuxhint soft nofile 100000

linuxhint hard nofile 100000

Here:

  • *” denotes the rest of the system users excluding “linuxhint”
  • linuxhint” is our domain
  • soft” or “hard” is the limit type
  • nofile” item is utilized for limiting the number of the file descriptor
  • nproc” item defines the maximum user processes limit
  • 100000” or “65535” are the limit values

Press “CTRL+O” and save the changes we made in the “/etc/security/limits.conf”:

In the next step, we will restrict the number of system resources a user can get in a single session by enabling the pam_limits. For this, you have to edit the “/etc/pam.d/common-session” file in your “nano” editor:

sudo nano /etc/pam.d/common-session

Now, add this line to enable the pam_limits for the current session:

session required pam_limits.so

Press “CTRL+O” to save the added content of “ /etc/pam.d/common-session” file:

How to increase system-wide open file limits in Ubuntu

Ubuntu also provides you the option to increase system-wide open file limits. For this purpose, we will edit the “/etc/sysctl.conf” file:

sudo nano /etc/sysctl.conf

You can set the maximum number of file handles that the Linux kernel will allocate by using the “fs.file-max” parameter. Now, add the below-given line in the “sysctl.conf” file:

fs.file-max = 2097152

Setting the value “2097152” for “fs.file-max” parameter will set this value as maximum number of file handles:

Again, press “CTRL+O” to save the “sysctl.conf” file:

The “sysctl” command with the “-p” option will load the kernel settings from the “sysctl.conf” file. For applying the changes we have made, execute the below-given “sysctl” command in your terminal:

sudo sysctl -p

The execution of the above-given command will increase the maximum number of open files across the entire system:

Conclusion

In Ubuntu, many applications, such as the Apache web server or the Oracle databases, require a higher open file limit, resulting in an excessive number of open files, file descriptors, etc. If the number of open files exceeds the default limit, file opening difficulties and access control issues may arise. This article showed you how you can increase the open file limits in your Ubuntu system. Moreover, you have also seen how to check and increase the system-wide and per-user limits of opened files.

Open Files Limit (riak.com)

How to Solve the "Too Many Open Files" Error on Linux (howtogeek.com)

如果您在 Linux 中看到“打开的文件过多”错误消息,则说明您的进程已达到允许打开的文件上限,通常为 1,024 个。您可以使用命令“ulimit -n 2048”暂时将限制增加到例如 2,048 个文件。通过编辑 systemd 配置文件来永久增加限制。

在 Linux 计算机上,系统资源在用户之间共享。尝试使用超过您应得的份额,您就会达到上限。您还可能成为其他用户或进程的瓶颈。

什么是打开文件过多错误?

在其他无数的工作中,Linux 计算机的内核总是忙于监视谁使用了多少有限的系统资源,例如RAMCPU 周期多用户系统需要持续关注,以确保人员和流程不会使用过多的给定系统资源。

例如,如果某人占用了如此多的 CPU 时间,以至于其他人都感觉计算机运行缓慢,这是不公平的。即使您是唯一使用 Linux 计算机的人,您的进程可以使用的资源也会受到限制。毕竟,您仍然只是另一个用户。

有关的:修复:Linux 中的“错误解释器:没有这样的文件或目录”错误

一些系统资源是众所周知且显而易见的,例如 RAM、CPU 周期和硬盘空间。但是还有很多很多的资源受到监控,并且每个用户(或每个用户拥有的进程)都有一个设定的上限。其中之一是进程可以一次打开的文件数量。

如果您曾在终端窗口中看到“打开的文件过多”错误消息或在系统日志中找到它,则意味着已达到上限,并且不允许该进程打开更多文件。

 

为什么打开这么多文件?

Linux 可以处理的打开文件数量存在系统范围的限制。正如我们将看到的,这是一个非常大的数字,但仍然存在限制。每个用户进程都有一个可以使用的分配。他们每个人都获得分配给他们的系统总量的一小部分。

实际分配的是一些文件句柄。每个打开的文件都需要一个句柄。即使在系统范围内进行了相当慷慨的分配,文件句柄的用完速度也可能比您最初想象的要快。

有关的:Linux 中“一切皆文件”是什么意思?

Linux 抽象了几乎所有内容,因此它看起来就像一个文件。有时它们只是普通的旧文件。但其他操作(例如打开目录)也使用文件句柄。Linux 使用块特殊文件作为硬件设备的一种驱动程序。字符特殊文件非常相似,但它们更常与具有吞吐量概念的设备一起使用,例如管道和串行端口。

块特殊文件一次处理数据块,而字符特殊文件单独处理每个字符。这两个特殊文件只能使用文件句柄来访问。程序使用的库使用文件句柄,流使用文件句柄,网络连接使用文件句柄。

抽象所有这些不同的需求,使它们以文件的形式出现,简化了与它们的接口,并允许管道和流等东西工作。

您可以看到,Linux 在幕后打开文件并使用文件句柄只是为了运行自身 — 不用担心您的用户进程。打开文件的计数不仅仅是您打开的文件数。操作系统中几乎所有内容都使用文件句柄。

 

如何检查文件句柄限制

使用此命令可以查看系统范围的最大文件句柄数。

猫 /proc/sys/fs/file-max
查找系统打开文件的最大值

这会返回一个大得荒谬的数字 9.2 quintillion。这是理论系统的最大值。它是64 位有符号整数中可以保存的最大可能值。您那台可怜的计算机是否真的能够处理同时打开那么多文件则完全是另一回事。

在用户级别,可以打开的文件的最大数量没有明确的值。但我们可以粗略地算出来。要找出某个进程可以打开的最大文件数,我们可以使用ulimit带有-n(open files) 选项的命令。

ulimit-n
查找一个进程可以打开多少个文件

为了找到用户可以拥有的最大进程数,我们将使用ulimit-u用户进程)选项。

ulimit -u
查找用户可以拥有的进程数

1024 和 7640 相乘得到 7,823,360。当然,其中许多进程已被您的桌面环境和其他后台进程使用。所以这是另一个理论上的最大值,也是你永远无法实际实现的。

重要的数字是进程可以打开的文件数量。默认情况下,该值为 1024。值得注意的是,同时打开同一个文件 1024 次与同时打开 1024 个不同的文件相同。一旦用完所有文件句柄,就完成了。

可以调整进程可以打开的文件数量。调整此数字时实际上需要考虑两个值。第一个是当前设置的值,或者您尝试设置的值。这称为软限制。还有一个硬限制,这是您可以将软限制提高到的最高值。

思考这个问题的方法是,软限制实际上是“当前值”,而上限是当前值可以达到的最高值。普通的非 root 用户可以将其软限制提高到硬限制以内的任意值。root 用户可以增加其硬限制。

要查看当前的软限制和硬限制,请ulimit-S(软)和-H(硬)选项以及-n(打开文件)选项一起使用。

ulimit-Sn
ulimit-Hn
查找进程文件句柄的软限制和硬限制

为了创建一种可以看到软限制被强制执行的情况,我们创建了一个程序,该程序会重复打开文件直到失败。然后,它会等待击键,然后放弃它使用的所有文件句柄。该程序称为open-files.

./open-文件
打开文件程序达到 1024 的软限制

它打开 1021 个文件,但在尝试打开文件 1022 时失败。

1024 减 1021 是 3。其他三个文件句柄发生了什么?它们用于流。STDINSTDOUTSTDERR它们是为每个进程自动创建的。它们的文件描述符值始终为 0、1 和 2。

有关的:如何使用 Linux Lsof 命令

我们可以使用带有(process) 选项的命令lsof-p程序的进程 IDopen-files来查看这些内容。它可以方便地将其进程 ID 打印到终端窗口。

lsof -p 11038
lsof 命令输出中的 stdin、stdout 和 stderr 流和文件句柄

当然,在现实情况下,您可能不知道哪个进程刚刚吞噬了所有文件句柄。要开始调查,您可以使用这一系列管道命令。它会告诉您计算机上文件句柄的 15 个最多产的用户。

lsof | awk '{ print $1 " " $2; }' | 排序-rn | uniq-c| 排序-rn | 头-15
查看使用最多文件句柄的进程

要查看更多或更少的条目,请调整命令-15的参数head。一旦确定了该进程,您需要弄清楚它是否已经失控并且由于失控而打开了太多文件,或者它是否真的需要这些文件。如果确实需要它们,您需要增加其文件句柄限制。

 

如何增加软限制

如果我们增加软限制并再次运行我们的程序,我们应该看到它打开更多文件。我们将使用该ulimit命令和-n数值为 2048 的(打开文件)选项。这将是新的软限制。

ulimit -n 2048
为进程设置新的文件句柄软限制

这次我们成功打开了2045个文件。正如预期的那样,这比 2048 少了 3 个,因为STDINSTDOUT和所使用的文件句柄STDERR

 

如何永久更改文件限制

增加软限制仅影响当前 shell。打开一个新的终端窗口并检查软限制。您会看到它是旧的默认值。但是有一种方法可以为进程可以打开的文件的最大数量全局设置一个新的默认值,该默认值是持久的并且在重新启动后仍然存在。

过时的建议通常建议您编辑“/etc/sysctl.conf”和“/etc/security/limits.conf”等文件。然而,在基于systemd的发行版上,这些编辑不能一致地工作,特别是对于图形登录会话。

这里展示的技术是在基于 systemd 的发行版上执行此操作的方法。我们需要使用两个文件。第一个是“/etc/systemd/system.conf”文件。我们需要使用sudo.

sudo gedit /etc/systemd/system.conf
编辑system.conf文件

搜索包含字符串“DefaultLimitNOFILE”的行。从行的开头删除散列“#”,并将第一个数字编辑为您希望新的进程软限制的值。我们选择 4096。该行的第二个数字是硬限制。我们没有调整这个。

system.conf 文件中的 DefaultLimitNOFILE 值

保存文件并关闭编辑器。

我们需要对“/etc/systemd/user.conf”文件重复该操作。

sudo gedit /etc/systemd/user.conf
编辑 user.conf 文件

对包含字符串“DefaultLimitNOFILE”的行进行相同的调整。

user.conf 文件中的 DefaultLimitNOFILE 值

保存文件并关闭编辑器。您必须重新启动计算机或使用systemctl带有daemon-reexec选项的命令,以便systemd重新执行并获取新设置。

sudo systemctl 守护进程重新执行
重新启动系统

打开终端窗口并检查新限制应显示您设置的新值。在我们的例子中是 4096。

ulimit-n
使用 ulimit -n 检查新的软限制

我们可以通过重新运行我们的文件贪婪程序来测试这是一个实时的、可操作的值。

./open-文件
使用 open-files 程序检查新的软限制

程序无法打开文件号 4094,这意味着打开了 4093 个文件。这是我们的预期值,比 4096 少 3。

 

记住一切都是文件

这就是 Linux 如此依赖文件句柄的原因。现在,如果您开始用完它们,您就会知道如何增加配额。

Linuxで開ける最大のfile descriptorの数について調べたメモ - Qiita

检查了有关可以在 Linux 中打开的最大文件描述符数量的注释

为了在一台服务器上维护大量永远在线的连接,我认为 Linux 文件描述符的上限将是一个瓶颈。
首先,请注意,如果您更改以下描述的设置不佳,系统可能无法正常工作,因此在更改时要小心。 当然,我不能承担责任。


文件描述符有两个上限:

  • 1 最大进程可以打开
  • 全系统开仓限额

1 最大进程可以打开

进程可以打开的上限有两种类型,称为软限制和硬限制。 此外,这些也是基于每个进程配置的。
您可以将 0 范围内的软限制更改为硬限制。
您可以自由更改硬限制以使其变小,但您有一个称为 CAP_SYS_RESOURCE 的专用权限来放大它。 您具有配置自己进程以外的进程的相同权限。

在命令行中,经常使用 ulimit 命令,这是一个 bash 内置命令。

  • ulimit -n 或参阅 中的软限制。 如果添加参数,请更改它。ulimit -Sn
  • ulimit -Hn 请参阅硬限制。 如果添加参数,请更改它。

ulimit 命令是使用 getrlimit/setrlimit 系统调用实现的。 可能。
在命令中,getrlimit/setrlimit 获取/修改项目。
默认软限制为 1024,硬限制为 4096。
ulimit -nRLIMIT_NOFILERLIMIT_NOFILE

如果你谷歌文件描述符,你可以找到一篇文章,你可以通过改变和重建linux内核来改变上限,但这似乎是一个改变旧Linux内核RLIMIT_NOFILE的初始软限制和硬限制的故事。 现在没有INR_OPEN变量。INR_OPEN

如上所述,可以使用 ulimit 命令更改此RLIMIT_NOFILE,但这仅限于内核参数的fs.nr_open。 nr_open的默认值为 1048576 (=1024*1024)。
这也可以改变,但上限用以下公式表示: 我认为在正常的 64 位环境中,它会2147483584 (= (2^31-1)&-64)

 
unsigned int sysctl_nr_open_max =
    __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

系统范围限制

整个系统的上限是内核参数 fs.file-max。

当然,我认为 nr_open < = 文件最大值,但特权用户似乎能够打开大于文件最大值的文件。 惊讶。

初始值似乎在这里确定。 我不完全明白,但它似乎不到内存 (KB) 的 10%。

最大值可以上升到无符号多头的上限。

总结

每个进程可以打开的最大文件描述符数由以下公式表示,该公式在典型的 64 位环境中2147483584。

可配置系统中可以打开的最大文件描述符数为 ULONG_MAX,这对于典型的 64bi 环境18446744073709551615。
就一台服务器可以处理的连接数量而言,这是开始调查的动机,如果设置它,你可以在一个进程中处理超过 20 亿个,所以看起来这不会成为瓶颈。

我不熟悉 Linux 代码库,所以如果我在引用源代码时犯了错误,我很抱歉。

另外,我写道,首先更改它时应该小心,但您必须特别小心fs.file-max。 如果将其设置为太小的值,您将无法创建新的文件描述符,并且除非您是 root 用户,否则似乎从 bash 执行可执行文件的所有操作都将失败。1。 换句话说,返回 fs.file-max 值和重新启动的 sysctl 命令都将失败。 由于您无法制作伪终端,因此无法登录新终端。 此外,如果您设置的值超过了无符号长整型的上限,它将溢出,即使您尝试设置略高于ULONG_MAX的值也会发生同样的事情。 当你实际尝试它时,它看起来像这样。

 
vagrant@ubuntu-xenial:~$ sudo sysctl fs.file-max=18446744073709551617
fs.file-max = 18446744073709551617
vagrant@ubuntu-xenial:~$ ls
-bash: start_pipeline: pgrp pipe: Too many open files in system
-bash: /bin/ls: Too many open files in system

即使可以从外部重新启动,例如虚拟机,如果是数据中心的物理服务器,可能也别无选择,只能按现场的物理电源按钮或停止电源,终止它,然后重新启动它。
由于RLIMIT_NOFILE适用于每个进程,因此当您再次登录时,可以重置 shell,并且您可以从另一个进程更改特定进程。 设置了nr_open最小值,在 64 位计算机上,只能设置最小值 64。 与 file-max 相比,操作不正确仍然更好。


  1. 这就像每次运行可执行文件时创建管道一样。 

 
posted @ 2023-07-21 10:19  CharyGao  阅读(144)  评论(0)    收藏  举报