Number of Files Opened
Linux allows you to control how many opened file descriptors a user can have at any one time.
System-wide settings
To show the number of files opened on a system, run:
Description | Command |
---|---|
Read the current file descriptor numbers. 3 values are:
|
# cat /proc/sys/fs/file-nr
7552 0 9223372036854775807
|
Maximum number of file descriptiors. | # cat /proc/sys/fs/file-max
9223372036854775807
|
Set the number of file descriptors | ## Via /proc
# cat 99999 > /proc/sys/fs/file-nr
## Via sysctl
# sysctl -w fs.file-max=99999
## Edit /etc/sysctl.conf so it applies on boot
# echo "fs.file-max = 99999" >> /etc/sysctl.conf
|
User session setting
On typical Linux distributions using PAM, user's session's limits are controlled by editing /etc/security/limits.conf
and are applied by the pam_limits.so
module (Enabled when using session required pam_limits.so
in the system-auth config)
A user can check their current session's limits with ulimit
on the bash shell.
Description | What |
---|---|
Max number of files opened | # ulimit -n
1024
## Soft limit
# ulimit -Sn
1024
## Hard limit
# ulimit -Hn
1024
|
Temporarily set a new limit | # ulimit -n 65536
Sets both the hard and soft limit for the shell's session. Cannot exceed the limits defined in |
To change a user's soft and hard file limits, add the following lines to /etc/security/limits.conf
:
## In /etc/security/limits.conf
username soft nofile 16384
username hard nofile 32768
The user will have to re-login for these new limits to be applied.
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
Checking if limits are set properly
Each process's limits are available under /proc/$pid/limits
. You can read this file to check whether a max file limit value is being set properly.
Other limit directives
Directive ulimit equivalent Unit
LimitCPU= ulimit -t Seconds
LimitFSIZE= ulimit -f Bytes
LimitDATA= ulimit -d Bytes
LimitSTACK= ulimit -s Bytes
LimitCORE= ulimit -c Bytes
LimitRSS= ulimit -m Bytes
LimitNOFILE= ulimit -n Number of File Descriptors
LimitAS= ulimit -v Bytes
LimitNPROC= ulimit -u Number of Processes
LimitMEMLOCK= ulimit -l Bytes
LimitLOCKS= ulimit -x Number of Locks
LimitSIGPENDING= ulimit -i Number of Queued Signals
LimitMSGQUEUE= ulimit -q Bytes
LimitNICE= ulimit -e Nice Level
LimitRTPRIO= ulimit -r Realtime Priority
LimitRTTIME= No equivalent