Lsof


lsof is a command meaning "list open files", which is used in many Unix-like systems to report a list of all open files and the processes that opened them. This open source utility was developed and supported by Victor A. Abell, the retired Associate Director of the Purdue University Computing Center. It works in and supports several Unix flavors.

Examples

Open files in the system include disk files, named pipes, network sockets and devices opened by all processes. One use for this command is when a disk cannot be unmounted because files are in use. The listing of open files can be consulted to identify the process that is using the files.

  1. lsof /var
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
syslogd 350 root 5w VREG 222,5 0 440818 /var/adm/messages
syslogd 350 root 6w VREG 222,5 339098 6248 /var/log/syslog
cron 353 root cwd VDIR 222,5 512 254550 /var -- atjobs

To view the port associated with a daemon:

  1. lsof -i -n -P | grep sendmail
sendmail 31649 root 4u IPv4 521738 TCP *:25

From the above one can see that "sendmail" is listening on its standard port of "25".
;-i: Lists IP sockets.
;-n: Do not resolve hostnames.
;-P: Do not resolve port names.
One can also list Unix Sockets by using lsof -U.

Lsof output

The lsof output describes:
For a complete list of options, see the Lsof Linux manual page