Linux: Ulimit And Maximum Number Of Open Files

Note: this is an old post from my previous blog

 

In this post, I’m going to show you how to raise the limit on the number of open files in your system. I assume in this post that you are using CentOS operating system.

List the limits

Apparently, CentOS has a default of 4096 number of open files limit. For some applications, this is simply not enough and you may want to raise this number.
You can check what is the current limit with the ulimit command

ulimit -Hn

This will show you the hard limit of the maximum number of open files for your user. You can also use -S to check the soft limit.

Note: using tcsh you can check this limit with limit descriptors
if you wish to check the max open file descriptors for a specific process, use the following command: 

cat /proc/[Process ID]/limits

To check what is the system limit for the number of files descriptors, use the following command:

cat /proc/sys/fs/file-max

Change the limit

Let’s change the limit for the user mario. All the limits are configured in /etc/security/limits.conf

vi /etc/security/limits.conf

Now change the following lines:

mario soft nofile 4096
mario hard nofile 20480

save the file and verify the result by using ulimit like we used it in the first section.
Note: If you want to set maximum number of processes use nproc instead of nofile
Note 2: If you want to set this setting to all users use * instead of specify user name

Let’s also change the limit for the entire system. For this we’ll edit /etc/sysctl.conf

vi /etc/sysctl.conf

Put the following line in the file

fs.file-max = 200500

Now update the system with the following command

sysctl -p

List the number of open files

To check the number of open files on your system, use the lsof command

lsof | wc -l

You can also check the allocated file descriptors by using:

cat /proc/sys/fs/files-nr

The first field in the output is the number of total allocated files descriptors. The second field is unused file descriptors and finally, the third field is the maximum file descriptors that can be used

One thought on “Linux: Ulimit And Maximum Number Of Open Files

  1. Just wanted to point out that the command to check the allocated file descriptors is:

    cat /proc/sys/fs/file-nr

    instead of

    cat /proc/sys/fs/files-nr

    Great article though, helped me alot!!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s