ulimit is a Linux command built into the shell that displays and changes the resource limits applied to the shell and every process it starts. In Linux process management it controls ceilings such as the number of open file descriptors, the maximum stack size, the size of core dumps and the number of processes a user may run.
Because ulimit is a shell builtin rather than a separate program, it changes the limits of the current shell session, and those limits are then inherited by child processes created with fork and exec. It is the interactive face of the kernel system calls getrlimit() and setrlimit().
How ulimit applies kernel resource limits
Every Linux process carries a table of resource limits (rlimits) stored in its kernel task structure and visible in /proc/PID/limits. When ulimit runs, the shell calls setrlimit() on itself, and the kernel enforces the new value whenever the process, or any descendant, tries to allocate that resource.
Each resource has two values. The soft limit is the value the kernel actually enforces, and the hard limit is the ceiling up to which an unprivileged process may raise its soft limit. A regular user can raise the soft limit up to the hard limit and can lower the hard limit irreversibly, while raising a hard limit requires root or the CAP_SYS_RESOURCE capability.
The table below shows how the main options select soft or hard values.
| Invocation | Effect | Who can do it |
|---|---|---|
ulimit -a | Lists all current soft limits | Any user |
ulimit -Ha | Lists all hard limits | Any user |
ulimit -Sn 4096 | Sets the soft open-file limit | Any user, up to the hard limit |
ulimit -Hn 2048 | Lowers the hard open-file limit | Any user, cannot be undone in that shell |
ulimit -n 4096 | Sets both soft and hard limits in bash | Root, or a user staying within the hard limit |
Key resources ulimit controls
Bash exposes about sixteen resources, each selected by a single-letter option. Units differ by resource: most sizes are in kilobytes, CPU time is in seconds, and counts such as open files are plain numbers. The value unlimited removes the ceiling where the hard limit allows it.
| Option | Resource | Unit | Typical default |
|---|---|---|---|
| -n | Open file descriptors (RLIMIT_NOFILE) | Count | 1024 soft |
| -u | Processes per real user ID (RLIMIT_NPROC) | Count | Derived from system memory |
| -s | Stack size of the main thread (RLIMIT_STACK) | Kilobytes | 8192 |
| -c | Core dump file size (RLIMIT_CORE) | 1024-byte blocks | 0 on many distributions |
| -v | Virtual address space (RLIMIT_AS) | Kilobytes | unlimited |
| -t | CPU time (RLIMIT_CPU) | Seconds | unlimited |
| -l | Locked memory (RLIMIT_MEMLOCK) | Kilobytes | 8192 on recent kernels |
The -m option for resident set size is accepted but ignored by modern Linux kernels, a frequent source of confusion. Exceeding a limit produces a specific outcome: EMFILE errors for file descriptors, SIGXCPU then SIGKILL for CPU time, a segmentation fault for stack overflow, and EAGAIN from fork for the process limit.
Making limits persistent
A value set with ulimit lasts only for the current shell and its children. Persistent limits must be configured at the point where sessions or services start, and the right place depends on how the process is launched.
| Launch method | Configuration location | Example setting |
|---|---|---|
| Login sessions via PAM | /etc/security/limits.conf or files in limits.d | appuser soft nofile 65536 |
| systemd services | Unit file [Service] section | LimitNOFILE=65536 |
| All systemd units by default | /etc/systemd/system.conf | DefaultLimitNOFILE= |
| Containers | Runtime options | --ulimit nofile=65536:65536 |
| One running process | prlimit command | prlimit --pid 1234 --nofile=8192:8192 |
A common mistake is editing limits.conf for a daemon that systemd starts. pam_limits applies only to PAM sessions, so services ignore that file and need LimitNOFILE= or a related directive in their unit. After the change, cat /proc/PID/limits confirms the values the running process actually received.
ulimit compared with cgroups and nice
ulimit sets per-process ceilings, with the exception of the process count, which is tracked per user. Control groups (cgroups) limit the combined usage of a group of processes, while nice and process priority influence scheduling without imposing a hard cap.
| Mechanism | Scope | Typical use | Enforcement style |
|---|---|---|---|
| ulimit | Single process and its children | Open files, stack, core dumps | Hard failure at the limit |
| cgroups v2 | Group of processes | Total memory, CPU share, I/O | Throttling or OOM kill within the group |
| nice | Single process | CPU scheduling preference | Relative weight, no cap |
| OOM killer | Whole system or cgroup | Recovery from memory exhaustion | Kills a selected process |
In practice the two are complementary: cgroups protect the host from a runaway service, while ulimit guards against single-process mistakes such as a descriptor leak or an infinite recursion that would otherwise exhaust memory.
Common problems and practical advice
- “Too many open files” errors in databases, web servers and message brokers usually mean the soft
nofilelimit is still 1024; raise it for that service. - Missing core dumps after a crash often result from
ulimit -c 0; setulimit -c unlimitedin the test shell and checkkernel.core_pattern. - sudo and su do not keep your ulimit values, because a new PAM session applies its own limits.
- Setting -v too low breaks programs that reserve large virtual address ranges, such as Java virtual machines and AddressSanitizer builds.
- ulimit in a script affects only that script and the commands it starts, not the parent terminal.
A sound approach is to measure actual usage first, for example with ls /proc/PID/fd | wc -l, then set limits with comfortable headroom rather than copying very large values that hide real leaks.
The simple explanation
Imagine every program on a computer receives a set of house rules when it starts: how many files it may keep open at once, how much scratch space it may use and how long it may run. ulimit is the command that reads and changes those house rules.
The rules pass from a parent to its children, like a family budget. Anyone can tighten the budget, but only the administrator can raise the absolute maximum. The limits protect the system from one program using too much.
How it is used in practice
A system administrator runs a busy reverse proxy that handles about 20,000 simultaneous connections. After a traffic peak, the error log fills with “Too many open files” messages, and some visitors see failed requests.
Checking /proc/PID/limits for the proxy process shows a soft limit of 1,024 open files. Each client connection and each upstream connection uses a descriptor, so the process hit the ceiling quickly. The administrator first tests the theory in a shell with ulimit -n 65536 and a manual start, then adds LimitNOFILE=65536 to the systemd unit and reloads it.
After the restart, the proxy reports the new limit and handles the next peak without a single descriptor error. The administrator also adds a monitoring alert at 80 percent of the limit so that a future leak is detected long before it affects users.