ulimit: How to permanently set kernel limits in Linux?


ulimit provides control over the resources available to the shell and to processes started by it, on systems that allow such control.

Usually, you have to increase the values of some of the Linux kernel limits before your install or run many applications.

With ulimit you can set two kind of limits:

1. Soft limit: is the value that the kernel enforces for the corresponding resource.
2. Hard limit:
acts as a ceiling for the soft limit.

An unprivileged process may only set its soft limit to a value in the range from 0 up to the hard limit, and (irreversibly) lower its hard limit. A privileged process may make arbitrary changes to either limit value.

Checking the values of the kernel limits

# ulimit -a
core file size (blocks, -c) 1
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 59517
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) 6695364
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 59517
virtual memory (kbytes, -v) 6301520
file locks (-x) unlimited

Changing temporally the value of a limit

With ulimit you can change the value of one limit for the current shell:

# ulimit -n 16384

This command sets the maximum number of opened files to 16384 for the current shell.

Making permanent changes to a limit

Linux servers have a PAM (plugabble authentication) module that handles system limits:

“By default limits are taken from the /etc/security/limits.conf config file. Then individual files from the /etc/security/limits.d/ directory are read. The files are parsed one after another in the order of “C” locale. The effect of the individual files is the same as if all the files were concatenated together in the order of parsing. If a config file is explicitely specified with a module option then the files in the above directory are not parsed.”

In our example we will set the maximum number of files to 16384 for all users of the system (values can be set for individual users or groups as well):

# vi /etc/security/limits.conf

Add two lines for each limit:

* soft nofile 16384
* hard nofile 16384

Reboot your machine and test the new limits configuration:

# ulimit -a
core file size (blocks, -c) 1
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 59517
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) 6695364
open files (-n) 16384
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 59517
virtual memory (kbytes, -v) 6301520
file locks (-x) unlimited

2 comments

  1. […] got something here http://singztechmusings.wordpress.co…mits-in-linux/ and not more error of Too many open […]

  2. resources limits stated in limits.conf are not pernament ! here is a quote from ‘man limits.conf’:

    “Also, please note that all limit settings are set per login. They are not global, nor are they permanent; existing only for the duration of the session.”

Leave a comment