ulimit · Linux command

On this page 7

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.

InvocationEffectWho can do it
ulimit -aLists all current soft limitsAny user
ulimit -HaLists all hard limitsAny user
ulimit -Sn 4096Sets the soft open-file limitAny user, up to the hard limit
ulimit -Hn 2048Lowers the hard open-file limitAny user, cannot be undone in that shell
ulimit -n 4096Sets both soft and hard limits in bashRoot, or a user staying within the hard limit
Soft and hard limit options

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.

OptionResourceUnitTypical default
-nOpen file descriptors (RLIMIT_NOFILE)Count1024 soft
-uProcesses per real user ID (RLIMIT_NPROC)CountDerived from system memory
-sStack size of the main thread (RLIMIT_STACK)Kilobytes8192
-cCore dump file size (RLIMIT_CORE)1024-byte blocks0 on many distributions
-vVirtual address space (RLIMIT_AS)Kilobytesunlimited
-tCPU time (RLIMIT_CPU)Secondsunlimited
-lLocked memory (RLIMIT_MEMLOCK)Kilobytes8192 on recent kernels
Commonly used ulimit resources

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 methodConfiguration locationExample setting
Login sessions via PAM/etc/security/limits.conf or files in limits.dappuser soft nofile 65536
systemd servicesUnit file [Service] sectionLimitNOFILE=65536
All systemd units by default/etc/systemd/system.confDefaultLimitNOFILE=
ContainersRuntime options--ulimit nofile=65536:65536
One running processprlimit commandprlimit --pid 1234 --nofile=8192:8192
Where persistent limits are configured

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.

MechanismScopeTypical useEnforcement style
ulimitSingle process and its childrenOpen files, stack, core dumpsHard failure at the limit
cgroups v2Group of processesTotal memory, CPU share, I/OThrottling or OOM kill within the group
niceSingle processCPU scheduling preferenceRelative weight, no cap
OOM killerWhole system or cgroupRecovery from memory exhaustionKills a selected process
Resource control mechanisms in Linux

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 nofile limit is still 1024; raise it for that service.
  • Missing core dumps after a crash often result from ulimit -c 0; set ulimit -c unlimited in the test shell and check kernel.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.