Bootstrap FreeKB - Linux Commands - Resolve "too many open files"
Linux Commands - Resolve "too many open files"

Updated:   |  Linux Commands articles

Let's say the following is being returned when attempting to do something on a Linux system.

Too many open files

 

The ulimit command with the -a (all) flag will display the current users limits. In this example, John Doe's limits are displayed.

[john.doe@server1 ~]# ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31119
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

 

The ulimit command followed by one of the flag listed above will return the value for only that limit. The -n flag is used to return the open files limit.

[john.doe@server1 ~]# ulimit -n
1024

 

A limit can temporarily be updated. In this example, John Doe's open files limit is temporarily updated to 2048. Typically, root or sudo is needed to make this change.

When the user signs out and then signs back into the system, the users limits will be returned to their prior value.

[root@server1 ~]# ulimit -n 2048

 

AVOID TROUBLE

ulimit: open files: cannot modify limit: Operation not permitted will be returned when attempting to set a ulimit that exceeds the hard ulimit in /etc/security/limits.conf or /etc/security/limits.d/90-nproc.conf.

To permanently update a limit, you will set the limit in the users /home/username/.bash_profile, like this.

ulimit -n 2048

 

The limits.conf file will set a hard or soft limit. 

  • /etc/security/limits.d/90-nproc.conf
  • /etc/security/limits.conf

 

Here is an example of what you would put in the limits.conf file. This would make it so that john.doe open files ulimit cannot exceed 2048. When used hard, the limit cannot be exceeded. When soft, the limit can be exceeded and a warning will be appended to /var/log/messages.

john.doe  hard  nofile  2048

 

The lsof (list open files) command can be used, as the name suggests, to list the open files on the system. The lsof command without any options should return something like this. Notice in this example that many of the files have "Permission denied". This is because the lsof command in this example is being ran by john.doe instead of root.

[john.doe@server1 ~]$ lsof
COMMAND      PID     USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
init           1     root  cwd   unknown                                        /proc/1/cwd (readlink: Permission denied)
init           1     root  rtd   unknown                                        /proc/1/root (readlink: Permission denied)
init           1     root  txt   unknown                                        /proc/1/exe (readlink: Permission denied)
init           1     root NOFD                                                  /proc/1/fd (opendir: Permission denied)
kthreadd       2     root  cwd   unknown                                        /proc/2/cwd (readlink: Permission denied)
kthreadd       2     root  rtd   unknown                                        /proc/2/root (readlink: Permission denied)
kthreadd       2     root  txt   unknown                                        /proc/2/exe (readlink: Permission denied)
kthreadd       2     root NOFD                                                  /proc/2/fd (opendir: Permission denied)
migration      3     root  cwd   unknown                                        /proc/3/cwd (readlink: Permission denied)

 

The  lsof (list open files) command can be piped through the wc (word count) command to determine how many files are open.

[john.doe@server1 ~]$ lsof | wc -l
1234

 

Often, the lsof command with the -u option is used to display the files opened by a certain user.

[john.doe@server1 ~]$ lsof -u john.doe | head
COMMAND      PID    USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
sshd      116079 john.doe cwd   unknown                                         /proc/116079/cwd (readlink: Permission denied)
sshd      116079 john.doe rtd   unknown                                         /proc/116079/root (readlink: Permission denied)
sshd      116079 john.doe txt   unknown                                         /proc/116079/exe

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 50359c in the box below so that we can be sure you are a human.