The fix that stops the bleeding is one command — sudo sysctl -w fs.inotify.max_user_watches=524288 — followed by writing the same value into /etc/sysctl.d/60-inotify.conf so it survives a reboot, and restarting whatever crashed. That takes thirty seconds and it is the right answer perhaps half the time. The other half, some process is holding hundreds of thousands of watches it should never have taken, and raising the ceiling only buys you a few weeks before the same error comes back with more kernel memory pinned behind it. This guide covers both: how inotify actually allocates, how to identify the real consumer, and how to make a deliberate decision instead of copying a number off a forum.
What you’ll accomplish
- Read the three
fs.inotifysysctls and understand what each one really limits. - Count how many watches are in use right now, and attribute them to individual processes through
/proc/*/fdinfo. - Tell apart the three errors people confuse:
ENOSPCfrom the watch limit,EMFILEfrom the instance limit, andToo many open filesfrom the per-process descriptor limit. - Raise the limit for the running kernel and persist it in
/etc/sysctl.d/, in a file that does not get quietly overridden. - Know when to exclude directories or switch to polling instead of raising a kernel limit again.
Prerequisites
- A Linux host on a modern kernel (4.x or newer). Everything here uses
sysctland/proc, both of which have been stable for a decade. - Root or
sudo. Reading/proc/<pid>/fdinfofor processes you do not own needs it, and so does writing sysctls. - Optional but useful:
inotify-toolsforinotifywaitandinotifywatch, andinotify-infofor a ready-made per-process table. Both are packaged on Debian, Ubuntu, Fedora and Arch. - This is a Linux kernel feature and does not exist elsewhere. macOS uses FSEvents and kqueue, which have entirely different limits and no
max_user_watches. WSL1 emulates the syscalls poorly and this guide does not apply; WSL2 runs a real Linux kernel, so it does, but the sysctl lives in the WSL VM rather than on Windows.
How it works
inotify is the kernel’s filesystem event notification interface. A process calls inotify_init() and gets back a file descriptor — that descriptor is an instance. It then calls inotify_add_watch() once per path it wants to observe, and each call registers a watch against one inode. Events on watched inodes are queued into the instance and the process reads them off the descriptor.
Two properties of that design cause almost all the pain. First, inotify does not recurse. Watching /srv/app tells you nothing about /srv/app/src. Any tool that appears to watch a tree — Vite, webpack, nodemon, chokidar, Syncthing, Dropbox — walks the tree itself and adds a watch per directory. A node_modules with twelve thousand package directories costs twelve thousand watches, and the user does not see that happening.
Second, the limits are per real user ID, not per process. This is the misdiagnosis that wastes the most time: someone sees the error from a single dev server, concludes that one server cannot possibly need 65,536 watches, and starts looking for a bug in the app. Meanwhile the same UID is also running a file sync client, three editor language servers, a container runtime and two other dev servers, and together they crossed the line. The failing process is usually just the last one to ask.
Three sysctls govern the behaviour:
fs.inotify.max_user_watches— total watches one UID may hold across every instance it owns. When it is exhausted,inotify_add_watch()fails withENOSPC, which glibc renders as “No space left on device” and Node renders as “System limit for number of file watchers reached”. It has nothing to do with disk space, though it is the same errno you would get from a genuinely full filesystem.fs.inotify.max_user_instances— how many inotify file descriptors one UID may hold. When exhausted,inotify_init()fails withEMFILE, which surfaces as “Too many open files” and sends people chasingulimit -nfor hours.fs.inotify.max_queued_events— the per-instance event queue depth. Overflowing it is not an error: the kernel drops events and delivers a singleIN_Q_OVERFLOWinstead. Nothing crashes, files silently stop syncing or rebuilding, and you find out later.
Watches are not free. Each one holds a small kernel structure — roughly a kilobyte on 64-bit — in memory that is never swapped out. Setting the ceiling to 1,048,576, a value copied around widely, gives one user permission to pin on the order of a gigabyte of unreclaimable kernel memory. On a laptop with 32 GB that is an acceptable trade. On a 2 GB CI runner it is a way to turn a clear error message into an out-of-memory kill.
Default values vary by kernel and distribution. Older kernels and older enterprise images ship max_user_watches at 8192, a figure that has not moved since machines had a gigabyte of RAM. Kernels from 5.11 onward size the default from available memory instead, which is why a current desktop distribution may already sit in the hundreds of thousands and never show you this error at all. Read yours rather than assuming.
Step-by-step
1. Read all three limits at once. The sysctl prefix match saves you three commands:
sysctl fs.inotify
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 65536
2. Count how many watches are actually in use. Every inotify descriptor exposes one line per watch in its fdinfo file, so counting those lines across all processes gives the system total:
sudo find /proc/[0-9]*/fdinfo -type f 2>/dev/null \
| sudo xargs grep -h '^inotify' 2>/dev/null \
| wc -l
64912
Compare that against max_user_watches. At 64,912 of 65,536 you have your answer. Note that this counts every UID on the box, while the limit applies per UID — on a multi-user host, do the per-process breakdown next and group by owner.
3. Attribute the watches to processes. This is the step most write-ups skip, and it is the one that tells you whether to raise the limit or fix an application:
sudo find /proc/[0-9]*/fdinfo -type f 2>/dev/null \
| sudo xargs grep -cH '^inotify' 2>/dev/null \
| awk -F: '$2 > 0 { split($1, path, "/"); watches[path[3]] += $2 }
END { for (pid in watches) print watches[pid], pid }' \
| sort -rn | head -10 \
| while read -r count pid; do
printf '%8s %-8s %s\n' "$count" "$pid" "$(ps -p "$pid" -o comm= 2>/dev/null)"
done
58204 4812 node
5120 1190 syncthing
842 2233 gnome-shell
190 1044 systemd
63 3901 gvfs-udisks2-vo
12 7734 tail
The shape of that table is the whole diagnosis. One process at 58,204 out of a 65,536 ceiling is not a capacity problem, it is a watcher walking somewhere it should not. A dozen processes at a few thousand each is a genuine capacity problem.
4. Use inotify-info if you would rather not maintain a pipeline. It reads the same data and prints the limits, a per-process table and the watched paths:
sudo inotify-info
INotify Limits:
max_queued_events 16384
max_user_instances 128
max_user_watches 65536
Pid Uid App Watches Instances
4812 1000 node 58204 3
1190 1000 syncthing 5120 1
2233 1000 gnome-shell 842 6
lsof is the other reflex here, and it is worth being precise about what it gives you: sudo lsof -n | grep inotify lists inotify descriptors, so it counts instances, not watches. That makes it the right tool for an EMFILE investigation and the wrong one for ENOSPC. For the general technique of tracing descriptors back to processes, see find open files and ports with lsof.
5. Raise the limit for the running kernel. If the breakdown showed a legitimate spread, pick a value with the memory cost in mind and apply it:
sudo sysctl -w fs.inotify.max_user_watches=524288
fs.inotify.max_user_watches = 524288
The change is immediate and applies to new inotify_add_watch() calls. It does not survive a reboot.
6. Persist it under /etc/sysctl.d/. Use a dedicated file rather than editing /etc/sysctl.conf, so the setting is greppable and removable:
sudo tee /etc/sysctl.d/60-inotify.conf >/dev/null <<'EOF'
# Raised for the dev servers and the sync agent on this host.
# Roughly 1 KB of unswappable kernel memory per watch.
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 512
EOF
Apply every file in the sysctl search path:
sudo sysctl --system
* Applying /usr/lib/sysctl.d/50-default.conf ...
* Applying /etc/sysctl.d/60-inotify.conf ...
fs.inotify.max_user_watches = 524288
fs.inotify.max_user_instances = 512
* Applying /etc/sysctl.conf ...
7. Restart the process that failed. Most watchers add their watches once at startup and never retry the ones that were refused, so a dev server that hit ENOSPC keeps running blind until you restart it. Raising the limit does not repair a running process.
8. Watch deliberately instead of watching everything. inotify-tools gives you a command-line watcher with real exclusions, which is also the fastest way to prove a directory tree is the problem:
inotifywait -m -r -e modify,create,delete \
--exclude '(node_modules|\.git|dist|\.cache)' /srv/app
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
/srv/app/src/ MODIFY index.ts
/srv/app/src/ CREATE handler.ts
inotifywatch from the same package aggregates instead of streaming, which answers “which directory is generating all this churn”:
inotifywatch -v -r -t 60 --exclude '(node_modules|\.git)' /srv/app
total modify create delete filename
118 104 9 5 /srv/app/src/
23 23 0 0 /srv/app/public/
Validate it works
Confirm the runtime value. Read it back rather than trusting the write:
sysctl fs.inotify.max_user_watches
fs.inotify.max_user_watches = 524288
Confirm the failing application recovers. Restart it and watch the count settle rather than climb without bound. Re-run the per-process breakdown from step 3 after a few minutes: a healthy watcher plateaus, a pathological one does not.
Confirm it survives a reboot. This is the only real test of persistence, because a file in /etc/sysctl.d/ that is shadowed by another file will still look correct until the machine restarts:
sudo reboot
After it comes back, sysctl fs.inotify should show your values. If it shows the old ones, go to the first troubleshooting item.
Troubleshooting
- The setting is in
/etc/sysctl.d/but the kernel disagrees after reboot. Files are applied in lexical order by filename across/usr/lib/sysctl.d/,/run/sysctl.d/and/etc/sysctl.d/, and the last write wins — so99-cloud-init.confbeats your60-inotify.conf. On systemd hosts/etc/sysctl.confis applied after all of them. Find every definition withgrep -rs inotify /etc/sysctl.conf /etc/sysctl.d/ /run/sysctl.d/ /usr/lib/sysctl.d/, then either delete the competing entry or rename yours to sort later. Cloud images and configuration management tools are the usual culprits. - A container or pod cannot set it.
docker run --sysctl fs.inotify.max_user_watches=524288is rejected, becausefs.inotify.*is not on the runtime’s list of settable namespaced sysctls — that list coverskernel.msg*,kernel.sem,kernel.shm*andfs.mqueue.*and nothing else. A Kubernetes pod that lists it undersecurityContext.sysctlsis likewise refused at admission as a forbidden sysctl. Since Linux 5.11 the kernel does track inotify limits per user namespace through the ucounts mechanism, exposed as/proc/sys/user/max_inotify_watches, so the “it is simply not namespaced” explanation you will read in older answers is out of date — but that changes nothing operationally, because no container runtime gives you a supported way to raise your ownfs.inotifyceiling. Set it on the node instead — through the image, the node bootstrap script, or a privileged DaemonSet that writes the value at startup. Remember that the limit is per UID across the whole node, so every pod running as the same UID shares one budget. - You get
EMFILEor “Too many open files” rather thanENOSPC. Two different limits produce that text.inotify_init()returnsEMFILEwhenmax_user_instancesis exhausted, andopen()returns it when the process hitsRLIMIT_NOFILE. Check the instance count first withsudo lsof -n | grep -c inotify, then check the process limit withcat /proc/<pid>/limitsand raise it throughLimitNOFILE=in the systemd unit rather thanulimitin a shell. Raisingmax_user_watcheswill not help either case. - The watch count climbs back to the new ceiling within days. You raised the limit on a watcher that was never going to stop. Configure exclusions where the watcher reads them:
watchOptions.ignoredfor webpack,server.watch.ignoredfor Vite,--ignorefor nodemon, anignorePatternslist for Syncthing. Excludingnode_modules,.git, build output and caches typically removes 90% of the watches on a JavaScript project. - Events stop arriving but nothing errors. That is
max_queued_eventsoverflowing under a burst — a largegit checkout, a build, an rsync. The kernel drops the backlog and emitsIN_Q_OVERFLOW, and most applications ignore it. Raisefs.inotify.max_queued_eventsto 65536 in the same sysctl file, but treat it as a symptom: if a directory generates bursts that deep, it probably should not be watched.
When this is the wrong approach
- When one application is the entire problem. If step 3 shows a single process holding 90% of the watches, fixing its exclusion list is the actual repair. Raising the ceiling to accommodate a watcher indexing
node_modulesmeans paying kernel memory forever for events nobody consumes. - When the workload is a large, mostly-static tree. Watching a million-file media library or an artifact store costs a million watches and a gigabyte of pinned memory to detect changes that happen twice a day. Periodic polling with
find -newer, a scheduled scan, or an event feed from the writing application costs almost nothing. - On ephemeral CI runners. Raising the limit in a runner image is a workaround for a build that is watching files it does not need to watch. Most CI failures of this kind come from running a dev server or a watch-mode test runner where a one-shot build belongs. Fix the command, not the kernel.
- When it is a symptom of process leakage. A count that keeps growing across the same PID, or dozens of copies of the same watcher process, points at instances that were never closed. A limit raise hides the leak and the host runs out of something else later.
The reference material worth reading is the kernel’s own: inotify(7) for the interface, the queue behaviour and the /proc/sys/fs/inotify files, and inotify_add_watch(2) for the exact conditions under which each errno is returned.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.