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.
Best Answer
ulimit -n
sets the soft limit by default; you can add the-H
option to view/set the hard limit.For the most part, soft and hard limits behave like this:
CAP_SYS_RESOURCE
) may raise or lower any limit on any process.So, hard limits function as a cap on soft limits (except for root, who as normal can do anything).
There is an exception: A soft CPU limit sends a
SIGXCPU
signal. A process may choose to ignore that, or spend time doing cleanup, etc. Once the hard CPU limit is crossed, the kernel sendsSIGKILL
—which is not catchable, handleable, or ignorable. So in this case, the soft limit functions as a warning "you're out of CPU time—finish up and exit promptly, or else!" and the hard limit is the "or else."Most are per-process, but a few (such as
RLIMIT_NPROC
) are per user. The getrlimit(2) manual page specifies for each limit.