Hello! When operating a Linux server, there are moments that can make your heart race. Imagine a situation where a single faulty script executed by one user consumes all of the server’s CPU or memory, bringing the entire system to a halt. π± Especially, a ‘fork bomb’ attack, which infinitely replicates itself, is terrifying just to imagine.
To prevent such a catastrophe, User Resource Limits are essential. They act as an invisible firewall that proactively prevents specific users or groups from excessively using system resources. Today, we’ll delve into everything about this powerful feature!

1. Two Faces of Limits: Soft Limit vs. Hard Limit π¦
There are two types of Linux resource limits. Understanding the difference between them is crucial.
Soft Limit
As the name suggests, it’s a gentle ‘warning line’ or ‘recommended limit’. Users receive a warning when they reach this limit, but they can temporarily raise the limit themselves within the hard limit range.
Hard Limit
This is an absolute ‘physical wall’ or ‘maximum limit’ that cannot be exceeded. Once set, only the administrator (root) can raise this limit, and regular users can never exceed this value.
π³ Credit Card Analogy: A soft limit is like a self-imposed budget goal, “I’ll only spend 1 million won this month.” On the other hand, a hard limit is like the ‘total credit limit of 5 million won’ set by the credit card company, meaning you can’t spend more even if you want to.
2. Where and How? Exploring Resource Limit Configuration Files π οΈ
These limits are configured in the /etc/security/limits.conf file, or, more preferably, by creating a new .conf file within the /etc/security/limits.d/ directory (e.g., 99-custom.conf).
The syntax of the configuration file is very simple, defining one rule per line.
# <domain> <type> <item> <value>
: The target to which the rule applies. - username: Specific user name (e.g., john)
- @groupname: Specific group (e.g., @developers)
- *: All users
: The type of limit. - soft: Soft limit
- hard: Hard limit
- : The type of resource to limit.
- nproc: Maximum number of processes – Essential for preventing ‘fork bombs’!
- fsize: Maximum size of files that can be created (unit: KB). Prevents log files from growing indefinitely.
- nofile: Maximum number of open files – Very important for web servers or DB servers.
- cpu: Maximum CPU time (unit: minutes)
- memlock: Maximum memory space that can be locked (unit: KB)
: The limit value.
3. Practical Examples: How to Use It π
Below are practical examples. You can save this content to a file like /etc/security/limits.d/99-my-limits.conf.
# All users in the 'developers' group can have a maximum of 100 processes (soft/hard same)
@developers - nproc 100
# User 'jane' can only create files up to 5GB in size (hard)
jane hard fsize 5242880 # 5 * 1024 * 1024 KB
# Limit 'john' user's open file count to soft 50000, hard 60000
john soft nofile 50000
john hard nofile 60000
# All users cannot use CPU for more than 1000 minutes
* hard cpu 1000
4. Configuration Done! Time to Verify β
After completing the configuration, two important steps remain.
1. Re-login is essential!
limits.conf settings are applied by the pam_limits.so module when a user logs in anew. Therefore, if you have changed the settings, the user must log out and log in again.
2. Verify with the ulimit command
A logged-in user can check the limits applied to them using the ulimit command.
- Check all limits:
`
ulimit -a
`
- Check maximum number of processes (soft/hard):
`
Check Soft limit (-Su)
ulimit -Su
Check Hard limit (-Hu)
ulimit -Hu
`
- Check maximum file size (soft):
`
Check Soft limit (-Sf)
ulimit -Sf
`
Conclusion: An Essential Habit for a Stable Server
User resource limits are not just a feature to ‘control’ users. They are a professional system administration technique that protects the entire system from unexpected mistakes or attacks, allocates fair resources to all users, and enhances server stability and reliability.
Set appropriate resource limits on your server right now and free yourself from sleepless nights caused by failures! π
Tags:
Linux, ulimit, limits.conf, resource management, system administration, security, nproc, fsize, DevOps, server stability
Leave a Reply