File Descriptors and Open Files Limit CentOS 7
Some programs like Apache and MySQL requires a higher number of file descriptors.By default its 1024, which is not enough for current high load servers. Which will give you error in apache like “Fatal Error Unable to create lock file: Bad file descriptor (9)“. To solve this problem, here are the steps to increase this limit.
Common errors we get with file descriptor limits:
a) Fatal Error Unable to create lock file: Bad file descriptor (9)
b) Too many open files (24)
c) Unable to lock file
I) Check default limit:
$ ulimit -a
default will be 1024.
check global limit:
$ cat /proc/sys/fs/file-max
if you get output like 10000 that means normal user can only open 10000 files in a single login session. To see the hard and soft values,
Hard limit:
$ ulimit -Hn # To check by user $ su <user> $ ulimit -Hn
Soft Limit:
$ ulimit -Sn # To check by user $ su <user> $ulimit -Sn
II) Changing system-wide file descriptors limits:
a) increase global limit:
$ vi /etc/sysctl.conf # edit line fs.file-max = 100000 #set this value as you need
b) Also need to increase hard and soft limit.
$ vi /etc/security/limits.conf
Add / Edit following lines.
* soft nproc 10000 * hard nproc 10000 * soft nofile 10000 * hard nofile 10000
Note: If we need to increase limit by user then we need to replace * with username. For example, to increase limit by user apache we do as follows.
apache soft nproc 10000 apache hard nproc 10000 apache soft nofile 10000 apache hard nofile 10000
c) reload systl
$ sysctl -p $ systemctl daemon-reload
d) Verify New Limits
Use following command to see max limit of file descriptors:
cat /proc/sys/fs/file-max
Hard Limit
ulimit -Hn
Soft Limit
ulimit -Sn