ulimit
Limit resource usage
TLDR
Get the properties of all the user limits
Get hard limit for the number of simultaneously opened files
Get soft limit for the number of simultaneously opened files
Set max per-user process limit
Display help (Bash only)
SYNOPSIS
ulimit [-HSacdfiklmnpqrstuvx] [limit]
PARAMETERS
-H
Sets or displays the hard limit. Hard limits can only be increased by the root user.
-S
Sets or displays the soft limit (default). Soft limits can be increased up to the hard limit by non-root users.
-a
Displays all current soft resource limits.
-c
Core file size (in 512-byte blocks).
-d
Data segment size (in kilobytes).
-f
File size (in 512-byte blocks) for files created by the shell and its children.
-i
The maximum number of pending signals.
-k
The maximum number of Kbytes that may be locked into memory (obsolete, use -l).
-l
The maximum size (in kilobytes) that may be locked into memory.
-m
The maximum resident set size (in kilobytes).
-n
The maximum number of open file descriptors.
-p
The pipe size (in 512-byte blocks).
-q
The maximum number of bytes in POSIX message queues.
-r
The maximum real-time priority.
-s
The maximum stack size (in kilobytes).
-t
The maximum amount of CPU time (in seconds) used by each process.
-u
The maximum number of processes available to a single user.
-v
The maximum amount of virtual memory (in kilobytes) available to the shell and its children.
-x
The maximum number of file locks.
DESCRIPTION
ulimit (user limit) is a built-in shell command in Unix-like operating systems that allows you to view or set resource limits for the current user's processes. These limits dictate the maximum amount of system resources that a process or group of processes can consume. Resources include CPU time, file size, data segment size, stack size, core file size, the number of open files, and the number of processes.
The command operates on two types of limits: soft limits and hard limits. Soft limits are the current, effective limits that can be increased by a user up to the corresponding hard limit. Hard limits represent the maximum value a soft limit can take and can only be increased by the root user.
Setting appropriate ulimit values is crucial for system stability and security. It prevents single processes or users from consuming excessive resources, which could degrade system performance or lead to denial-of-service. Common uses include preventing runaway processes from filling up disk space with large files or consuming all available memory. Limits set with ulimit apply to the current shell and any processes started from it. For persistent system-wide limits, configuration files like /etc/security/limits.conf are typically used.
CAVEATS
Limits set by ulimit are inherited by child processes but are not persistent across reboots or new login sessions unless explicitly configured. Non-root users can only decrease hard limits or increase soft limits up to the current hard limit; only the root user can increase hard limits. Some resource limits might be system-wide and not configurable via ulimit directly, or might be constrained by kernel parameters.
UNITS OF MEASUREMENT
The unit for the limit value varies depending on the resource being controlled:
-f (file size) and -c (core file size) are in 512-byte blocks.
-d (data segment), -s (stack), -l (locked memory), -m (resident set), and -v (virtual memory) are in kilobytes.
-t (CPU time) is in seconds.
Other options like -n (open files), -u (processes), -i (signals), and -x (file locks) are raw counts. The keyword 'unlimited' can be used to remove a limit.
MAKING LIMITS PERSISTENT
To make ulimit settings persist for an individual user across sessions, add the commands to your shell's initialization files (e.g., ~/.bashrc or ~/.profile for Bash). For system-wide persistent limits affecting all users or specific groups, configure the /etc/security/limits.conf file, which works in conjunction with the PAM (Pluggable Authentication Modules) framework.
HISTORY
The concept of resource limits, upon which ulimit is built, has been a fundamental part of Unix-like operating systems since early versions (e.g., BSD 4.2), tied closely to the getrlimit and setrlimit system calls. ulimit emerged as a convenient built-in command in popular shells like bash and ksh, standardizing the management of process resources directly from the command line for users and system administrators alike.