Operations 10 min read

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.

Raymond Ops
Raymond Ops
Raymond Ops
Mastering lsof: List Open Files, Processes, and Network Connections on Linux

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

lsof

you can obtain details of any open file.

1. Introduction to lsof

Running

lsof

without 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

+D

to list files under a directory recursively, or

+d

for a non‑recursive listing.

<code># lsof +D /var/log</code>

4. List files opened by processes matching a name

Use

-c

followed by a string; multiple

-c

switches 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

-u

followed by the username.

<code># lsof -u lakshmanan</code>

7. List files opened by a specific PID

Use

-p

with the process ID.

<code># lsof -p 1753</code>

8. Kill all processes belonging to a user

Combine

lsof -t -u USER

to 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

-a

to 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

-r

or

+r

to repeat the listing at intervals;

+r

stops when no files are found.

<code># lsof -u lakshmanan -c init -a -r5</code>

Finding network connections

Network sockets are also files, so

lsof

can 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>
LinuxSystem Administrationfile descriptorslsofnetwork connections
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.