Number of Files Opened

From Leo's Notes
Revision as of 16:54, 4 October 2022 by Leo (talk | contribs)
This page was last edited on 4 October 2022, at 16:54.

Linux allows you to control how many opened file descriptor an user can have at any one time.

System-Wide Settings

To show the number of files opened on a system, run:

# cat /proc/sys/fs/file-nr
1536	0	793120

The fields are:

  1. Current number of opened files
  2. Number of free allocated file descriptors
  3. Maximum number of file descriptors for the whole system

You may change this value by:

# cat <new value> > /proc/sys/fs/file-nr
## Edit <code>/etc/sysctl.conf</code> so that there is a line
## fs.file-max = <new value>

Per-User Settings

You can see the limits set by running ulimit on the bash shell.

# ulimit -n
1024

This tells you that the maximum number of files you can have in this session is 1024 files.

For normal shell login sessions, you can specify these limits in /etc/security/limits.conf. To change a user's soft and hard file limits, add the following lines:

## In /etc/security/limits.conf
username soft nofile 16384
username hard nofile 32768

Systemd service

Services that are started by systemd will not adhere to the limits specified in the /etc/security/limits.conf file (because those are set by PAM?). You will have to change the service file and specify the limits there instead.

For example:

[Unit]
Description=Galaxy Web Service
After=network.target

[Service]
Type=simple
User=galaxy
ExecStart=/galaxy/run.sh
# ExecStop=/galaxy/run.sh stop
# ExecReload=/galaxy/run.sh restart
Restart=always

LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

See Also