Mastering lsof: How to List Open Files and Network Connections on Linux
This guide explains how to use the powerful lsof command to list information about files opened by processes, filter results by file type, user, process name, directory, network connections, and even repeatedly monitor changes, providing practical examples and code snippets for Linux administrators.
1. Introduction to lsof
lsof (list open files) is a command‑line utility that displays information about files opened by processes. In Linux, everything is a file (pipes, sockets, directories, devices, etc.), so lsof can reveal details of any open file. # lsof Typical output columns include COMMAND, PID, USER, FD (file descriptor), TYPE, DEVICE, SIZE/OFF, NODE, and NAME. FD values such as cwd, txt, mem, mmap, and numbers with mode letters (r, w, u) indicate the file’s role and access mode.
cwd – current working directory
txt – program text (executable)
mem – memory‑mapped file
mmap – memory‑mapped device
NUMBER – actual file descriptor; suffixes u (read/write), r (read), w (write)
REG – regular file
DIR – directory
FIFO – FIFO (named pipe)
CHR – character special file
2. List processes that opened a specific file
# lsof /var/log/syslog3. List files opened under a directory
Use +D to list files opened in a directory recursively; +d for a non‑recursive listing.
# lsof +D /var/log/4. List files opened by process name
# lsof -c ssh -c init5. List processes using a mount point
# lsof /home6. List files opened by a specific user
# lsof -u lakshmananPrefix the user with ^ to exclude that user.
7. List files opened by a specific PID
# lsof -p 17538. Kill all processes belonging to a user that have open files
# kill -9 `lsof -t -u lakshmanan`9. Combine list options with OR logic (default) or AND logic using -a
Example (OR): list processes of user lakshmanan OR names starting with init: # lsof -u lakshmanan -c init Example (AND): list processes that satisfy both conditions:
# lsof -u lakshmanan -c init -a10. Repeating mode
Use -r or +r to repeat the listing at intervals; -r continues indefinitely, +r stops when no files are found.
# lsof -u lakshmanan -c init -a -r5Finding Network Connections
11. List all network connections
# lsof -iUse -i4 or -i6 to restrict to IPv4 or IPv6.
12. List network files used by a specific process
# lsof -i -a -p 23413. List processes listening on a specific port
# lsof -i :2514. List all TCP or UDP connections
# lsof -i tcp
# lsof -i udp15. List all NFS files
# lsof -N -u lakshmanan -aThese examples demonstrate how lsof can be used for detailed inspection of open files, directories, users, processes, and network sockets on a Linux system.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
