Network

The lsof command

Lists the files opened by processes. On Linux everything is a file, so it shows which process uses a port, a socket or a disk. It is very useful to find out why a disk will not unmount.

Syntax

lsof [opciones] [nombre]Try it in the terminal →

Most useful options

-i [dirección]Network files. With -i :80 it shows those using port 80.
-sTCP:LISTENOnly TCP sockets in listen state: the services waiting for connections.
-p <pid>Only the files of that process.
-u <usuario>Only the files opened by that user.
-tPrints only the PIDs, one per line; ideal to feed kill in a script.
+D <directorio>Processes with files open inside a directory, walking all of it. Slow on large folders.

Copy-ready examples

$ sudo lsof -i :8080Which process listens on port 8080.

Example output

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    4821 miguel  21u  IPv4  61234      0t0  TCP *:8080 (LISTEN)
$ sudo lsof -i TCP -sTCP:LISTENEvery TCP service listening on the machine.
$ lsof -p 4821Lists everything that process has open: files, libraries and sockets.
$ sudo lsof +D /var/logWho has files open under /var/log. It takes longer because it walks the whole tree.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
No error messageNo output and exit code 1: nobody has what you are looking for open, or you lack permission to see it.Try with sudo and check that the port or path is correct.
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfslsof cannot read a virtual filesystem, such as the FUSE or desktop gvfs mounts.It is a harmless warning: filter with -i or -p to see only what matters.
No error messageThe port is held by docker-proxy: a container publishes that port and Docker is the real owner.Stop the container with docker stop, not kill on docker-proxy.

Typical mistakes

  • Without sudo you only see your own processes, and lsof does not warn about the rest.

Differences from similar commands

  • ss: Shows network sockets quickly, without searching open files. For ports, ss -tulpn is enough.
  • fuser: Says which processes use a file or port, with less detail than lsof.

Security

With sudo, lsof sees files and sockets of every user: use it to diagnose, not to spy on other people's sessions. Killing whatever lsof returns can take down an important service: check the COMMAND before running kill $(lsof -t -i :8080).

Related commands

FAQ

How do I free a port that is in use?

Find the PID with lsof -i :PORT, then decide whether to stop it with systemctl stop or with kill PID, depending on whether it is a service or a loose program.

Why is lsof so slow?

+D walks every file under a directory, and without -n it resolves DNS names. Add -n -P or narrow it with -i.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course