无法使用 sysctl 更改每个进程的最大打开文件数

Cannot change the maximum open files per process with sysctl

我的实际极限是1024:

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 95979
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

我试过了:

sysctl -w fs.file-max=100000

并附加到 /etc/sysctl.conf

fs.file-max = 100000

没有成功,即使在 运行 sysctl -p 重新加载设置 and/or 重新启动后,在 Ubuntu 16.04 和 CentOS 6 上也是如此。

它始终保持设置为 1024


这个问题是的延伸。

对于 Ubuntu 17.04。参见 this solution

在 Ubuntu 17.04 之前:

我不知道为什么上述设置不起作用,但使用 /etc/security/limits.conf file.

似乎可以得到相同的结果

/etc/security/limits.conf

中设置限制
sudo bash -c "echo '* - nofile 10240' >> /etc/security/limits.conf"
  • *表示所有用户。您可以将其替换为特定的用户名。
  • - 表示 type of limit to be enforcedsofthard。 Hard 只能由超级用户修改。 Soft 可以被非 root 用户修改,不能优于 hard。
  • nofile最大打开文件数 参数。
  • 10240 是新的限制。

重新加载

注销并重新登录。sudo sysctl -p 似乎不够重新加载。

您可以通过以下方式检查新限制:

ulimit -n

在 Ubuntu 16.04 和 CentOS 6 上测试。受此启发 answer

对于 MacOs 10.14.6 及更高版本,如果您需要临时提高限制,以下方法有效:

检查您的当前限制:

ulimit -n

我的是 256

更改它:

ulimit -n 1024

在同一选项卡中再次检查:

ulimit -n

我的现在显示 1024。

更改是即时的,无需注销并重新登录,也无需打开新的终端选项卡。这解决了我的特殊问题。

很遗憾,更改是暂时的。稍后,当您打开一个新的终端时 window,您将返回旧的值和问题。