HostOnNet Blog


lsof

lsof is a linux command to list open files.

To list all files open by a process, run

lsof -p PID

Example

boby@hon-pc-01:~ $ lsof -p 10132
COMMAND   PID USER   FD      TYPE DEVICE SIZE/OFF    NODE NAME
tail    10132 boby  cwd       DIR   8,19     4096 2098166 /home/boby/www
tail    10132 boby  rtd       DIR   8,18     4096       2 /
tail    10132 boby  txt       REG   8,18    64432 4720044 /usr/bin/tail
tail    10132 boby  mem       REG   8,18  2981280 4731729 /usr/lib/locale/locale-archive
tail    10132 boby  mem       REG   8,18  1864888 3801314 /lib/x86_64-linux-gnu/libc-2.23.so
tail    10132 boby  mem       REG   8,18   162632 3801316 /lib/x86_64-linux-gnu/ld-2.23.so
tail    10132 boby    0u      CHR 136,13      0t0      16 /dev/pts/13
tail    10132 boby    1u      CHR 136,13      0t0      16 /dev/pts/13
tail    10132 boby    2u      CHR 136,13      0t0      16 /dev/pts/13
tail    10132 boby    3r      REG   8,18   262941 4194568 /var/log/syslog
tail    10132 boby    4r  a_inode   0,11        0    8136 inotify
boby@hon-pc-01:~ $ 

List all Connection on a Port

root@hon-vpn:~# lsof -i :80
COMMAND   PID     USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
apache2  6811 www-data    4u  IPv6 25414114      0t0  TCP *:http (LISTEN)
apache2 11035 www-data    4u  IPv6 25414114      0t0  TCP *:http (LISTEN)
apache2 11036 www-data    4u  IPv6 25414114      0t0  TCP *:http (LISTEN)
apache2 11037 www-data    4u  IPv6 25414114      0t0  TCP *:http (LISTEN)
apache2 11038 www-data    4u  IPv6 25414114      0t0  TCP *:http (LISTEN)
apache2 11039 www-data    4u  IPv6 25414114      0t0  TCP *:http (LISTEN)
apache2 25240     root    4u  IPv6 25414114      0t0  TCP *:http (LISTEN)
root@hon-vpn:~# 

lsof -P -iTCP

List all TCP connections

root@hon-vpn:~# lsof -P -iTCP
COMMAND     PID     USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
sshd        392     root    3u  IPv4      10961      0t0  TCP *:3333 (LISTEN)
sshd        392     root    4u  IPv6      10970      0t0  TCP *:3333 (LISTEN)
prometheu   394     root   21u  IPv6      12232      0t0  TCP *:9090 (LISTEN)
xinetd     6687     root    5u  IPv4    6405498      0t0  TCP *:5666 (LISTEN)
pure-ftpd  6750     root    4u  IPv4    6405818      0t0  TCP *:21 (LISTEN)
pure-ftpd  6750     root    5u  IPv6    6405819      0t0  TCP *:21 (LISTEN)
apache2    6811 www-data    4u  IPv6   25414114      0t0  TCP *:80 (LISTEN)
sshd       8551     root    3u  IPv4 1148099283      0t0  TCP vpn.hostonnet.com:3333->59.98.136.47:38238 (ESTABLISHED)
apache2   11035 www-data    4u  IPv6   25414114      0t0  TCP *:80 (LISTEN)
apache2   11036 www-data    4u  IPv6   25414114      0t0  TCP *:80 (LISTEN)
apache2   11037 www-data    4u  IPv6   25414114      0t0  TCP *:80 (LISTEN)
apache2   11038 www-data    4u  IPv6   25414114      0t0  TCP *:80 (LISTEN)
apache2   11039 www-data    4u  IPv6   25414114      0t0  TCP *:80 (LISTEN)
mysqld    16429    mysql   24u  IPv6   25405306      0t0  TCP *:3306 (LISTEN)
grafana-s 20491     root    5u  IPv6   16802620      0t0  TCP *:3000 (LISTEN)
grafana-s 20491     root    7u  IPv4 1148149055      0t0  TCP vpn.hostonnet.com:38987->220.222.178.107.bc.googleusercontent.com:443 (ESTABLISHED)
grafana-s 20491     root    9u  IPv4 1148149428      0t0  TCP vpn.hostonnet.com:47816->151.101.0.133:443 (ESTABLISHED)
squid3    20833    proxy    9u  IPv6   27325704      0t0  TCP *:3128 (LISTEN)
apache2   25240     root    4u  IPv6   25414114      0t0  TCP *:80 (LISTEN)
root@hon-vpn:~# 

Device or resource busy


Posted in Linux