lsof stands for "list open files" (in Spanish, "lista de archivos abiertos"); it is used on many Unix-like systems to obtain a list of all open files and the processes that opened them. You can also use it to list connections in a similar way to netstat or ss.
This open-source utility was developed and supported by Victor A. Abell, the retired Associate Director of the Purdue University Computing Center. In most Clouding Linux images it is installed by default. An equivalent of lsof on Windows would be handle and ListDLLs from Sysinternals by Mark Russinovich (through a GUI with Process Explorer).
Below is a table of contents with some usage examples and relevant information about the lsof command:
Installing lsof
To install lsof, simply use your distribution's package manager:
# yum install lsof -y# apt install lsof -yOutput columns
The default lsof output structure is as follows:
| COMMAND | PID | TID | TASKCMD | USER | FD | TYPE | DEVICE | SIZE/OFF | NODE | NAME |
Below is a brief explanation of all the fields:
| Name | Description |
|---|---|
| COMMAND | Contains the first nine characters of the UNIX command name associated with the process. |
| PID | The process identification number. |
| TID | The task (thread) identification number; a blank indicates a process (that is, not a task). |
| TASKCMD | The task's command name. |
| ZONE | The zone name when -z has been specified. |
| SECURITY-CONTEXT | The SELinux security context when -Z has been specified. |
| PPID | The identification number of the process's parent process when -R has been specified. |
| PGID | The identification number of the process group associated with the process when -g has been specified. |
| USER | The user identification number or login name of the user who owns the process. |
| FD | The file descriptor number or current working directory (cwd), among others. More information here. |
| TYPE | The type of node associated with the file. More information here. |
| FILE-ADDR | Contains the address of the kernel file structure when f has been specified in +f. |
| FCT | Contains the file reference count from the kernel file structure when c has been specified in +f. |
| FILE-FLAG | Contains the contents of the f_flag[s] member of the kernel file structure, among others, when g or G has been specified in +f. More information here. |
| NODE-ID | Contains a unique identifier for the file's node when n has been specified in +f. |
| DEVICE | Contains the comma-separated device numbers for a character special, block special, regular, directory, or NFS file. |
| SIZE, SIZE/OFF, or OFFSET | The file size or file offset in bytes. More information here. |
| NLINK | Contains the file link count when +L has been specified. |
| NODE | The node number of a local file or the inode number of an NFS file on the server host, among others. More information here. |
| NAME | The name of the filesystem mount point, the name of a character special device, the Internet addresses of a network file, or the local hostname, among others. More information here. |
Examples
List all files opened by processes
To list all open files, simply run lsof:
# lsof | lessAdding the less command lets you view the content more comfortably.
List processes by file
To list the processes for a specific open file, enter the path and file as an argument:
# lsof /ruta/archivoReplace "/ruta/archivo" with the desired open file.
List open files by user
With the following command, you can list the files that a particular user has open:
# lsof -u usuarioReplace "usuario" with the desired system user.
List files opened by a process
To list the files opened by a process, you can use its name as follows:
# lsof -c nombre_procesoRemember to replace "nombre_proceso" with the name of the process in question.
List open files by PID
To list open files by the process's PID, simply run:
# lsof -p numero_PIDReplace "numero_PID" with the process identification number (you can obtain it with the command top).
List connections by process
To list connections opened by processes:
# lsof -iYou will get a list; you can use less to make it easier to read.
List connections by PID, user, or process name
You can combine the option to list connections with, for example, a process identification number:
# lsof -i -a -p numero_pidYou can also use other arguments such as the user (-u), file, or process name (-c), as well as filter by port or IP protocol.
List connections by port
To list connections on a specific port, simply run:
# lsof -i :numero_puertoAfter the colon, add the port number in question ("numero_puerto").
List IPv4 or IPv6 connections
List IPv4 connections:
# lsof -i4You can use it in combination with other arguments, and for IPv6 it would be:
# lsof -i6An example combination is the following:
# lsof -i6 :numero_puertoYou would get the IPv6 connections on a specific port.
More information
As you have seen, lsof is a very useful tool, and you can combine it with different commands. One example would be using Kill to kill all processes in an lsof result; an example of this is killing all of a user's processes:
# kill -9 $(lsof -t -u usuario) You can also combine arguments; the following would show files opened by a specific process belonging to a user:
# lsof -u usuario -c nombre_procesoFinally, using the help argument (-h) will give you more information about lsof:
lsof 4.93.2
latest revision: https://github.com/lsof-org/lsof
latest FAQ: https://github.com/lsof-org/lsof/blob/master/00FAQ
latest (non-formatted) man page: https://github.com/lsof-org/lsof/blob/master/Lsof.8
usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-E] [+|-e s] [+|-f[gG]]
[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges.
-?|-h list help -a AND selections (OR) -b avoid kernel blocks
-c c cmd c ^c /c/[bix] +c w COMMAND width (9) +d s dir s files
-d s select by FD set +D D dir D tree *SLOW?* +|-e s exempt s *RISKY*
-i select IPv[46] files -K [i] list|(i)gn tasKs -l list UID numbers
-n no host names -N select NFS files -o list file offset
-O no overhead *RISKY* -P no port names -R list paRent PID
-s list file size -t terse listing -T disable TCP/TPI info
-U select Unix socket -v list version info -V verbose search
+|-w Warnings (+) -X skip TCP&UDP* files -Z Z context [Z]
-- end option scan
-E display endpoint info +E display endpoint info and files
+f|-f +filesystem or -file names +|-f[gG] flaGs
-F [f] select fields; -F? for help
+|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0)
+m [m] use|create mount supplement
+|-M portMap registration (-) -o o o 0t offset digits (8)
-p s exclude(^)|select PIDs -S [t] t second stat timeout (15)
-T qs TCP/TPI Q,St (s) info
-g [s] exclude(^)|select and print process group IDs
-i i select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
+|-r [t[m<fmt>]] repeat every t seconds (15); + until no files, - forever.
An optional suffix to t is m<fmt>; m must separate t from <fmt> and
<fmt> is an strftime(3) format for the marker line.
-s p:s exclude(^)|select protocol (p = TCP|UDP) states by name(s).
-u s exclude(^)|select login|UID set s
-x [fl] cross over +d|+D File systems or symbolic Links
names select named files or files on named file systems
We hope this article has helped you. If you still have questions about this or any other issue related to your servers on Clouding, write to us at soporte@clouding.io. We're here to help! 😉