Power of Linux lsof Command for Daily Usage in Linux

"lsof" stands for List Open Files. It is extremely useful for a system administrator to find out the details about files that have been opened and their respective processes. In Linux, everything is just a file - pipes, directories, inodes, or IP sockets. With the help of this command, you can monitor your system for what is actually going on, and also find out the details about files that are open in some particular partitions. This command helps the professional system administrator to perform advanced level tasks such as disk usage calculations, or mounting/unmounting of removal media etc.

Installing lsof

Considering the popularity and the wide range of utility of this command, almost all modern operating systems have it preinstalled. But if it's still missing on your Linux system, you can install it by running one of the following commands (depending on your Linux distribution type):

2) List processes using a mount point / partition

When trying to unmount some partition, you might have seen “Device or Resource Busy” error, it happens because that particular partition or mount point is still being used by some other processes. We need to use lsof to find out those processes and kill them before successfully unmounting the partition. In order to see which processes are using some particular partition, use lsof command followed by partition name as follows:

4) Find out processes using some particular port

Many times we find that some services cannot bind to a port since the port is already being used by other processes. In such cases, it is necessary to find that process using the port and kill it. Here is one example which may help:

lsof -i :25

5) List Network connections

You can use lsof to list network connections . Use 'option 'i' along with lsof to see these details.

8) Kill processes for some particular user

You can kill all processes belonging to some particular user by using the following command, replace USERNAME with your actual username:

kill -9 `lsof -t -u USERNAME

9) Find out Ports and Services Mapping

You can find out ports and their affiliated services mapping on a Linux system using lsof command. The following command will show you the mapping details for the various running services and their associated ports on IPV4 network interface:

12) Run lsof in repeat mode

We can run lsof in repeat mode too, which means it will display results once and then take break for few seconds and then it will display updated results once again on the basis of the same criteria. It is very useful for troubleshooting Linux system performance issues. Run lsof command followed by "-r" option and the delay interval in seconds.

lsof -i :25 -r5

Conclusion

We hope you found this article useful. In case you have any questions, please do let us know in the comments. Utilities like lsof are a real blessing for system administrators. It is a lightweight, easy to use tool which is used on a daily basis by thousands of system administrators to troubleshoot issues with Linux systems.

Related Posts

Splunk is one of the most powerful tool for exploring and searching data. It is one of the easiest, faster and secured way to search, analysis, collect and visualize massive data streams in realtime from applications, webservers, databases, server platforms, [...]

Peripheral component interface is a computer bus used for attaching the hardware devices in a computer. “lspci” command is used in Linux operating system to display all the information about these PCI buses of the system and devices which are [...]

All major Linux distributions come up with a base Python package. It ships with Python because the system actually needs it. But If you update the core Python interpreter, you risk breaking certain OS utilities, such as yum in CentOS. However, [...]