Resource limits with ulimit
Per-process resource limits: soft and hard values, why limits.conf does not reach systemd services, LimitNOFILE=, and fixing 'too many open files'.
Processes through /procPAM and the login pathWriting and overriding unit files
Resource limits with ulimit
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- An rlimit is a per-process cap the kernel enforces, such as open files (NOFILE), processes per user (NPROC), core dump size (CORE) and stack size (STACK).
- Each limit has a soft value, which is enforced, and a hard value, a ceiling. An unprivileged process may raise its soft limit up to the hard one and may lower the hard one, but never raise it.
- Limits are inherited across fork and exec, so they are set by whatever starts the process: the login path for shells, systemd for services.
- /etc/security/limits.conf is read by pam_limits during login. It does not apply to systemd services. Use LimitNOFILE= in the unit instead.
- systemd's default for services is a soft open-file limit of 1024 and a hard limit of 524288. Programs that do not use select() are expected to raise their own soft limit.
- 'Too many open files' is EMFILE, the per-process limit. ENFILE means the system-wide fs.file-max, which is rarely the real problem.
- Check a running process's real limits in /proc/<pid>/limits, not with ulimit in your shell.
One process should not take everything
A web server leaks a file descriptor on every failed request. Left alone, it would keep opening until the kernel’s tables were full and nothing on the machine could open a file, including your SSH session. A shell script with a bug forks in a loop. Left alone, it would fill the process table.
Resource limits, rlimits, are the kernel’s per-process seatbelt. Each process carries a set of caps, and when it hits one, the system call fails for that process only. The machine stays up and the error points at the culprit.
The limits and their two values
getrlimit(2) lists them. The ones you will touch:
| Limit | ulimit flag |
Caps |
|---|---|---|
RLIMIT_NOFILE |
-n |
Open file descriptors, more precisely one more than the highest fd number allowed |
RLIMIT_NPROC |
-u |
Processes, on Linux threads, for the process’s real UID |
RLIMIT_CORE |
-c |
Size of a core dump, 0 disables them |
RLIMIT_STACK |
-s |
Main thread’s stack size |
RLIMIT_MEMLOCK |
-l |
Memory that may be locked in RAM |
RLIMIT_NICE, RLIMIT_RTPRIO |
-e, -r |
How far nice and real-time priority may go, from the scheduling lesson |
Each has two values. The soft limit is what the kernel enforces. The hard limit is a ceiling on the soft one. Per the man page, an unprivileged process may set its soft limit anywhere up to the hard limit, and may lower its hard limit, irreversibly. Only a process with CAP_SYS_RESOURCE can raise a hard limit.
Think of a company card with a per-transaction limit the employee can adjust in the app, up to a ceiling only finance can raise.
Hitting one
I ran this on this Mac: a Python script that lowers its own soft RLIMIT_NOFILE to 20, then opens /dev/null until the kernel refuses. macOS, not Linux, but the call, the limit and the error number are the same:
soft limit now 20
opened 17 files, highest fd 19, then: [Errno 24] Too many open files
Seventeen, because descriptors 0, 1 and 2 were already taken by standard input, output and error. Errno 24 is EMFILE, the per-process limit. If the whole system runs out of file handles, the error is ENFILE and proc_sys_fs(5) says the kernel logs “VFS: file-max limit … reached”. On current machines that system-wide ceiling is rarely the problem. The per-process one almost always is.
Where limits come from
Limits are inherited across fork() and execve(). So a process gets whatever its parent had, and the question “what are this service’s limits?” becomes “who started it, and what did they set?”
For login sessions, pam_limits runs during login, as part of the PAM stack, and applies /etc/security/limits.conf and /etc/security/limits.d/*.conf:
# <domain> <type> <item> <value>
@developers soft nofile 8192
@developers hard nofile 65536
* hard core 0
That covers logins whose PAM configuration includes pam_limits, typically SSH and the console. It changes nothing for a process systemd starts.
For services, systemd sets limits itself. With nothing configured, systemd-system.conf(5) gives DefaultLimitNOFILE=1024:524288, soft 1024 and hard 524288. To change one service, a drop-in:
# systemctl edit nginx.service
[Service]
LimitNOFILE=65536
A single value sets soft and hard together. soft:hard sets them separately. The most common “I raised the limit and nothing changed” story is editing limits.conf for a daemon. The unit files lesson covered drop-ins, and the Debian rsyslog unit there already carried LimitNOFILE=16384.
Why the soft default stays at 1024
systemd.exec(5) explains the odd-looking default. select(), an old way of waiting on many file descriptors, cannot handle fd numbers above 1023 on Linux. Raising everyone’s soft limit would make programs that use it fail in strange ways. So the soft limit stays at 1024, the hard limit is high, and programs that use poll() or epoll() are expected to raise their own soft limit up to the hard one at startup. Many servers do. The man page also calls LimitNPROC= and LimitAS= poor tools, because NPROC counts per user, not per service, and AS limits address space, not memory. For a service, TasksMax= and MemoryMax=, the cgroup controls in the cgroups lesson, are the better choice.
Checking and changing a live process
These commands are from prlimit(1) and proc(5). I did not run them here:
cat /proc/4123/limits # the truth, for a running process
ls /proc/4123/fd | wc -l # how many fds it has open now
prlimit --pid 4123 --nofile=65536:65536 # change a running process's limit
ulimit -Sn; ulimit -Hn # soft and hard for this shell only
ulimit in your shell says nothing about a service. Always read /proc/<pid>/limits. And if a process is at its limit because it leaks descriptors, raising the limit only delays the outage. ls -l /proc/<pid>/fd shows what it has open. Thousands of sockets to one backend, or the same file opened over and over, is a bug to fix.
About the video
The embed is all of CyberUdaan Tech’s “Why Linux Says ‘Too Many Open Files’ (And How to Fix It)”, September 2026, six minutes. Its chapters follow the incident: what file-descriptor exhaustion is, checking ulimit -n, checking /proc/sys/fs/file-max and file-nr, finding the top consumers with lsof, raising kernel limits in sysctl.conf, editing limits.conf, and a systemd override for nginx. That order fits this lesson, but it is a small channel and I could not verify the narration line by line, which is why I judged it from the chapter list only. One caution: raising fs.file-max is listed as a step, but the error in most incidents is EMFILE, the per-process limit, so the systemd override is the step that matters for a service. It does not explain soft against hard, or why limits.conf misses services, which is the core of this lesson. English was judged from the title, channel and English auto-caption track. Better-known channels did not have a focused video on this topic, so Jacob Sorber’s explanation of what a file descriptor is sits in deeper.
What was run: the Python EMFILE demo on macOS. Limits, defaults and systemd behaviour are from getrlimit(2) and proc_sys_fs(5) in man-pages 6.19, limits.conf(5) and the systemd 262 man pages, checked 2026-11-28.
Where this fits
Memory is the one resource rlimits handle badly. How Linux really uses RAM is next: memory, page cache and swap.
Check yourself
You add `nginx soft nofile 65536` to /etc/security/limits.conf and restart nginx. /proc/<pid>/limits still shows 1024. Why?
limits.conf is applied by pam_limits when a user logs in. nginx is started by systemd, which never goes through that PAM path. Set LimitNOFILE= in a drop-in for nginx.service.
What is the difference between EMFILE and ENFILE?
EMFILE means this process hit its RLIMIT_NOFILE. ENFILE means the whole system hit fs.file-max.
Why does systemd keep the default soft NOFILE limit at 1024 when the hard limit is 524288?
Because select() cannot handle file descriptors above 1023. Programs that use poll or epoll can raise their own soft limit to the hard limit safely.
Why is LimitNPROC= usually the wrong tool to stop a service forking too much?
It counts processes for the whole real UID, not the service, and is not enforced for root. TasksMax= limits the service's cgroup instead.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.