{"id":1512,"date":"2019-04-15T08:15:26","date_gmt":"2019-04-15T02:45:26","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1512"},"modified":"2019-04-30T10:42:12","modified_gmt":"2019-04-30T05:12:12","slug":"10-lsof-command-examples-in-linux","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/10-lsof-command-examples-in-linux\/","title":{"rendered":"10 lsof Command Examples in Linux"},"content":{"rendered":"\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>Description<\/strong><\/p>\n\n\n\n<p><strong>lsof<\/strong>&nbsp;meaning&nbsp;<strong>\u2018LiSt Open Files\u2019<\/strong>&nbsp;is used to find out which files are open by which process. <\/p>\n\n\n\n<p>As we all know&nbsp;<strong>Linux\/Unix<\/strong>&nbsp;considers everything as a files (<strong>pipes<\/strong>,&nbsp;<strong>sockets<\/strong>,&nbsp;<strong>directories<\/strong>,&nbsp;<strong>devices<\/strong>&nbsp;etc). <\/p>\n\n\n\n<p>One of the reason to use&nbsp;<strong>lsof<\/strong>&nbsp;command is when a disk cannot be unmounted as it says the files are being used. <\/p>\n\n\n\n<p>With the help of this command we can easily identify the files which are in use. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" style=\"text-align:left\"><strong>1.List all Open Files with lsof Command<\/strong><\/h4>\n\n\n\n<p>In the below example, it will show long listing of open files some of them are extracted for better understanding which displays the columns like&nbsp;<strong>Command<\/strong>,&nbsp;<strong>PID<\/strong>,&nbsp;<strong>USER<\/strong>,&nbsp;<strong>FD<\/strong>,&nbsp;<strong>TYPE<\/strong>&nbsp;etc.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof\n\nCOMMAND    PID      USER   FD      TYPE     DEVICE  SIZE\/OFF       NODE NAME\ninit         1      root  cwd      DIR      253,0      4096          2 \/\ninit         1      root  rtd      DIR      253,0      4096          2 \/\ninit         1      root  txt      REG      253,0    145180     147164 \/sbin\/init\ninit         1      root  mem      REG      253,0   1889704     190149 \/lib\/libc-2.12.so\ninit         1      root   0u      CHR        1,3       0t0       3764 \/dev\/null\ninit         1      root   1u      CHR        1,3       0t0       3764 \/dev\/null\ninit         1      root   2u      CHR        1,3       0t0       3764 \/dev\/null\ninit         1      root   3r     FIFO        0,8       0t0       8449 pipe\ninit         1      root   4w     FIFO       0,8       0t0       8449 pipe\ninit         1      root   5r      DIR       0,10         0          1 inotify\ninit         1      root   6r      DIR       0,10         0          1 inotify\ninit         1      root   7u     unix 0xc1513880       0t0       8450 socket<\/code><\/pre>\n\n\n\n<p>Sections and it\u2019s values are self-explanatory. However, we\u2019ll review&nbsp;<strong>FD &amp; TYPE<\/strong>&nbsp;columns more precisely.<\/p>\n\n\n\n<p><strong>FD<\/strong>&nbsp;\u2013 stands for File descriptor and may seen some of the values as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> <strong>cwd<\/strong>&nbsp;current working directory <\/li><li> <strong>rtd<\/strong>&nbsp;root directory <\/li><li> <strong>txt<\/strong>&nbsp;program text (code and data) <\/li><li> <strong>mem<\/strong>&nbsp;memory-mapped file <\/li><\/ul>\n\n\n\n<p>Also in <strong>FD<\/strong>&nbsp;column numbers like&nbsp;<strong>1u<\/strong>&nbsp;is actual file descriptor and followed by u,r,w of it\u2019s mode as: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> <strong>r<\/strong>&nbsp;for read access. <\/li><li> <strong>w<\/strong>&nbsp;for write access. <\/li><li> <strong>u<\/strong>&nbsp;for read and write access. <\/li><\/ul>\n\n\n\n<p><strong>TYPE<\/strong>&nbsp;\u2013 of files and it\u2019s identification. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> <strong>DIR<\/strong>&nbsp;\u2013 Directory <\/li><li> <strong>REG<\/strong>&nbsp;\u2013 Regular file <\/li><li> <strong>CHR<\/strong>&nbsp;\u2013 Character special file. <\/li><li> <strong>FIFO<\/strong>&nbsp;\u2013 First In First Out <\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>2. List User Specific Opened Files<\/strong><\/p>\n\n\n\n<p>The below command will display the list of all opened files of user&nbsp;<strong>jaipur<\/strong>.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -u jaipur\n\nCOMMAND  PID    USER   FD   TYPE     DEVICE SIZE\/OFF   NODE NAME\nsshd    1838 jaipur  cwd    DIR      253,0     4096      2 \/\nsshd    1838 jaipur  rtd    DIR      253,0     4096      2 \/\nsshd    1838 jaipur  txt    REG      253,0   532336 188129 \/usr\/sbin\/sshd\nsshd    1838 jaipur  mem    REG      253,0    19784 190237 \/lib\/libdl-2.12.so\nsshd    1838 jaipur  mem    REG      253,0   122436 190247 \/lib\/libselinux.so.1\nsshd    1838 jaipur  mem    REG      253,0   255968 190256 \/lib\/libgssapi_krb5.so.2.2\nsshd    1838 jaipur  mem    REG      253,0   874580 190255 \/lib\/libkrb5.so.3.3<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>3. Find Processes running on Specific Port<\/strong><\/p>\n\n\n\n<p>To find out all the running process of specific port, just use the following command with option&nbsp;<strong>-i<\/strong>. <\/p>\n\n\n\n<p>The below example will list all running process of port&nbsp;<strong>22<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -i TCP:22\n\nCOMMAND  PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nsshd    1471    root    3u  IPv4  12683      0t0  TCP *:ssh (LISTEN)\nsshd    1471    root    4u  IPv6  12685      0t0  TCP *:ssh (LISTEN)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>4. List Only IPv4 &amp; IPv6 Open Files<\/strong><\/p>\n\n\n\n<p>In below example shows only&nbsp;<strong>IPv4<\/strong>&nbsp;and&nbsp;<strong>IPv6<\/strong>&nbsp;network files open with separate commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -i 4\n\nCOMMAND    PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nrpcbind   1203     rpc    6u  IPv4  11326      0t0  UDP *:sunrpc\nrpcbind   1203     rpc    7u  IPv4  11330      0t0  UDP *:954\nrpcbind   1203     rpc    8u  IPv4  11331      0t0  TCP *:sunrpc (LISTEN)\navahi-dae 1241   avahi   13u  IPv4  11579      0t0  UDP *:mdns\navahi-dae 1241   avahi   14u  IPv4  11580      0t0  UDP *:58600\n\n# lsof -i 6\n\nCOMMAND    PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nrpcbind   1203     rpc    9u  IPv6  11333      0t0  UDP *:sunrpc\nrpcbind   1203     rpc   10u  IPv6  11335      0t0  UDP *:954\nrpcbind   1203     rpc   11u  IPv6  11336      0t0  TCP *:sunrpc (LISTEN)\nrpc.statd 1277 rpcuser   10u  IPv6  11858      0t0  UDP *:55800\nrpc.statd 1277 rpcuser   11u  IPv6  11862      0t0  TCP *:56428 (LISTEN)\ncupsd     1346    root    6u  IPv6  12112      0t0  TCP localhost:ipp (LISTEN)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>5. List Open Files of TCP Port ranges 1-1024<\/strong><\/p>\n\n\n\n<p>To list all the running process of open files of&nbsp;<strong>TCP<\/strong>&nbsp;Port ranges from&nbsp;<strong>1-1024<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -i TCP:1-1024\n\nCOMMAND  PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nrpcbind 1203     rpc   11u  IPv6  11336      0t0  TCP *:sunrpc (LISTEN)\ncupsd   1346    root    7u  IPv4  12113      0t0  TCP localhost:ipp (LISTEN)\nsshd    1471    root    4u  IPv6  12685      0t0  TCP *:ssh (LISTEN)\nmaster  1551    root   13u  IPv6  12898      0t0  TCP localhost:smtp (LISTEN)\nsshd    1834    root    3r  IPv4  15101      0t0  TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED)\nsshd    1838 tecmint    3u  IPv4  15101      0t0  TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED)\nsshd    1871    root    3r  IPv4  15842      0t0  TCP 192.168.0.2:ssh->192.168.0.1:groove (ESTABLISHED)\nhttpd   1918    root    5u  IPv6  15991      0t0  TCP *:http (LISTEN)\nhttpd   1918    root    7u  IPv6  15995      0t0  TCP *:https (LISTEN)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>6. Exclude User with \u2018^\u2019 Character<\/strong><\/p>\n\n\n\n<p>Here, we have excluded&nbsp;<strong>root<\/strong>&nbsp;user. <\/p>\n\n\n\n<p>You can exclude a particular user using&nbsp;<strong>\u2018^\u2019<\/strong>&nbsp;with command as shown above.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -i -u^root\n\nCOMMAND    PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nrpcbind   1203     rpc    6u  IPv4  11326      0t0  UDP *:sunrpc\nrpcbind   1203     rpc    7u  IPv4  11330      0t0  UDP *:954\nrpcbind   1203     rpc    8u  IPv4  11331      0t0  TCP *:sunrpc (LISTEN)\nrpcbind   1203     rpc    9u  IPv6  11333      0t0  UDP *:sunrpc\nrpcbind   1203     rpc   10u  IPv6  11335      0t0  UDP *:954\nrpcbind   1203     rpc   11u  IPv6  11336      0t0  TCP *:sunrpc (LISTEN)\navahi-dae 1241   avahi   13u  IPv4  11579      0t0  UDP *:mdns\navahi-dae 1241   avahi   14u  IPv4  11580      0t0  UDP *:58600\nrpc.statd 1277 rpcuser    5r  IPv4  11836      0t0  UDP *:soap-beep\nrpc.statd 1277 rpcuser    8u  IPv4  11850      0t0  UDP *:55146\nrpc.statd 1277 rpcuser    9u  IPv4  11854      0t0  TCP *:32981 (LISTEN)\nrpc.statd 1277 rpcuser   10u  IPv6  11858      0t0  UDP *:55800\nrpc.statd 1277 rpcuser   11u  IPv6  11862      0t0  TCP *:56428 (LISTEN)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>7. Find Out who\u2019s Looking What Files and Commands?<\/strong><\/p>\n\n\n\n<p>Below example shows user&nbsp;<span style=\"background-color: rgb(232, 234, 235);\"><b>jaipur <\/b><\/span>is using command like&nbsp;<strong>ping<\/strong>&nbsp;and&nbsp;<strong>\/etc<\/strong>&nbsp;directory .<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -i -u jaipur\n\nCOMMAND  PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nbash    1839 jaipur  cwd    DIR  253,0    12288   15 \/etc\nping    2525 jaipur  cwd    DIR  253,0    12288   15 \/etc<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>8. List all Network Connections<\/strong><\/p>\n\n\n\n<p>The following command with option&nbsp;<strong>\u2018-i\u2019<\/strong>&nbsp;shows the list of all network connections \u2018<strong>LISTENING &amp; ESTABLISHED\u2019<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -i\n\nCOMMAND    PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\nrpcbind   1203     rpc    6u  IPv4  11326      0t0  UDP *:sunrpc\nrpcbind   1203     rpc    7u  IPv4  11330      0t0  UDP *:954\nrpcbind   1203     rpc   11u  IPv6  11336      0t0  TCP *:sunrpc (LISTEN)\navahi-dae 1241   avahi   13u  IPv4  11579      0t0  UDP *:mdns\navahi-dae 1241   avahi   14u  IPv4  11580      0t0  UDP *:58600\nrpc.statd 1277 rpcuser   11u  IPv6  11862      0t0  TCP *:56428 (LISTEN)\ncupsd     1346    root    6u  IPv6  12112      0t0  TCP localhost:ipp (LISTEN)\ncupsd     1346    root    7u  IPv4  12113      0t0  TCP localhost:ipp (LISTEN)\nsshd      1471    root    3u  IPv4  12683      0t0  TCP *:ssh (LISTEN)\nmaster    1551    root   12u  IPv4  12896      0t0  TCP localhost:smtp (LISTEN)\nmaster    1551    root   13u  IPv6  12898      0t0  TCP localhost:smtp (LISTEN)\nsshd      1834    root    3r  IPv4  15101      0t0  TCP 192.168.0.2:ssh->192.168.0.1:conclave-cpp (ESTABLISHED)\nhttpd     1918    root    5u  IPv6  15991      0t0  TCP *:http (LISTEN)\nhttpd     1918    root    7u  IPv6  15995      0t0  TCP *:https (LISTEN)\nclock-app 2362   narad   21u  IPv4  22591      0t0  TCP 192.168.0.2:45284->www.gov.com:http (CLOSE_WAIT)\nchrome    2377   narad   61u  IPv4  25862      0t0  TCP 192.168.0.2:33358->maa03s04-in-f3.1e100.net:http (ESTABLISHED)\nchrome    2377   narad   80u  IPv4  25866      0t0  TCP 192.168.0.2:36405->bom03s01-in-f15.1e100.net:http (ESTABLISHED)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>9. Search by PID<\/strong><\/p>\n\n\n\n<p>The below example only shows whose&nbsp;<strong>PID<\/strong>&nbsp;is&nbsp;<strong>1<\/strong>&nbsp;[<strong>One<\/strong>].<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># lsof -p 1\n\nCOMMAND PID USER   FD   TYPE     DEVICE SIZE\/OFF   NODE NAME\ninit      1 root  cwd    DIR      253,0     4096      2 \/\ninit      1 root  rtd    DIR      253,0     4096      2 \/\ninit      1 root  txt    REG      253,0   145180 147164 \/sbin\/init\ninit      1 root  mem    REG      253,0  1889704 190149 \/lib\/libc-2.12.so\ninit      1 root  mem    REG      253,0   142472 189970 \/lib\/ld-2.12.so<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><\/h4>\n\n\n\n<p class=\"has-medium-font-size\"><strong>10. Kill all Activity of Particular User<\/strong><\/p>\n\n\n\n<p>Sometimes you may have to kill all the processes for a specific user. <\/p>\n\n\n\n<p>Below command will kills all the processes of&nbsp;<span style=\"background-color: rgb(232, 234, 235);\"><b>jaipur <\/b><\/span>user.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># kill -9 `lsof -t -u jaipur`<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;Here, it\u2019s not possible to give example of all available options, this guide is only to show how&nbsp;<strong>lsof<\/strong>command can be use. <\/p>\n\n\n\n<p>You may refer man page of&nbsp;<strong>lsof<\/strong> command to know more about it. <\/p>\n\n\n\n<p> <br>We hope you\u2019ve found this useful! <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description lsof&nbsp;meaning&nbsp;\u2018LiSt Open Files\u2019&nbsp;is used to find out which files are open by which process. As we all know&nbsp;Linux\/Unix&nbsp;considers everything as a files (pipes,&nbsp;sockets,&nbsp;directories,&nbsp;devices&nbsp;etc). One of the reason to use&nbsp;lsof&nbsp;command is when a disk cannot be unmounted as it says the files are being used. With the help of this command we can easily identify [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1513,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1512","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1512","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1512"}],"version-history":[{"count":10,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1512\/revisions"}],"predecessor-version":[{"id":1532,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1512\/revisions\/1532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1513"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1512"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1512"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1512"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}