Saturday, January 11, 2014

CentOS/RHEL: Ulimit And Maximum Number Of Open Files

Sometimes you may want to set limits on some of your system resources(Processes, Files)
But other times, you may want to do the just the opposite - disable those limits or change them to higher limits like I needed to do few days ago.

1. Check what are the limits

Apparently, RedHat set the default max number of open files for users  to 1024 and and not long ago, this was change to 4096.
For some applications/users this is very low and can cause you a lot of problems.

You can check what is the current limit with ulimit using bash shell:
ulimit -Hn
This will show you the hard limit of maximum number of files for your user. you can use -S to check the soft limit.
tcsh: using tcsh you can can check this limit with limit descriptors

if you wish to check the max Open file descriptors for the process your user/application running, use the following command: 
cat /proc/[Process ID]/limits

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

cat /proc/sys/fs/file-max

2. Change the limit

lets start by changing the limit for the user mario:
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 section 1.
If you want to set maximum number of processes use nproc instead of nofile
If you want to set this setting to all users use * instead of specify user name

Now lets change the limit for the entire system:
vi /etc/sysctl.conf
and put the line:
fs.file-max = 200500

update the system with::
sysctl -p

3. List number of open files/allocated file handles

To check the number of files opened on your system, use:
lsof | wc -l
You can also check the allocated file descriptors by using:
cat /proc/sys/fs/files-nr
the first field is the number of total allocated files descriptors.
second field is unused file descriptors
and the third field is the maximum file descriptors that can be used


Laxmi said...

Im using Amazon Linux...

But when I do cat /proc/sys/fs/files-nr

I'm getting file not found

Anonymous said...

cat /proc/sys/fs/file-nr

promoter said...
This comment has been removed by the author.

Post a Comment