How to configure the process open file limit of a user?
On Linux you can configure it via limits.conf
, e.g. via
# cd /etc/security
# echo debian-transmission - nofile 8192 > limits.d/transmission.conf
(which sets both the hard and soft limit for processes started under the user debian-transmission to 8192)
You can verify the change via:
# sudo -u debian-transmission bash -c "ulimit -a"
[..]
open files (-n) 8192
[..]
If a daemon is already running, it has to be restarted such that the new limit is picked up. In case the daemon is manually started from a user session, the user has to re-login to get the new limit.
Alternatively, you can also specify additional limits directly in /etc/security/limits.conf
, of course - but I prefer the .d
directory approach for better maintainability.
For enforcing different soft/hard limits use two entries, e.g.
debian-transmission soft nofile 4096
debian-transmission hard nofile 8192
(rationale behind this: the soft value is set after the user logs in but a users process is allowed to increases the limit up to the hard limit)
The limits.conf
/limits.d
configuration is used by pam_limits.so
, which is enabled by default on current Linux distributions.
Related
There is also a system-wide limit on Linux, /proc/sys/fs/file-max
:
This file defines a system-wide limit on the number of open files for all processes.
For example the default on Ubuntu 10.04:
$ cat /proc/sys/fs/file-max
786046
The pseudo file /proc/sys/fs/file-nr
provides more information, e.g.
$ cat /proc/sys/fs/file-nr
1408 0 786046
the number of allocated file handles (i.e., the number of files presently opened); the number of free file handles; and the maximum number of file handles
Thus, on the one hand, you also may have to adjust system-wide file-max
limit, in case its is very small and/or the system is already very loaded. On the other hand, just increasing file-max
is not sufficient, because it does not influence the soft/hard limits enforced by the pam_limits mechanism.
To change file-max
on the command line (no reboot necessary):
# sysctl -w fs.file-max=786046
For permanent changes add fs.file-max=786046
to /etc/sysctl.conf
or /etc/sysctl.d
.
The upper limit on fs.file-max
is recorded in fs.nr_open
. For example, (again) on Ubuntu 10.04:
$ sysctl -n fs.nr_open
1048576
(which is 1024*1024)
This sysctl is also configurable.
Related videos on Youtube
maxschlepzig
My name is Georg Sauthoff. 'Max Schlepzig' is just a silly old pseudonym (I am hesitant to change it because existing @-replies will not be updated) I studied computer science In my current line of work, I work on trading system software and thus care about low-latency
Updated on September 18, 2022Comments
-
maxschlepzig over 1 year
The default open file limit per process is 1024 on - say - Linux. For certain daemons this is not enough. Thus, the question: How to change the open file limit for a specific user?
-
Liczyrzepa about 7 yearsThank you for the last bit of information - I was scratching my head trying to figure out why I couldn't set a user nofile limit greater than 1048576 in /etc/security/limits.conf