Mastering lsof: List Open Files, Processes, and Network Connections on Linux
This guide explains how to use the Linux lsof command‑line utility to list information about files opened by processes—including regular files, directories, sockets, and pipes—while covering options for filtering by file, directory, process name, user, PID, network connections, repeat mode, and more.
It is a command‑line utility that lists information about files opened by processes. In Linux everything is a file (pipes, sockets, directories, devices, etc.), so using
lsofyou can obtain details of any open file.
1. Introduction to lsof
Running
lsofwithout arguments prints all open files for all active processes.
<code># lsof
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
init 1 root cwd DIR 8,1 4096 2 /
...</code>Each column is self‑explanatory; the article explains the FD (file descriptor) and TYPE columns.
cwd – current working directory
txt – program text (executable)
mem – memory‑mapped file
mmap – memory‑mapped device
NUMBER – actual file descriptor; suffix ‘u’, ‘r’, ‘w’ indicate read/write mode.
TYPE indicates the file type, e.g. REG (regular file), DIR (directory), FIFO (named pipe), CHR (character device).
2. List processes that have a specific file open
Provide a pathname as an argument:
<code># lsof /var/log/syslog</code>3. List files opened under a directory
Use
+Dto list files under a directory recursively, or
+dfor a non‑recursive listing.
<code># lsof +D /var/log</code>4. List files opened by processes matching a name
Use
-cfollowed by a string; multiple
-cswitches are allowed.
<code># lsof -c ssh -c init</code>5. Find processes using a mount point
Useful when a filesystem cannot be unmounted because it is busy.
<code># lsof /home
# lsof +D /home</code>6. List files opened by a specific user
Use
-ufollowed by the username.
<code># lsof -u lakshmanan</code>7. List files opened by a specific PID
Use
-pwith the process ID.
<code># lsof -p 1753</code>8. Kill all processes belonging to a user
Combine
lsof -t -u USERto get PIDs and feed them to
kill.
<code># kill -9 `lsof -t -u lakshmanan`</code>9. Combine list options with OR/AND
Multiple options are ORed by default; use
-ato require all conditions.
<code># lsof -u lakshmanan -c init # OR
# lsof -u lakshmanan -c init -a # AND (no output if no such process)</code>10. Repeating mode
Use
-ror
+rto repeat the listing at intervals;
+rstops when no files are found.
<code># lsof -u lakshmanan -c init -a -r5</code>Finding network connections
Network sockets are also files, so
lsofcan list them.
11. List all network connections
<code># lsof -i</code>12. List network files used by a specific process
<code># lsof -i -a -p 234
# lsof -i -a -c ssh</code>13. List processes listening on a specific port
<code># lsof -i :25</code>14. List all TCP or UDP connections
<code># lsof -i tcp
# lsof -i udp</code>15. List NFS files
<code># lsof -N -u lakshmanan -a</code>Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.