Ubuntu Linux ulimit

Introduction

ulimit is a built-in shell command used to display and set various limits on the shell's resource usage.

Among the limits that can be set, you will find the maximum file size, the maximum core file size, and the maximum resident memory size.

Although the limits do not depend on the shell, the exact syntax depends on which shell you use.

It is recommended to set some of these limits so that, for example, a faulty shell script does not start an unlimited number of copies of itself, or so that users on the system do not start processes that run forever.

nproc

You often need to run many processes at the same time.

For example, for load testing

You can see how many processes (nproc) a user is using with the command

ps -lfu $(whoami) | wc -l

For example:

ps -lfu root | wc -l

266

ps -lfu andrei | wc -l

8

The limit values can be found by running

ulimit -a

-t: cpu time (seconds) unlimited -f: file size (blocks) unlimited -d: data seg size (kbytes) unlimited -s: stack size (kbytes) 8192 -c: core file size (blocks) 0 -m: resident set size (kbytes) unlimited -u: processes 126675 -n: file descriptors 1024 -l: locked-in-memory size (kbytes) 65536 -v: address space (kbytes) unlimited -x: file locks unlimited -i: pending signals 126675 -q: bytes in POSIX msg queues 819200 -e: max nice 0 -r: max rt priority 0 -N 15: unlimited

You can try to change the limit by editing the file limits.conf

sudo vi /etc/security/limits.conf

# /etc/security/limits.conf # #Each line describes a limit for a user in the form: # #<domain> <type> <item> <value> # #Where: #<domain> can be: # - a user name # - a group name, with @group syntax # - the wildcard *, for default entry # - the wildcard %, can be also used with %group syntax, # for maxlogin limit # - NOTE: group and wildcard limits are not applied to root. # To apply a limit to the root user, <domain> must be # the literal username root. # #<type> can have the two values: # - "soft" for enforcing the soft limits # - "hard" for enforcing hard limits # #<item> can be one of the following: # - core - limits the core file size (KB) # - data - max data size (KB) # - fsize - maximum filesize (KB) # - memlock - max locked-in-memory address space (KB) # - nofile - max number of open file descriptors # - rss - max resident set size (KB) # - stack - max stack size (KB) # - cpu - max CPU time (MIN) # - nproc - max number of processes # - as - address space limit (KB) # - maxlogins - max number of logins for this user # - maxsyslogins - max number of logins on the system # - priority - the priority to run user process with # - locks - max number of file locks the user can hold # - sigpending - max number of pending signals # - msgqueue - max memory used by POSIX message queues (bytes) # - nice - max nice priority allowed to raise to values: [-20, 19] # - rtprio - max realtime priority # - chroot - change root to directory (Debian-specific) # #<domain> <type> <item> <value> # #* soft core 0 #root hard core 100000 #* hard rss 10000 #@student hard nproc 20 #@faculty soft nproc 20 #@faculty hard nproc 50 #ftp hard nproc 0 #ftp - chroot /ftp #@student - maxlogins 4 # End of file

nofile

You can increase the limit on the number of open files in a similar way

sudo cat /etc/systemd/system.conf | grep DefaultLimitNOFILE

#DefaultLimitNOFILE=1024:524288

Change it to the value you need

Log in to the terminal again

ulimit -a

-t: cpu time (seconds) unlimited -f: file size (blocks) unlimited -d: data seg size (kbytes) unlimited -s: stack size (kbytes) 8192 -c: core file size (blocks) 0 -m: resident set size (kbytes) unlimited -u: processes 126675 -n: file descriptors 1024 -l: locked-in-memory size (kbytes) 65536 -v: address space (kbytes) unlimited -x: file locks unlimited -i: pending signals 126675 -q: bytes in POSIX msg queues 819200 -e: max nice 0 -r: max rt priority 0 -N 15: unlimited

You can learn about all the possible options by running

man limits.conf

Or by reading the article limits.conf

Find the maximum

You can find the maximum possible limit by running

ulimit -Hn

1048576

Article author: Andrei Olegovich