That is our ongoing sequence of Linux instructions and on this article, we’re going to evaluate lsof command with sensible examples. lsof that means ‘LiSt Open Information’ is used to seek out out which information are open by which Linux course of.
As everyone knows Linux/Unix considers every part as a file (pipes, sockets, directories, units, and many others). One of many causes to make use of the lsof command is when a disk can’t be unmounted because it says the information are getting used. With the assistance of lsof command, we will simply establish the information that are in use.
1. Checklist All Open Information with lsof Command
Within the beneath instance, it would present a lengthy itemizing of open information a few of them are extracted for higher understanding which shows the columns like Command, PID, USER, FD, TYPE, and many others.
# lsof COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root cwd DIR 253,0 4096 2 / init 1 root rtd DIR 253,0 4096 2 / init 1 root txt REG 253,0 145180 147164 /sbin/init init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so init 1 root 0u CHR 1,3 0t0 3764 /dev/null init 1 root 1u CHR 1,3 0t0 3764 /dev/null init 1 root 2u CHR 1,3 0t0 3764 /dev/null init 1 root 3r FIFO 0,8 0t0 8449 pipe init 1 root 4w FIFO 0,8 0t0 8449 pipe init 1 root 5r DIR 0,10 0 1 inotify init 1 root 6r DIR 0,10 0 1 inotify init 1 root 7u unix 0xc1513880 0t0 8450 socket
Sections and their values are self-explanatory. Nevertheless, we’ll evaluate FD & TYPE columns extra exactly.
FD – stands for a File descriptor and might even see a few of the values as:
- cwd present working listing
- rtd root listing
- txt program textual content (code and information)
- mem memory-mapped file
Additionally in FD column numbers like 1u is precise file descriptor and adopted by u,r,w of its mode as:
- r for learn entry.
- w for write entry.
- u for learn and write entry.
TYPE – of information and it’s identification.
- DIR – Listing
- REG – Common file
- CHR – Character particular file.
- FIFO – First In First Out
2. Checklist Person Particular Opened Information
The beneath command will show the listing of all opened information of consumer tecmint.
# lsof -u tecmint COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1838 tecmint cwd DIR 253,0 4096 2 / sshd 1838 tecmint rtd DIR 253,0 4096 2 / sshd 1838 tecmint txt REG 253,0 532336 188129 /usr/sbin/sshd sshd 1838 tecmint mem REG 253,0 19784 190237 /lib/libdl-2.12.so sshd 1838 tecmint mem REG 253,0 122436 190247 /lib/libselinux.so.1 sshd 1838 tecmint mem REG 253,0 255968 190256 /lib/libgssapi_krb5.so.2.2 sshd 1838 tecmint mem REG 253,0 874580 190255 /lib/libkrb5.so.3.3
3. Discover Processes Operating on Particular Port
To search out out all of the working Linux processes of a selected port, simply use the next command with possibility -i. The beneath instance will listing all of the working processes of port 22.
# lsof -i TCP:22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN) sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN)
4. Checklist Solely IPv4 & IPv6 Open Information
In beneath instance reveals solely IPv4 and IPv6 community information open with separate instructions.
# lsof -i 4 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954 rpcbind 1203 rpc 8u IPv4 11331 0t0 TCP *:sunrpc (LISTEN) avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600 # lsof -i 6 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 9u IPv6 11333 0t0 UDP *:sunrpc rpcbind 1203 rpc 10u IPv6 11335 0t0 UDP *:954 rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) rpc.statd 1277 rpcuser 10u IPv6 11858 0t0 UDP *:55800 rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN) cupsd 1346 root 6u IPv6 12112 0t0 TCP localhost:ipp (LISTEN)
5. Checklist Open Information of TCP Port Ranges 1-1024
To listing all of the working technique of open information of TCP Port ranges from 1-1024.
# lsof -i TCP:1-1024 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN) sshd 1471 root 4u IPv6 12685 0t0 TCP *:ssh (LISTEN) grasp 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp (LISTEN) sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED) sshd 1838 tecmint 3u IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED) sshd 1871 root 3r IPv4 15842 0t0 TCP 192.168.0.2:ssh->192.168.0.1:groove (ESTABLISHED) httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN) httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN)
6. Exclude Person with ‘^’ Character
Right here, we now have excluded the root consumer. You may exclude a specific consumer utilizing ‘^’ with the command as proven above.
# lsof -i -u^root COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954 rpcbind 1203 rpc 8u IPv4 11331 0t0 TCP *:sunrpc (LISTEN) rpcbind 1203 rpc 9u IPv6 11333 0t0 UDP *:sunrpc rpcbind 1203 rpc 10u IPv6 11335 0t0 UDP *:954 rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600 rpc.statd 1277 rpcuser 5r IPv4 11836 0t0 UDP *:soap-beep rpc.statd 1277 rpcuser 8u IPv4 11850 0t0 UDP *:55146 rpc.statd 1277 rpcuser 9u IPv4 11854 0t0 TCP *:32981 (LISTEN) rpc.statd 1277 rpcuser 10u IPv6 11858 0t0 UDP *:55800 rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN)
7. Discover Out who’s Trying What Information and Instructions?
The beneath instance reveals consumer tecmint is utilizing instructions like ping and /and many others listing.
# lsof -i -u tecmint COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 1839 tecmint cwd DIR 253,0 12288 15 /and many others ping 2525 tecmint cwd DIR 253,0 12288 15 /and many others
8. Checklist all Community Connections
The next command with possibility ‘-i’ reveals the listing of all community connections ‘LISTENING & ESTABLISHED’.
# lsof -i COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 1203 rpc 6u IPv4 11326 0t0 UDP *:sunrpc rpcbind 1203 rpc 7u IPv4 11330 0t0 UDP *:954 rpcbind 1203 rpc 11u IPv6 11336 0t0 TCP *:sunrpc (LISTEN) avahi-dae 1241 avahi 13u IPv4 11579 0t0 UDP *:mdns avahi-dae 1241 avahi 14u IPv4 11580 0t0 UDP *:58600 rpc.statd 1277 rpcuser 11u IPv6 11862 0t0 TCP *:56428 (LISTEN) cupsd 1346 root 6u IPv6 12112 0t0 TCP localhost:ipp (LISTEN) cupsd 1346 root 7u IPv4 12113 0t0 TCP localhost:ipp (LISTEN) sshd 1471 root 3u IPv4 12683 0t0 TCP *:ssh (LISTEN) grasp 1551 root 12u IPv4 12896 0t0 TCP localhost:smtp (LISTEN) grasp 1551 root 13u IPv6 12898 0t0 TCP localhost:smtp (LISTEN) sshd 1834 root 3r IPv4 15101 0t0 TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED) httpd 1918 root 5u IPv6 15991 0t0 TCP *:http (LISTEN) httpd 1918 root 7u IPv6 15995 0t0 TCP *:https (LISTEN) clock-app 2362 narad 21u IPv4 22591 0t0 TCP 192.168.0.2:45284->www.gov.com:http (CLOSE_WAIT) chrome 2377 narad 61u IPv4 25862 0t0 TCP 192.168.0.2:33358->maa03s04-in-f3.1e100.web:http (ESTABLISHED) chrome 2377 narad 80u IPv4 25866 0t0 TCP 192.168.0.2:36405->bom03s01-in-f15.1e100.web:http (ESTABLISHED)
9. Search by PID
The beneath instance solely reveals whose PID is 1 [One].
# lsof -p 1 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root cwd DIR 253,0 4096 2 / init 1 root rtd DIR 253,0 4096 2 / init 1 root txt REG 253,0 145180 147164 /sbin/init init 1 root mem REG 253,0 1889704 190149 /lib/libc-2.12.so init 1 root mem REG 253,0 142472 189970 /lib/ld-2.12.so
10. Kill all Exercise of Explicit Person
Generally you will have to kill all of the processes for a selected consumer. The beneath command will kill all of the processes of the tecmint consumer.
# kill -9 `lsof -t -u tecmint`
Word: Right here, it’s not doable to present examples of all obtainable choices, this information is simply to indicate how lsof command can be utilized. You could refer man web page of lsof command to know extra about it. Please share it in case you discover this text is beneficial by way of our remark field beneath.
If You Admire What We Do Right here On TecMint, You Ought to Contemplate:
TecMint is the quickest rising and most trusted neighborhood web site for any form of Linux Articles, Guides and Books on the internet. Thousands and thousands of individuals go to TecMint! to go looking or browse the 1000’s of printed articles obtainable FREELY to all.
For those who like what you’re studying, please take into account shopping for us a espresso ( or 2 ) as a token of appreciation.
We’re grateful to your by no means ending assist.