Error too many open files in Linux

If you have worked with programs that have to handle a very large number of file descriptors, for example, with distributed databases such as Elasticsearch, you were probably faced with the error “too many open files in Linux”.

In this small article we will figure out what this error means and how to fix it in different situations.

Error too many open files Linux

Literally, this error means that the program has opened too many files and more it can’t be opened. Linux has a hard limit on the number of open files for each process and user.

See how many you can open files in your file system, execute the command:

cat /proc/sys/fs/file-max

To see current limits on the number of open files for a user with the command:

ulimit-n

Utility ulimit returns two types of constraints – hard and soft. The soft limit you can change in any direction until it exceeds the hard. The hard limit can only be changed in the smaller party on behalf of a normal user. As root you can change both types of restrictions as it should. Displays by default the soft-limit:

ulimit-nS

Hard to display, use the-H option:

ulimit -nH

You can change the limit by simply passing in the new ulimit value:

ulimit-n 3000

But as the hard-limit is 4000, then set a limit greater than this value, you can not. To change restriction settings for a user on a regular basis, you need to configure the file /etc/security/limits.conf. The syntax for it is:

username typoglycemia nazvaniyami value

Instead of a user name you can use asterisk for the changes to apply to all users in the system. Type constraints can be soft or hard. Name – in our case, we need nofile. And last point – the desired value. Install high – 1617596.

sudo vi /etc/security/limits.conf

* hard nofile 1617596
* soft nofile 1617596

You need to set value for soft and hard setting, if you want to make the changes take effect. Also make sure that in the file /etc/pam.d/common-session there is such a line:

session required pam_limits.so

If not, add at the end. She needs to have your restrictions to be fetched when the user logs in.

If you need to set restrictions only for certain services, such as Apache or Elasticsearch, then this is not necessary to change all settings in the system. You can do this by using systemctl. Simply run:

sudo systemctl edit service name

And paste in the opened file the following lines:

[Service]
LimitNOFILE=1617596
LimitNOFILESoft=1617596

Here we set the maximum limit for both hard – and soft-option. Then you need to close the file and update the configuration services:

sudo systemctl daemon-reload

Then restart the service:

sudo systemctl restart service name

Make sure that your service has applied the appropriate limits, by opening the file /proc/pid_сервиса/limits. First, look at the PID of the desired service:

ps aux | grep elasticsearch

Then watch information:

cat /proc/8388/limits

Insights

In this short article we discussed what to do if you get the error “too many open files Linux”, and also how to change the limits on the number of open files for the user process in Linux.

Source: losst.ru

(Visited 339 times, 1 visits today)