Understanding lsof: Linux Open Files and Network Connections
At a glance#
- Purpose: Reference for using
lsofto inspect open files, network connections and listening ports. - Applies to: Any Linux or Unix-like system.
- Risk: None — read-only diagnostics.
- Time: Reference article, no fixed duration.
Overview#
lsof stands for list open files. On Unix-like systems almost everything is represented as a file — regular files, directories, sockets, pipes, devices — so a tool that lists open files ends up being one of the most useful diagnostic commands available.
It answers questions that come up constantly during incidents:
- What is holding port 443 open?
- Why can I not unmount this filesystem?
- The disk is full but
dudisagrees withdf— what is holding the space? - Which files is this runaway process touching?
Note:lsofneeds root to see files opened by other users' processes. Withoutsudoit silently shows only your own, which can be very misleading during troubleshooting. Usesudoby default.
Network connections#
List all network connections#
sudo lsof -P -i -n| Flag | Meaning |
|---|---|
-P | Show port numbers rather than service names (443 not https) |
-i | Network connections only |
-n | Do not resolve IPs to hostnames — much faster, and works when DNS is broken |
Using -P and -n together is the habit worth forming. Both avoid lookups that can hang for seconds when a system is already unhealthy.
Find what is using a specific port#
The most common use of the command. When a service will not start because the port is taken:
sudo lsof -i tcp:443sudo lsof -i :8080Show only listening ports#
sudo lsof -i -P -n | grep LISTENFormatted into aligned columns, IPv4 only:
sudo lsof -Pni4 | grep LISTEN | column -tConnections for one process#
sudo lsof -i -a -p 1234The -a flag means AND rather than the default OR. Without it, lsof shows network connections or anything belonging to PID 1234, which is rarely what you want.
Files and processes#
All files opened by a command#
sudo lsof -c nginxMatches by command name, so this covers every nginx worker process at once.
All files opened by a process ID#
sudo lsof -p 1234Everything a user has open#
sudo lsof -u usernameRestricted to a particular directory tree:
sudo lsof -u username -a +D /etc+D recurses into the directory. It is thorough but slow on large trees — +d checks only the directory itself.
What is using a mount point#
The answer to "target is busy" when unmounting:
sudo lsof +D /mnt/dataOr by device:
sudo lsof /dev/sdb1A process's current working directory#
sudo lsof -p 1234 | grep cwdUseful when a process is writing to a relative path and you need to know where that actually is.
Disk space investigation#
Deleted files still holding space#
This is the classic case where df reports a full disk but du cannot account for it. A process still holds an open handle to a file that has been deleted, so the space is not released until that process closes it or exits.
sudo lsof | grep deletedOr scoped to one filesystem:
sudo lsof +L1+L1 lists files with a link count below 1 — deleted but still open.
Once identified, restart the owning process to release the space. Truncating the file in place also works without a restart:
sudo truncate -s 0 /proc/<PID>/fd/<FD>Largest open files#
sudo lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB", $9, $1 }' | sort -n -u | tail | column -tCommon options reference#
| Option | Description |
|---|---|
-i | Network connections; can be narrowed with -i tcp:443 |
-P | Numeric port numbers instead of service names |
-n | Do not resolve hostnames |
-p | Filter by process ID |
-u | Filter by username |
-c | Filter by command name |
-a | Combine filters with AND instead of OR |
+D | Recurse into a directory |
+L1 | Show deleted files still held open |
-t | Output PIDs only — useful for piping into kill |
Reading the output#
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1234 root 6u IPv4 28371 0t0 TCP *:443 (LISTEN)| Column | Meaning |
|---|---|
COMMAND | Process name |
PID | Process ID |
USER | Owning user |
FD | File descriptor. cwd = working directory, txt = program text, mem = memory-mapped, a number = an open handle |
TYPE | REG regular file, DIR directory, IPv4/IPv6 socket, FIFO pipe |
NAME | Path, or address and port for sockets |
In the FD column, the suffix indicates access mode: r read, w write, u read and write.
Practical recipes#
Kill everything using a mount point so it can be unmounted:
sudo lsof -t +D /mnt/data | xargs -r sudo killWarning: This terminates processes without warning. Review the list with sudo lsof +D /mnt/data first.
Confirm which process to blame for a port conflict:
sudo lsof -i :80 -P -n | grep LISTENWatch a process open files in real time:
watch -n1 "sudo lsof -p 1234 | wc -l"A steadily climbing count usually means a file descriptor leak.
Alternatives#
| Tool | Better for |
|---|---|
ss -tulpn | Faster for sockets alone; the modern replacement for netstat |
fuser -v /path | Quickly identifying processes using one file or mount |
netstat -tulpn | Deprecated; still common in older documentation |
lsof remains the most complete of these because it covers files and sockets together.