File descriptors are a fundamental concept in Linux that every system administrator should understand. They represent open files, sockets, pipes, and other I/O resources used by processes.
Understanding File Descriptors
Every process in Linux has a table of file descriptors. By default, three are always open: stdin (0), stdout (1), and stderr (2). When a process opens additional files or network connections, new file descriptors are assigned incrementally.
# View file descriptors for a process
ls -la /proc/$(pidof nginx)/fd
# Count open file descriptors system-wide
cat /proc/sys/fs/file-nr
# Check per-process limits
ulimit -n
cat /proc/$(pidof nginx)/limits | grep "open files"
Increasing File Descriptor Limits
For high-traffic servers, the default limit of 1024 is often too low. Here is how to increase it:
# Edit /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
# For systemd services, edit the unit file:
[Service]
LimitNOFILE=65535
# Apply system-wide
echo "fs.file-max = 2097152" >> /etc/sysctl.d/99-file-limits.conf
sysctl --system
Summary
Properly managing file descriptors prevents "Too many open files" errors and ensures your services can handle the required number of concurrent connections.