lsof

Linux · Processes · reference only

What it does

Lists open files and which process uses them. On Linux almost everything is a file, so it also shows network connections and listening ports.

Syntax

lsof [opciones] [nombre...]

Options and parameters

  • -i [dirección]

    Shows network connections; accepts filters such as :22 or tcp@server.

  • -p PID

    Shows the files opened by that process.

  • -u usuario

    Shows the files opened by that user.

  • -c nombre

    Shows the files opened by processes whose name starts like this.

  • +D directorio

    Shows the processes using files inside that directory.

  • -t

    Shows only the PIDs, to use with other commands.

  • -n

    Does not resolve host names, so it is faster.

  • -P

    Does not convert port numbers into service names.

Usage examples

  • sudo lsof -i :22

    Shows which process uses port 22.

  • sudo lsof -iTCP -sTCP:LISTEN -nP

    Lists listening TCP ports with numbers and no DNS.

  • lsof -u ana

    Shows everything user ana has open.

  • lsof +D /var/log

    Finds which processes keep log files open.

Notes and common mistakes

  • Seeing other users' processes requires sudo.
  • It is how you find out why a disk cannot be unmounted: some process still uses files on it.

Related commands