Unix - User Resource Limits in Unix/Linux (ulimit)
In Unix and Linux systems, user resource limits control how much system resources a user or process can consume. The ulimit command is used to view and set these limits, helping administrators prevent runaway processes, improve system stability, and enforce fair resource usage.
What Is ulimit?
ulimit is a built-in shell command (commonly in Bash) that restricts resources such as CPU time, memory usage, number of open files, and number of processes for a user session. These limits apply per shell and to all child processes spawned from it.
Viewing Current Limits
To display all current limits:
ulimit -a
To check a specific limit:
ulimit -n # Number of open file descriptors
ulimit -u # Max user processes
ulimit -f # Max file size
Soft Limits vs Hard Limits
Unix uses two types of limits:
-
Soft limit: Enforced limit that users can temporarily increase (up to the hard limit)
-
Hard limit: Maximum allowed value, set by the administrator
Examples:
ulimit -Sn 1024 # Set soft limit for open files
ulimit -Hn 4096 # Set hard limit for open files
Only the root user can increase hard limits.
Common Resource Limits
| Option | Resource Controlled |
|---|---|
-n |
Open files |
-u |
User processes |
-v |
Virtual memory |
-m |
Physical memory |
-t |
CPU time |
-c |
Core file size |
Permanent Resource Limits
Changes made with ulimit are temporary and apply only to the current shell session. To make limits permanent, configure:
-
/etc/security/limits.conf -
/etc/security/limits.d/*.conf
Example:
username hard nofile 4096
username soft nofile 1024
These settings are enforced through PAM (pam_limits.so).
Practical Use Cases
-
Prevent users from exhausting system memory
-
Limit processes in shared hosting environments
-
Control open files for database or web server users
-
Improve overall system reliability
In summary, ulimit is a powerful mechanism for controlling resource consumption in Unix/Linux, ensuring system performance, security, and fair usage across users and applications.