linux open file number too many open files solution

Keywords: lsof Ubuntu Linux socket

linux open file number too many open files solution


too many open files

The reason for this prompt is that the number of file/socket connections opened by the program exceeds the system settings.


View the maximum number of open files per user

ulimit -a

  1. fdipzone@ubuntu:~$ ulimit -a  
  2. core file size          (blocks, -c) 0  
  3. data seg size           (kbytes, -d) unlimited  
  4. scheduling priority             (-e) 20  
  5. file size               (blocks, -f) unlimited  
  6. pending signals                 (-i) 16382  
  7. max locked memory       (kbytes, -l) 64  
  8. max memory size         (kbytes, -m) unlimited  
  9. open files                      (-n) 1024  
  10. pipe size            (512 bytes, -p) 8  
  11. POSIX message queues     (bytes, -q) 819200  
  12. real-time priority              (-r) 0  
  13. stack size              (kbytes, -s) 8192  
  14. cpu time               (seconds, -t) unlimited  
  15. max user processes              (-u) unlimited  
  16. virtual memory          (kbytes, -v) unlimited  
  17. file locks                      (-x) unlimited  

Where open files (-n) 1024 indicates that the maximum number of files allowed to open per user is 1024


View the number of files open on the current system

  1. lsof | wc -l  
  2. watch "lsof | wc -l"  

View the number of open files for a process

  1. lsof -p pid | wc -l  
  2. lsof -p 1234 | wc -l  

Setting the open files numerical method

ulimit -n 2048

  1. fdipzone@ubuntu:~$ ulimit -n 2048  
  2. fdipzone@ubuntu:~$ ulimit -a  
  3. core file size          (blocks, -c) 0  
  4. data seg size           (kbytes, -d) unlimited  
  5. scheduling priority             (-e) 20  
  6. file size               (blocks, -f) unlimited  
  7. pending signals                 (-i) 16382  
  8. max locked memory       (kbytes, -l) 64  
  9. max memory size         (kbytes, -m) unlimited  
  10. open files                      (-n) 2048  
  11. pipe size            (512 bytes, -p) 8  
  12. POSIX message queues     (bytes, -q) 819200  
  13. real-time priority              (-r) 0  
  14. stack size              (kbytes, -s) 8192  
  15. cpu time               (seconds, -t) unlimited  
  16. max user processes              (-u) unlimited  
  17. virtual memory          (kbytes, -v) unlimited  
  18. file locks                      (-x) unlimited  

In this way, the maximum number of open files allowed by the current user can be set to 2048, but this setting method will be restored to the default value after reboot.


Permanent Setup Method

  1. vim /etc/security/limits.conf  
  2. At the end of the day.
  3. * soft nofile 4096  
  4. * hard nofile 4096  
The first * represents all users and can set up a user as needed, for example

  1. fdipzone soft nofile 8192  
  2. fdipzone hard nofile 8192  
It will take effect after the change is cancelled.

Posted by neorunner on Sun, 10 Feb 2019 21:03:18 -0800