{"id":13273,"date":"2019-04-01T10:59:17","date_gmt":"2019-04-01T10:59:17","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13273"},"modified":"2019-04-01T10:59:17","modified_gmt":"2019-04-01T10:59:17","slug":"how-to-find-out-who-is-using-a-file-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-find-out-who-is-using-a-file-in-linux\/","title":{"rendered":"How to Find Out Who is Using a File in Linux"},"content":{"rendered":"<p>In this article, we will explain how to find out who is using a particular file in Linux. This will help you know the system user or process that is using an open file.<\/p>\n<p>We can use the\u00a0<a href=\"https:\/\/www.tecmint.com\/10-lsof-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">lsof command<\/a>\u00a0to know if someone is using a file, and if they are, who. It reads kernel memory in its search for open files and helps you list all open files. In this case, an open file may be a regular file, a directory, a block special file, a character special file, a stream, a network file and many others \u2013 because in\u00a0<a href=\"https:\/\/www.tecmint.com\/explanation-of-everything-is-a-file-and-types-of-files-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">Linux everything is a file<\/a>.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/10-lsof-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">Lsof<\/a>\u00a0is used on a file system to identify who is using any files on that file system. You can run\u00a0<strong>lsof command<\/strong>\u00a0on Linux filesystem and the output identifies the owner and process information for processes using the file as shown in the following output.<\/p>\n<pre><strong>$ lsof \/dev\/null<\/strong>\r\n<\/pre>\n<div class=\"code-label\">List of All Opened Files in Linux<\/div>\n<pre>COMMAND    PID    USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\r\nsystemd   1480 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\nsh        1501 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\nsh        1501 tecmint    1w   CHR    1,3      0t0    6 \/dev\/null\r\ndbus-daem 1530 tecmint    0u   CHR    1,3      0t0    6 \/dev\/null\r\nxfce4-ses 1603 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\nxfce4-ses 1603 tecmint    1w   CHR    1,3      0t0    6 \/dev\/null\r\nat-spi-bu 1604 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\ndbus-daem 1609 tecmint    0u   CHR    1,3      0t0    6 \/dev\/null\r\nat-spi2-r 1611 tecmint    0u   CHR    1,3      0t0    6 \/dev\/null\r\nxfconfd   1615 tecmint    0u   CHR    1,3      0t0    6 \/dev\/null\r\nxfwm4     1624 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\nxfwm4     1624 tecmint    1w   CHR    1,3      0t0    6 \/dev\/null\r\nxfce4-pan 1628 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\nxfce4-pan 1628 tecmint    1w   CHR    1,3      0t0    6 \/dev\/null\r\nThunar    1630 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\nThunar    1630 tecmint    1w   CHR    1,3      0t0    6 \/dev\/null\r\nxfdesktop 1632 tecmint    0r   CHR    1,3      0t0    6 \/dev\/null\r\nxfdesktop 1632 tecmint    1w   CHR    1,3      0t0    6 \/dev\/null\r\n....\r\n<\/pre>\n<p>To list user specific opened files, run the following command replace\u00a0<code>tecmint<\/code>\u00a0with the actual user name.<\/p>\n<pre><strong>$ lsof -u tecmint<\/strong>\r\n<\/pre>\n<div class=\"code-label\">List of Files Opened by User<\/div>\n<pre>COMMAND    PID    USER   FD      TYPE             DEVICE  SIZE\/OFF       NODE NAME\r\nsystemd   1480 tecmint  cwd       DIR                8,3      4096          2 \/\r\nsystemd   1480 tecmint  rtd       DIR                8,3      4096          2 \/\r\nsystemd   1480 tecmint  txt       REG                8,3   1595792    3147496 \/lib\/systemd\/systemd\r\nsystemd   1480 tecmint  mem       REG                8,3   1700792    3150525 \/lib\/x86_64-linux-gnu\/libm-2.27.so\r\nsystemd   1480 tecmint  mem       REG                8,3    121016    3146329 \/lib\/x86_64-linux-gnu\/libudev.so.1.6.9\r\nsystemd   1480 tecmint  mem       REG                8,3     84032    3150503 \/lib\/x86_64-linux-gnu\/libgpg-error.so.0.22.0\r\nsystemd   1480 tecmint  mem       REG                8,3     43304    3150514 \/lib\/x86_64-linux-gnu\/libjson-c.so.3.0.1\r\nsystemd   1480 tecmint  mem       REG                8,3     34872    2497970 \/usr\/lib\/x86_64-linux-gnu\/libargon2.so.0\r\nsystemd   1480 tecmint  mem       REG                8,3    432640    3150484 \/lib\/x86_64-linux-gnu\/libdevmapper.so.1.02.1\r\nsystemd   1480 tecmint  mem       REG                8,3     18680    3150450 \/lib\/x86_64-linux-gnu\/libattr.so.1.1.0\r\nsystemd   1480 tecmint  mem       REG                8,3     18712    3150465 \/lib\/x86_64-linux-gnu\/libcap-ng.so.0.0.0\r\nsystemd   1480 tecmint  mem       REG                8,3     27112    3150489 \/lib\/x86_64-linux-gnu\/libuuid.so.1.3.0\r\nsystemd   1480 tecmint  mem       REG                8,3     14560    3150485 \/lib\/x86_64-linux-gnu\/libdl-2.27.so\r\n...\r\n<\/pre>\n<p>Another important use of\u00a0<strong>lsof<\/strong>\u00a0is to\u00a0<a href=\"https:\/\/www.tecmint.com\/find-out-which-process-listening-on-a-particular-port\/\" target=\"_blank\" rel=\"noopener noreferrer\">find out the process listening on a specific port<\/a>. For example identify the process listening on port\u00a0<strong>80<\/strong>\u00a0using the following command.<\/p>\n<pre><strong>$ sudo lsof -i TCP:80<\/strong>\r\n<\/pre>\n<div class=\"code-label\">Find Out Process Listening Port<\/div>\n<pre>COMMAND  PID   USER   FD   TYPE DEVICE SIZE\/OFF NODE NAME\r\nhttpd    903   root    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   1320 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   1481 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   1482 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   1493 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   1763 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   2027 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   2029 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   2044 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   3199 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\nhttpd   3201 apache    4u  IPv6  20222      0t0  TCP *:http (LISTEN)\r\n<\/pre>\n<p><strong>Note<\/strong>: Since\u00a0<strong>lsof<\/strong>\u00a0reads kernel memory in its search for open files, rapid changes in kernel memory may result into unpredictable outputs. This is one of the major downsides of using\u00a0<a href=\"https:\/\/www.tecmint.com\/10-lsof-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">lsof command<\/a>.<\/p>\n<p>For more information, look at the\u00a0<strong>lsof<\/strong>\u00a0man page:<\/p>\n<pre>$ man lsof\r\n<\/pre>\n<p>That\u2019s all! In this article, we have explained how to know who is using a particular file in Linux. We have shown how to identify the owner and process information for processes using an open file. Use the feedback form below to reach us for any questions or comments.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/find-out-who-is-using-a-file-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explain how to find out who is using a particular file in Linux. This will help you know the system user or process that is using an open file. We can use the\u00a0lsof command\u00a0to know if someone is using a file, and if they are, who. It reads kernel memory &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/how-to-find-out-who-is-using-a-file-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Find Out Who is Using a File in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13273","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13273"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13273\/revisions"}],"predecessor-version":[{"id":13274,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13273\/revisions\/13274"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}