On Linux - Set Maximum Open Files to Unlimited. Possible

On Linux - set maximum open files to unlimited. Possible?

POSIX allows you to set the RLIMIT_NOFILE resource limit to RLIM_INFINITY using setrlimit(). What this means is that the system will not enforce this resource limit. Of course, you will still be limited by the implementation (e.g. MAXINT) and any other resource limitations (e.g. available memory).

Update: RHEL 5 has a maximum value of 1048576 (220) for this limit (NR_OPEN in /usr/include/linux/fs.h), and will not accept any larger value including infinity, even for root. So on RHEL 5 you can use this value in /etc/security/limits.conf and that is as close as you are going to get to infinity.

Not long ago a Linux kernel patch was applied to allow this limit to be set to infinity, however it has since been reverted as a result of unintended consequences.

How do I increase the open files limit for a non-root user?

The ulimit command by default changes the HARD limits, which you (a user) can lower, but cannot raise.

Use the -S option to change the SOFT limit, which can range from 0-{HARD}.

I have actually aliased ulimit to ulimit -S, so it defaults to the soft limits all the time.

alias ulimit='ulimit -S'

As for your issue, you're missing a column in your entries in /etc/security/limits.conf.

There should be FOUR columns, but the first is missing in your example.

* soft nofile 4096
* hard nofile 4096

The first column describes WHO the limit is to apply for. '*' is a wildcard, meaning all users. To raise the limits for root, you have to explicitly enter 'root' instead of '*'.

You also need to edit /etc/pam.d/common-session* and add the following line to the end:

session required pam_limits.so

Max open files for working process

As a system administrator: The /etc/security/limits.conf file controls this on most Linux installations; it allows you to set per-user limits. You'll want a line like myuser - nofile 1000.

Within a process: The getrlimit and setrlimit calls control most per-process resource allocation limits. RLIMIT_NOFILE controls the maximum number of file descriptors. You will need appropriate permissions to call it.

What is the max opened files limitation on Linux?

You can check the Soft limits and hard limits of your system by ulimit -a command.

  1. soft limits are simply the currently enforced limits.
  2. hard limits 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.

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.

If the limitation was 1024, means you/process can open maximum 1024 files. if you exceed this limit means open, pipe and dup system calls will fail:

RLIMIT_NOFILE:

Specifies a value one greater than the maximum file descriptor number that can be opened by this process. Attempts (open(2), pipe(2), dup(2), etc.) to exceed this limit yield the error EMFILE.

Cannot change the maximum open files per process with sysctl

For Ubuntu 17.04. See this solution.

Prior to Ubuntu 17.04:

I don't know why the above settings don't work but it seems you can get the same result by using the /etc/security/limits.conf file.

Set the limit in /etc/security/limits.conf

sudo bash -c "echo '* - nofile 10240' >> /etc/security/limits.conf"
  • * means all users. You can replace it by a specific username.
  • - means both soft and hard for the type of limit to be enforced. Hard can only be modified by the superuser. Soft can be modified by a non-root user and cannot be superior to hard.
  • nofile is the Maximum number of open files parameter.
  • 10240 is the new limit.

Reload

Logout and log back in. sudo sysctl -p doesn't seem to be enough to reload.

You can check the new limit with:

ulimit -n

Tested on Ubuntu 16.04 and CentOS 6. Inspired by this answer.

How to set nginx max open files?

On CentOS (tested on 7.x):

Create file /etc/systemd/system/nginx.service.d/override.conf with the following contents:

[Service]
LimitNOFILE=65536

Reload systemd daemon with:

systemctl daemon-reload

Add this to Nginx config file:

worker_rlimit_nofile 16384; (has to be smaller or equal to LimitNOFILE set above)

And finally restart Nginx:

systemctl restart nginx

You can verify that it works with cat /proc/<nginx-pid>/limits.

Open more server sockets than in `ulimit -n`

These limits are (on linux) configured in /etc/security/limits.conf.
Here you can increase the number of file descriptors per file, by e.g. adding this line (as root):

*               hard    nofile      20000

Then verify using the ulimit command you used before.
On other unixes there is often a similar kernel parameter in the respective kernel configuration tool.

Increasing the per-process limit may in turn require you to increase the system-wide number of file descriptors. For this, again on linux, use:

# sysctl -w fs.file-max=100000

Why redis can not set maximum open file

Redis will never change the maximum open files.

This is a OS configuration and it can be configured on a per user basis also. The error is descriptive and tells you: "increase 'ulimit -n'"

You can refer to this blog post on how to increase the maximum open files descriptors:
http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/



Related Topics



Leave a reply



Submit