Ulimit in Linux controls how many processes a single user can run simultaneously. If you’ve ever hit a “cannot fork” error or had an application crash due to resource limits, you need to know how to set ulimit in Linux. This guide walks you through the entire process step by step.
Think of ulimit as a safety valve. It prevents one user from hogging all system resources. Without it, a runaway process could freeze your entire server. Setting these limits correctly keeps your system stable and your applications running smoothly.
You don’t need to be a Linux guru to manage ulimits. With a few commands and configuration file edits, you can control open files, processes, memory, and more. Let’s get started.
What Is Ulimit And Why It Matters
Ulimit stands for “user limits.” It’s a built-in Linux shell command that sets soft and hard resource restrictions for user sessions. Soft limits can be increased by the user up to the hard limit. Hard limits are the absolute maximum set by the system administrator.
Common resources controlled by ulimit include:
- Maximum number of open file descriptors
- Maximum number of processes
- Maximum stack size
- Maximum core file size
- Maximum memory usage
Without proper ulimit settings, high-traffic web servers or databases may fail. For example, a web server handling thousands of connections needs many open files. If the limit is too low, the server rejects new connections.
How To Set Ulimit In Linux
Now let’s get into the core of this guide. There are two main ways to set ulimits: temporarily with the ulimit command, and permanently by editing configuration files. We’ll cover both methods.
Checking Current Ulimit Values
Before you change anything, see what’s currently set. Open a terminal and run:
ulimit -a
This shows all current soft limits. To see hard limits, use:
ulimit -a -H
Look for lines like “open files” and “max user processes.” These are the values you’ll likely adjust.
Setting Ulimit Temporarily For The Current Session
For quick tests, use the ulimit command directly. Changes last only until you close the terminal. To set the maximum number of open files to 4096:
ulimit -n 4096
To set the maximum number of processes to 1024:
ulimit -u 1024
Remember, you can only set values up to the hard limit. If you get an error, check the hard limit first.
Setting Ulimit Permanently For A User
Permanent changes require editing configuration files. The most common method is editing /etc/security/limits.conf. This file applies to PAM (Pluggable Authentication Modules) sessions.
Open the file with root privileges:
sudo nano /etc/security/limits.conf
Add lines in this format:
<domain> <type> <item> <value>
For example, to set open files for user “john”:
john soft nofile 4096
john hard nofile 8192
To set max processes for the same user:
john soft nproc 1024
john hard nproc 2048
You can also use wildcards. To apply limits to all users, use * as the domain:
* soft nofile 4096
* hard nofile 8192
After saving the file, log out and log back in for changes to take effect. Verify with ulimit -a.
Setting Ulimit Permanently Via Systemd
If your system uses systemd (most modern Linux distros do), you need to set limits differently for services. Edit the service’s override file:
sudo systemctl edit <service-name>
Add lines like:
[Service]
LimitNOFILE=4096
LimitNPROC=1024
Then reload and restart the service:
sudo systemctl daemon-reload
sudo systemctl restart <service-name>
This method is essential for services like Nginx, Apache, or PostgreSQL.
Setting Ulimit For The Root User
Root user ulimits are often set in /etc/security/limits.conf as well. However, some systems ignore this for root. To ensure root limits apply, add the root domain:
root soft nofile 65536
root hard nofile 65536
You may also need to edit /etc/pam.d/common-session or /etc/pam.d/login to include:
session required pam_limits.so
This line is usually present by default, but check if you face issues.
Common Ulimit Parameters Explained
Here are the most frequently adjusted ulimit parameters. Each controls a specific resource.
Open File Descriptors (Nofile)
This limits how many files a process can open simultaneously. Web servers, databases, and file servers often need high values. A typical production server might set this to 65536 or higher.
Max User Processes (Nproc)
This controls the number of processes a single user can create. If you run many background jobs or containers, increase this value. Default is often 1024, but heavy users may need 4096 or more.
Stack Size (Stack)
Stack size per thread. Large stack sizes can waste memory. For most applications, the default 8192 KB is fine. Some applications, like deep learning tools, may need more.
Core File Size (Core)
Limits the size of core dump files. Set to 0 to disable core dumps, or unlimited for debugging. In production, you usually disable them to save disk space.
Virtual Memory (As)
Limits the address space a process can use. Useful for preventing memory leaks from crashing the system. Set in kilobytes.
Applying Ulimit Changes To Running Processes
Changes to limits.conf only affect new login sessions. Already running processes keep their old limits. To apply new limits to a running process, you have a few options:
- Restart the process or service
- Use
prlimitcommand (requires root) - Log out and log back in
The prlimit command is powerful. It can change limits of a running process without restarting. Example:
sudo prlimit --pid 1234 --nofile=4096:8192
Replace 1234 with the actual PID. This sets soft limit to 4096 and hard limit to 8192.
Verifying Ulimit Settings
Always verify your changes. Use these commands:
ulimit -n # Shows open files limit
ulimit -u # Shows max user processes
ulimit -s # Shows stack size
For a specific process, check /proc/PID/limits:
cat /proc/1234/limits
This shows all limits for that process. Compare with what you set.
Troubleshooting Common Ulimit Issues
Even with correct configuration, things can go wrong. Here are common problems and solutions.
Changes Not Taking Effect
If your limits.conf edits don’t work, check these:
- Did you log out and back in?
- Is
pam_limits.soenabled in PAM configuration? - Are you editing the correct file? Some distros use
/etc/security/limits.d/directory. - For systemd services, did you use the
LimitNOFILEdirective?
Permission Denied When Setting Limits
Non-root users cannot raise hard limits. They can only lower them. If you get “Operation not permitted,” you need root privileges or a higher hard limit set by an admin.
Ulimit Ignored For SSH Sessions
SSH may bypass limits.conf. Add this line to /etc/ssh/sshd_config:
UsePAM yes
Then restart SSH service. This forces SSH to use PAM limits.
Systemd Service Ignores Limits.conf
Systemd services don’t read limits.conf by default. You must set limits in the service file or override. Use LimitNOFILE=, LimitNPROC=, etc.
Best Practices For Setting Ulimit
Follow these guidelines to avoid problems.
- Start with conservative values and increase as needed
- Monitor system resource usage before setting limits
- Set hard limits higher than soft limits
- Document changes in a changelog
- Test after every change with a non-production system
- Use systemd limits for services, not
limits.conf - For high-traffic applications, set open files to at least 65536
Ulimit For Different Linux Distributions
While the core concepts are the same, some distros have quirks.
Ubuntu And Debian
These use /etc/security/limits.conf and PAM by default. Systemd is standard. Works as described above.
Red Hat And CentOS
Similar to Ubuntu. However, older versions may use /etc/security/limits.d/20-nproc.conf for nproc limits. Check that file if your changes don’t stick.
Fedora
Modern Fedora uses systemd aggressively. For user sessions, limits.conf works, but for services, always use systemd directives.
Arch Linux
Arch is minimal. You may need to enable PAM limits manually. Edit /etc/pam.d/system-auth to include pam_limits.so.
Advanced Ulimit Configuration
For power users, there are more advanced options.
Using Ulimit With Containers
Docker containers inherit limits from the host. You can set limits per container with --ulimit flag:
docker run --ulimit nofile=1024:2048 myimage
This overrides host defaults for that container.
Setting Limits Per Group
In limits.conf, you can use @groupname as the domain. For example:
@developers soft nproc 2048
This applies to all users in the “developers” group.
Using Ulimit In Scripts
You can set ulimit inside shell scripts. Place ulimit -n 4096 at the top of the script. This only affects the script and its child processes.
Monitoring Ulimit Usage
To see if a process is hitting limits, check system logs. Use dmesg or journalctl:
sudo dmesg | grep -i "ulimit"
sudo journalctl -xe | grep -i "ulimit"
Also monitor open file counts per process:
sudo lsof -p 1234 | wc -l
This shows how many files process 1234 has open. Compare with its limit.
Frequently Asked Questions
How do I set ulimit permanently in Linux?
Edit /etc/security/limits.conf and add lines for the user or group. For systemd services, use systemctl edit and add LimitNOFILE= or LimitNPROC=.
What is the difference between soft and hard ulimit?
Soft limits are the current working limit. Users can increase them up to the hard limit. Hard limits are the absolute maximum set by the administrator. Only root can increase hard limits.
How to check ulimit for a specific process?
Run cat /proc/PID/limits where PID is the process ID. This shows all limits for that process.
Why does my ulimit change not apply to SSH sessions?
Ensure UsePAM yes is in /etc/ssh/sshd_config. Then restart SSH. Also check that pam_limits.so is loaded in PAM configuration.
Can I set ulimit to unlimited?
Yes, for some parameters like core file size. Use ulimit -c unlimited. But be careful with unlimited memory or processes—it can crash the system.
Final Thoughts On Setting Ulimit
Mastering how to set ulimit in Linux is essential for system stability. Start with checking current values, then make small changes. Always test in a safe environment first. Whether you’re running a small home server or a large production cluster, proper ulimit configuration prevents resource exhaustion and keeps applications running.
Remember to check both soft and hard limits. Use the right method for your system—limits.conf for user sessions, systemd directives for services. Monitor logs and process limits regularly. With these skills, you can handle any resource limit challenge that comes your way.
Now go ahead and check your own system’s ulimits. You might be surprised how low the defaults are. A few minutes of configuration can save hours of troubleshooting later.