cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Etc


Reference














.

os:linux:command:lsof.html



Linux Commands#System

How to use lsof command in Linux

Manual

# man lsof

DESCRIPTION
       An  open  file  may be a regular file, a directory, a block special file, a character special file,
       an executing text reference, a library, a stream or a network file (Internet socket, NFS file or UNIX domain socket.)
       A specific file or  all  the files in a file system may be selected by path.

OPTIONS
       In the absence of any options, lsof lists all open files belonging to all active processes.

       -i [i]   selects  the listing of files any of whose Internet address matches the address specified in i.
                An Internet address is specified in the form (Items in square brackets are optional.):
                [46][protocol][@hostname|hostaddr][:service|port]



       -a       This option causes list selection options to be ANDed, as described above.

       -p s     This option excludes or selects the listing of files for the processes whose
                optional process IDentification  (PID) numbers  are in the comma-separated set s - e.g., 123 or 123,^456.
                (There should be no spaces in the set.)

                PID numbers that begin with ^ (negation) represent exclusions.

       -D D     directs  lsof's  use  of the device cache file.

       -u s     selects the listing of files for the user whose login names or user ID numbers
                are in the comma-separated set s - e.g., ``abe'', or ``548,root''. 


Example

Which process is using the Port?

# lsof -i
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd     1   root   45u  IPv6  13815      0t0  TCP *:sunrpc (LISTEN)
systemd     1   root   46u  IPv4  13816      0t0  TCP *:sunrpc (LISTEN)
chronyd   645 chrony    1u  IPv4  14842      0t0  UDP localhost:323
chronyd   645 chrony    2u  IPv6  14843      0t0  UDP localhost:323
dhclient  963   root    6u  IPv4  15848      0t0  UDP *:bootpc
dhclient  963   root   20u  IPv4  15769      0t0  UDP *:54379
dhclient  963   root   21u  IPv6  15770      0t0  UDP *:33421
vsftpd   1126   root    3u  IPv6  16667      0t0  TCP *:ftp (LISTEN)
xinetd   1127   root    5u  IPv4  16742      0t0  UDP *:tftp
httpd    1132   root    4u  IPv6  17556      0t0  TCP *:http (LISTEN)
httpd    1132   root    6u  IPv6  17566      0t0  TCP *:https (LISTEN)
....
# lsof -i -P
COMMAND   PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd     1   root   45u  IPv6  13815      0t0  TCP *:111 (LISTEN)
systemd     1   root   46u  IPv4  13816      0t0  TCP *:111 (LISTEN)
chronyd   645 chrony    1u  IPv4  14842      0t0  UDP localhost:323
chronyd   645 chrony    2u  IPv6  14843      0t0  UDP localhost:323
dhclient  963   root    6u  IPv4  15848      0t0  UDP *:68
dhclient  963   root   20u  IPv4  15769      0t0  UDP *:54379
dhclient  963   root   21u  IPv6  15770      0t0  UDP *:33421
vsftpd   1126   root    3u  IPv6  16667      0t0  TCP *:21 (LISTEN)
xinetd   1127   root    5u  IPv4  16742      0t0  UDP *:69
httpd    1132   root    4u  IPv6  17556      0t0  TCP *:80 (LISTEN)
httpd    1132   root    6u  IPv6  17566      0t0  TCP *:443 (LISTEN)
# lsof -i :25
COMMAND   PID    USER   FD   TYPE DEVICE SIZE NODE NAME
master   4031    root   11u  IPv4   9638       TCP localhost.localdomain:smtp (LISTEN)
master   4031    root   14u  IPv4   9642       TCP test1.example.local:smtp (LISTEN)
smtpd   25830 postfix    6u  IPv4   9642       TCP test1.example.local:smtp (LISTEN)
#
# lsof -i :80,443
COMMAND  PID   USER   FD   TYPE DEVICE SIZE NODE NAME
httpd   2880   root    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2927 apache    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2928 apache    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2929 apache    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2930 apache    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2931 apache    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2932 apache    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2936 apache    3u  IPv6   8421       TCP *:http (LISTEN)
httpd   2938 apache    3u  IPv6   8421       TCP *:http (LISTEN)
#


Listing Files Opened By a PID

# lsof -p 3804
# lsof -a -p 3804


Count All Open File Handles

# lsof | wc -l


etc

# lsof /path/file
# lsof /var/log/httpd/access_log
# lsof -D /var/log/httpd/



os/linux/command/lsof.html.txt ยท Last modified: 2018/02/14 by admin

Page Tools