如何更改 Linux 中打开文件的数量限制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:22:38  来源:igfitidea点击:

How do I change the number of open files limit in Linux?

linux

提问by John Meagher

When running my application I sometimes get an error about too many files open.

运行我的应用程序时,我有时会收到有关too many files open.

Running ulimit -areports that the limit is 1024. How do I increase the limit above 1024?

运行ulimit -a报告限制为 1024。如何将限制增加到 1024 以上?

Editulimit -n 2048results in a permission error.

编辑ulimit -n 2048导致权限错误。

采纳答案by hoyhoy

You could always try doing a ulimit -n 2048. This will only reset the limit for your current shell and the number you specify must not exceed the hard limit

你总是可以尝试做一个ulimit -n 2048. 这只会重置您当前 shell 的限制,并且您指定的数量不得超过硬限制

Each operating system has a different hard limit setup in a configuration file. For instance, the hard open file limit on Solaris can be set on boot from /etc/system.

每个操作系统在配置文件中都有不同的硬限制设置。例如,可以在从 /etc/system 引导时设置 Solaris 上的硬打开文件限制。

set rlim_fd_max = 166384
set rlim_fd_cur = 8192

On OS X, this same data must be set in /etc/sysctl.conf.

在 OS X 上,必须在 /etc/sysctl.conf 中设置相同的数据。

kern.maxfilesperproc=166384
kern.maxfiles=8192

Under Linux, these settings are often in /etc/security/limits.conf.

在 Linux 下,这些设置通常在 /etc/security/limits.conf 中。

There are two kinds of limits:

有两种限制:

  • softlimits are simply the currently enforced limits
  • hardlimits mark the maximum value which cannot be exceeded by setting a soft limit
  • 限制只是当前强制执行的限制
  • 限制标记了通过设置软限制不能超过的最大值

Soft limits could be set by any user while hard limits are changeable only by root. Limits are a property of a process. They are inherited when a child process is created so system-wide limits should be set during the system initialization in init scripts and user limits should be set during user login for example by using pam_limits.

任何用户都可以设置软限制,而硬限制只能由 root 更改。限制是过程的一个属性。它们是在创建子进程时继承的,因此应在系统初始化期间在 init 脚本中设置系统范围的限制,并应在用户登录期间设置用户限制,例如使用 pam_limits。

There are often defaults set when the machine boots. So, even though you may reset your ulimit in an individual shell, you may find that it resets back to the previous value on reboot. You may want to grep your boot scripts for the existence ulimit commands if you want to change the default.

机器启动时通常会设置默认值。因此,即使您可以在单个 shell 中重置您的 ulimit,您也可能会发现它在重新启动时重置回以前的值。如果您想更改默认值,您可能需要 grep 存在 ulimit 命令的引导脚本。

回答by Jonathan Stanton

If you are using Linux and you got the permission error, you will need to raise the allowed limit in the /etc/limits.confor /etc/security/limits.conffile (where the file is located depends on your specific Linux distribution).

如果您使用的是 Linux 并且遇到权限错误,则需要提高/etc/limits.confor/etc/security/limits.conf文件中的允许限制(文件所在的位置取决于您的特定 Linux 发行版)。

For example to allow anyone on the machine to raise their number of open files up to 10000 add the line to the limits.conffile.

例如,要允许机器上的任何人将其打开文件的数量增加到 10000,请将该行添加到limits.conf文件中。

* hard nofile 10000

* hard nofile 10000

Then logout and relogin to your system and you should be able to do:

然后注销并重新登录到您的系统,您应该能够执行以下操作:

ulimit -n 10000

ulimit -n 10000

without a permission error.

没有权限错误。

回答by Vikrant Telkar

1) Add the following line to /etc/security/limits.conf

1) 将以下行添加到 /etc/security/limits.conf

webuser hard nofile 64000

then login as webuser

然后以网络用户身份登录

su - webuser

2) Edit following two files for webuser

2) 为 webuser 编辑以下两个文件

append .bashrc and .bash_profilefile by running

通过运行追加.bashrc 和 .bash_profile文件

echo "ulimit -n 64000" >> .bashrc ; echo "ulimit -n 64000" >> .bash_profile

3) Log out, then log back in and verify that the changes have been made correctly:

3) 注销,然后重新登录并验证更改是否正确:

$ ulimit -a | grep open
open files                      (-n) 64000

Thats it and them boom, boom boom.

就是这样,他们繁荣,繁荣。

回答by Sysadmin

If some of your services are balking into ulimits, it's sometimes easier to put appropriate commands into service's init-script. For example, when Apache is reporting

如果您的某些服务无法使用 ulimit,有时将适当的命令放入服务的 init 脚本中会更容易。例如,当 Apache 报告

[alert] (11)Resource temporarily unavailable: apr_thread_create: unable to create worker thread

[alert] (11)资源暂时不可用:apr_thread_create:无法创建工作线程

Try to put ulimit -s unlimitedinto /etc/init.d/httpd. This does not require a server reboot.

尝试把ulimit -s unlimited/etc/init.d/httpd。这不需要重新启动服务器。