File Descriptor
A file descriptor is a small non-negative integer the kernel gives a process to refer to an open file, socket, pipe, or device. Descriptors 0, 1, and 2 are stdin, stdout, and stderr.
Understanding File Descriptors
What Is a File Descriptor in Linux?
When a program opens a file, the kernel returns a number, not the file itself. That number is the file descriptor (FD), and the program passes it to read(), write(), and close(). Files, network sockets, pipes, and devices all use the same mechanism, which is why one set of system calls works on all of them.
How the File Descriptor Table Works
Each process has its own table mapping FD numbers to open resources.
FD Points to0 stdin (keyboard or pipe)1 stdout (terminal or pipe)2 stderr (terminal)3 /var/log/app.log4 socket: TCP connection to database5 /etc/config.yamlRedirection simply repoints these numbers. command > out.txt repoints FD 1 at a file, and 2>&1 makes FD 2 point wherever FD 1 points at that moment.
How to Inspect File Descriptors
ls -la /proc/$$/fd/ # FDs of the current shelllsof -p 1234 # open files of a processlsof /var/log/nginx/access.log # who has this file openlsof +L1 # files deleted but still open ulimit -n # per-process FD limit for this shellls /proc/1234/fd | wc -l # FDs in use by a processgrep 'open files' /proc/1234/limitsHow to Fix "Too Many Open Files"
This error (EMFILE) means a process hit its FD limit. Every network connection costs one FD, so a busy server needs far more than the common default of 1024.
# systemd service unit[Service]LimitNOFILE=65536# nginx: add to nginx.conf in the main context# worker_rlimit_nofile 65536; # Verify the running process picked it upgrep 'open files' /proc/$(pidof -s nginx)/limitsTroubleshooting File Descriptors
| Symptom | Command | What to Look For |
|---|---|---|
| Too many open files | lsof -p PID | wc -l |
Count against limit |
| Disk full after delete | lsof +L1 |
Process holding file open |
| Cannot unmount | lsof /mountpoint |
Open files on mount |
| FD count keeps growing | ls /proc/PID/fd | wc -l |
Leak over time |
TipIf you delete a large log file and
dfshows no space freed, a process still holds it open. The space returns only when every FD for it closes. Restart the process, or truncate it with: > filebefore deleting next time.
Common MistakeRaising
ulimit -nin your shell and expecting a systemd service to benefit. Services useLimitNOFILE=from their unit file instead.
RememberFD exhaustion is a classic production failure for busy services. Set the limit deliberately and monitor FD counts.
Frequently Asked Questions
Why do file descriptors matter for debugging "too many open files"?
The limit covers sockets, pipes, and files together, so a service with many connections or an unclosed-handle leak eventually fails to accept connections or open files. Compare `ls /proc/PID/fd | wc -l` with the process limit to confirm.
What is the difference between stdout and stderr?
They are separate streams (FD 1 and FD 2). Keeping them separate lets you save normal output to a file while errors still show on screen, or capture each independently.
What causes file descriptor leaks in code?
Opening a file or socket and not closing it on an error path. Constructs that guarantee cleanup, such as Python's `with`, Java's try-with-resources, or Go's `defer`, prevent most leaks.