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 PIDShows the files opened by that process.
-u usuarioShows the files opened by that user.
-c nombreShows the files opened by processes whose name starts like this.
+D directorioShows the processes using files inside that directory.
-tShows only the PIDs, to use with other commands.
-nDoes not resolve host names, so it is faster.
-PDoes not convert port numbers into service names.
Usage examples
sudo lsof -i :22Shows which process uses port 22.
sudo lsof -iTCP -sTCP:LISTEN -nPLists listening TCP ports with numbers and no DNS.
lsof -u anaShows everything user ana has open.
lsof +D /var/logFinds 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.