Linux How-To

How you can Discover Out Who’s Utilizing a File in Linux

On this article, we are going to clarify learn how to discover out who’s utilizing a selected file in Linux. This may assist the system consumer or course of that’s utilizing an open file.

We are able to use the lsof command to know if somebody is utilizing a file, and if they’re, who. It reads kernel reminiscence in its seek for open information and helps you checklist all open information. On this case, an open file could also be an everyday file, a listing, a block particular file, a personality particular file, a stream, a community file and plenty of others – as a result of in Linux every part is a file.

Lsof is used on a file system to determine who’s utilizing any information on that file system. You’ll be able to run lsof command on Linux filesystem and the output identifies the proprietor and course of info for processes utilizing the file as proven within the following output.

$ lsof /dev/null

Record of All Opened Information in Linux

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1480 tecmint 0r CHR 1,three 0t0 6 /dev/null
sh 1501 tecmint 0r CHR 1,three 0t0 6 /dev/null
sh 1501 tecmint 1w CHR 1,three 0t0 6 /dev/null
dbus-daem 1530 tecmint 0u CHR 1,three 0t0 6 /dev/null
xfce4-ses 1603 tecmint 0r CHR 1,three 0t0 6 /dev/null
xfce4-ses 1603 tecmint 1w CHR 1,three 0t0 6 /dev/null
at-spi-bu 1604 tecmint 0r CHR 1,three 0t0 6 /dev/null
dbus-daem 1609 tecmint 0u CHR 1,three 0t0 6 /dev/null
at-spi2-r 1611 tecmint 0u CHR 1,three 0t0 6 /dev/null
xfconfd 1615 tecmint 0u CHR 1,three 0t0 6 /dev/null
xfwm4 1624 tecmint 0r CHR 1,three 0t0 6 /dev/null
xfwm4 1624 tecmint 1w CHR 1,three 0t0 6 /dev/null
xfce4-pan 1628 tecmint 0r CHR 1,three 0t0 6 /dev/null
xfce4-pan 1628 tecmint 1w CHR 1,three 0t0 6 /dev/null
Thunar 1630 tecmint 0r CHR 1,three 0t0 6 /dev/null
Thunar 1630 tecmint 1w CHR 1,three 0t0 6 /dev/null
xfdesktop 1632 tecmint 0r CHR 1,three 0t0 6 /dev/null
xfdesktop 1632 tecmint 1w CHR 1,three 0t0 6 /dev/null
….

To checklist consumer particular opened information, run the next command change tecmint with the precise consumer identify.

$ lsof -u tecmint

Record of Information Opened by Person

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1480 tecmint cwd DIR eight,three 4096 2 /
systemd 1480 tecmint rtd DIR eight,three 4096 2 /
systemd 1480 tecmint txt REG eight,three 1595792 3147496 /lib/systemd/systemd
systemd 1480 tecmint mem REG eight,three 1700792 3150525 /lib/x86_64-linux-gnu/libm-2.27.so
systemd 1480 tecmint mem REG eight,three 121016 3146329 /lib/x86_64-linux-gnu/libudev.so.1.6.9
systemd 1480 tecmint mem REG eight,three 84032 3150503 /lib/x86_64-linux-gnu/libgpg-error.so.zero.22.zero
systemd 1480 tecmint mem REG eight,three 43304 3150514 /lib/x86_64-linux-gnu/libjson-c.so.three.zero.1
systemd 1480 tecmint mem REG eight,three 34872 2497970 /usr/lib/x86_64-linux-gnu/libargon2.so.zero
systemd 1480 tecmint mem REG eight,three 432640 3150484 /lib/x86_64-linux-gnu/libdevmapper.so.1.02.1
systemd 1480 tecmint mem REG eight,three 18680 3150450 /lib/x86_64-linux-gnu/libattr.so.1.1.zero
systemd 1480 tecmint mem REG eight,three 18712 3150465 /lib/x86_64-linux-gnu/libcap-ng.so.zero.zero.zero
systemd 1480 tecmint mem REG eight,three 27112 3150489 /lib/x86_64-linux-gnu/libuuid.so.1.three.zero
systemd 1480 tecmint mem REG eight,three 14560 3150485 /lib/x86_64-linux-gnu/libdl-2.27.so

One other essential use of lsof is to seek out out the method listening on a selected port. For instance determine the method listening on port 80 utilizing the next command.

$ sudo lsof -i TCP:80

Discover Out Course of Listening Port

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 903 root 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 1320 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 1481 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 1482 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 1493 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 1763 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 2027 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 2029 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 2044 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 3199 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)
httpd 3201 apache 4u IPv6 20222 0t0 TCP *:http (LISTEN)

Be aware: Since lsof reads kernel reminiscence in its seek for open information, speedy modifications in kernel reminiscence might end result into unpredictable outputs. This is among the main downsides of utilizing lsof command.

For extra info, have a look at the lsof man web page:

$ man lsof

That’s all! On this article, we now have defined learn how to know who’s utilizing a selected file in Linux. We have now proven learn how to determine the proprietor and course of info for processes utilizing an open file. Use the suggestions kind under to achieve us for any questions or feedback.

Source link

Related Articles

Back to top button