无需重启即可修改和应用limits.conf

mah*_*ood 21 linux ulimit

我添加了一行/etc/security/limits.conf以增加打开文件的数量。

*    hard nofile 4096
root hard nofile 16384
Run Code Online (Sandbox Code Playgroud)

但是,当我运行时,ulimit -n它说 1024 是默认值。我注销并登录,但仍然看到 1024。我该如何应用更改?

Tom*_*art 23

ulimit命令所做的更改:

$ ulimit -n 4096
$ ulimit -Hn 16384
Run Code Online (Sandbox Code Playgroud)

将仅适用于当前用户和会话。为了使其永久化,您必须/etc/security/limits.conf通过添加限制进行修改:

* soft nofile 4096
* hard nofile 16384
Run Code Online (Sandbox Code Playgroud)

但是,通配符*不适用于root用户。为此,您必须明确说明:

* soft nofile 4096
* hard nofile 16384
root soft nofile 4096
root hard nofile 16384
Run Code Online (Sandbox Code Playgroud)

这些限制将在重新启动后应用。

如果要在不重启的情况下应用更改,请/etc/pam.d/common-session在文件末尾添加以下行进行修改:

session required pam_limits.so
Run Code Online (Sandbox Code Playgroud)

下次登录时,您应该会看到更新的限制,您可以检查它们(软限制和硬限制):

$ ulimit -a
$ ulimit -Ha
Run Code Online (Sandbox Code Playgroud)

  • 我对这种方法有一个问题,这真的很奇怪。我正在使用 Ubuntu 14 并在 `/etc/pam.d/common-session` 中启用了 `pam_limits.so`。我已经在`/etc/security/limits.conf` 用户x 中配置了`nofile` 64000 的硬限制和软限制。`sudo -ux` 然后`ulimit -a` 显示我没有应用更改。我意识到`su` 和`sudo` 具有不同的pam 配置,因此为了使其正常工作,我需要在`/etc/pam.d/common-session-noninteractive` 中启用`pam_limits.so`。如果您想知道用例是什么 - 我使用 ansible 和 sudo 来更改用户。 (2认同)

Flu*_*lup 11

如果您使用bashulimit -n将只显示软限制。要获得硬限制,您需要执行ulimit -Hn.

在我的系统上,我看到了这个:

$ ulimit -n
1024
$ ulimit -Hn
4096
Run Code Online (Sandbox Code Playgroud)