10 lsof Command Examples

by lifeLinux on May 14, 2011

Example 7: Pointing to a file shows what’s interacting with that file

Example, type the following command

lsof /var/log/messages 

Sample outputs

COMMAND   PID USER   FD   TYPE DEVICE    SIZE     NODE NAME
syslogd  2424 root    2w   REG  253,4 1437387 21463252 /var/log/messages
lfd     20823 root    6r   REG  253,4 1437387 21463252 /var/log/messages

Example 8: Display who is accessing a device

lsof /dev/tty1

Sample outputs

COMMAND   PID USER   FD   TYPE DEVICE SIZE NODE NAME
mingetty 3032 root    0u   CHR    4,1       854 /dev/tty1
mingetty 3032 root    1u   CHR    4,1       854 /dev/tty1
mingetty 3032 root    2u   CHR    4,1       854 /dev/tty1

Example 9: What files and network connections a command

The flag -c to display files and network connections a command, example display files and network connections syslog command

 lsof -c syslog

Sample outputs

COMMAND  PID USER   FD   TYPE     DEVICE    SIZE     NODE NAME
syslogd 2424 root  cwd    DIR      253,4    4096        2 /
syslogd 2424 root  rtd    DIR      253,4    4096        2 /
syslogd 2424 root  txt    REG      253,4   35832 41746614 /sbin/syslogd
syslogd 2424 root  mem    REG      253,4         16252949 /lib/libc-2.5.so (path inode=16252965)
syslogd 2424 root  mem    REG      253,4   50848 16253138 /lib/libnss_files-2.5.so
syslogd 2424 root  mem    REG      253,4         16252942 /lib/ld-2.5.so (path inode=16252964)
syslogd 2424 root    0u  unix 0xf7804040             5125 /dev/log
syslogd 2424 root    2w   REG      253,4 1439314 21463252 /var/log/messages
syslogd 2424 root    3w   REG      253,4   46447 21463260 /var/log/secure
syslogd 2424 root    4w   REG      253,4 3725001 21463266 /var/log/maillog
syslogd 2424 root    5w   REG      253,4  582424 21463282 /var/log/cron
syslogd 2424 root    6w   REG      253,4       0 21463270 /var/log/spooler
syslogd 2424 root    7w   REG      253,4       0 21463275 /var/log/boot.log

Example 10: Show all processes opening files in a directory

lsof +D /usr/bin/

Sample outputs

COMMAND     PID USER   FD   TYPE DEVICE  SIZE     NODE NAME
mysqld_sa  2574 root  255r   REG  253,4 13073 50999014 /usr/bin/mysqld_safe
munin-nod  2968 root  txt    REG  253,4 14984 50993003 /usr/bin/perl
lfd       20823 root  txt    REG  253,4 14984 50993003 /usr/bin/perl

References

The lsof man page

Pages: 1 2

Related Posts:

Previous post:

Next post: