How-to

The lsof Command: See Open Files, Ports and Sockets

In Linux almost everything is a file, including network sockets. lsof lists them all with the process that owns each one, which makes it the tool for busy mounts, mystery ports and vanished disk space.

What does lsof actually show?

lsof stands for list open files. For every process it prints the regular files, directories, libraries, pipes, devices and network sockets the process holds. Run it without arguments and you get thousands of lines, so in practice you always add a filter. Run it with sudo; as a normal user you only see your own processes.

sudo apt install -y lsof      # Debian, Ubuntu
sudo dnf install -y lsof      # AlmaLinux, Rocky, CentOS Stream

Most images ship it already. If the command is missing, the line above for your distro adds it in a second.

How do I read the output?

ColumnWhat it tells you
COMMAND, PID, USERThe process and its owner
FDFile descriptor: cwd, txt, mem, or a number with r, w or u for read, write, both
TYPEREG file, DIR, CHR device, FIFO pipe, IPv4 or IPv6 socket, unix socket
DEVICE, SIZE/OFF, NODEDevice numbers, size or offset, inode
NAMEPath, or address and port for sockets

Which process is using this port?

sudo lsof -nP -iTCP:8443 -sTCP:LISTEN
sudo lsof -nP -i :5432
sudo lsof -nP -iUDP

Always add -n and -P: they skip DNS and service name lookups, which keeps lsof fast and shows real port numbers. -sTCP:LISTEN limits the result to listening sockets, the usual answer to "address already in use". For a wider view of every listener, see Check open ports in Linux.

Who is keeping this mount or folder busy?

sudo lsof +f -- /mnt/backup
sudo lsof +D /srv/uploads
sudo lsof /var/log/nginx/error.log

When umount fails with "target is busy", lsof +f -- on the mount point lists every process with something open there, including shells whose working directory is inside it. +D walks a directory tree recursively; it is slow on large trees, so point it at the smallest path that answers the question.

Why is my disk full when the files are deleted?

If a process still holds a file open, deleting the file only removes its name. The data stays on disk until the process closes it. This happens constantly with log files removed by hand while a service keeps writing. df says the disk is full while du cannot find the space.

sudo lsof -nP +L1 | awk '$7 > 104857600'
sudo lsof -nP | grep '(deleted)'

+L1 lists open files with a link count below one, meaning deleted. The awk filter keeps files bigger than 100 MiB. The fix is to restart or reload the service holding the file. If you cannot restart it right now, truncate the open file through its descriptor:

sudo truncate -s 0 /proc/1384/fd/7

Replace the PID and descriptor number with the ones from the lsof output. Then set up log rotation properly so it does not happen again.

What files does one process have open?

sudo lsof -p 2204
sudo lsof -p 2204 -a -d 0-999 | wc -l
sudo lsof -c java -a -i

-a joins filters with AND; without it lsof treats them as OR, which surprises almost everyone the first time. The second line counts numbered descriptors, which you can compare against the process limit in /proc/2204/limits when an app logs "too many open files". The third shows network sockets for every process named java.

How do I see everything a user has open?

sudo lsof -u deploy -a -i
sudo lsof -u ^root -nP -iTCP -sTCP:ESTABLISHED

A caret negates a filter, so the second command shows established TCP connections for every user except root. That is a quick way to check what an app account talks to on the network.

Is lsof slow on a big server?

It can be. On a box with many processes and many open files, such as a database on M2 with 512 GB DDR4 of RAM, a full lsof run reads a lot of /proc. Narrow by PID, user or port, keep -n and -P, and add -b to avoid kernel calls that can block on a dead network mount. For listening ports alone, ss from The ss command is faster.

Machines for Services With Many Open Files

ServerProcessorCores / threadsMemoryStorageNetworkPer monthSmallest order
S1Intel Xeon E-2276G6 / 1264 GB DDR41x 1 TB NVMe2x 10 Gbps$53.32$53.32 (1 month)
S5Intel Xeon E-2276G6 / 12128 GB DDR42x 1 TB NVMe2x 10 Gbps$88.72$88.72 (1 month)
M22x Intel Xeon Gold 531724 / 48512 GB DDR42x 4 TB NVMe2x 25 Gbps$435.32$435.32 (1 month)

Keep Reading

Check open ports in Linux

Find which ports your Linux machine listens on, which program owns each one, and what the internet can really reach from outside.

Open

The ss command

List sockets fast with ss: listening ports, filters by state, port and address, TCP internals like RTT, summaries and killing stuck sockets.

Open

List all processes in Linux

Show every process on a Linux machine with ps, pgrep, pstree and systemd tools, then sort by memory or CPU to find the heavy ones.

Open

Read Linux log files

Find and read Linux logs on a server: the systemd journal, /var/log text files, app logs, filtering by time and following live output.

Open

FAQ

Quick Answers

Run it with sudo. Without root, lsof only lists sockets owned by your own user.

The file was removed but a process still holds it open, so its disk space is not freed until that process closes it.

Add -a between them. By default lsof joins filters with OR.

Yes, it only reads. Narrow the search on very large systems so it finishes quickly.

fuser answers one question, which PIDs use a file or port, and can kill them. lsof gives the full detail.