Increase file descriptors

Linux manages open files as well as open network connections via a file descriptor table (network connections are also files in Linux). And sometimes the default size of that table is not sufficient.

I'll explain how to modify that table size in this tutorial. Also I tested it using Ubuntu Linux 18, this should work for pretty much every Linux distribution and version.

The current limits can be obtained via the following commands:

root@machine:~# ulimit -n ; ulimit -Hn ; ulimit -Sn
1024
1048576
1024

First is the soft limit (which is the default limit ulimit shows, the second is the hard limit and the last one is the soft limit (this time explicitly).

About the difference between soft limits and hard limits I found the following explanation (Actually from a manual for a UNIX version, but also applies for Linux):

The soft limits are the ones that actually affect processes; hard limits are the
maximum values for soft limits. Any user or process can raise the soft limits up
to the value of the hard limits. Only processes with superuser authority can
raise the hard limits.

Now we edit the configuration:

root@machine:~# nano /etc/security/limits.conf

The default file shipped with Ubuntu 18 should look like this:

# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#        - chroot - change root to directory (Debian-specific)
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#root            hard    core            100000
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#ftp             -       chroot          /ftp
#@student        -       maxlogins       4

# End of file

Between the example and the # End of file comment we add the following lines:

*                hard    nofile          8192
*                soft    nofile          8192
root             hard    nofile          8192
root             soft    nofile          8192

This will change the soft and hard limit for all users as well as the root (which also includes the processes / services running as root) to 8192.

In our case (Ubuntu 18) this will increase the soft limit from 1024 to 8192, but will decrease the hard limit from 1048576 to 8192 - at least for the root user.