×


Linux LSOF Command - Explained with Examples



The lsof command stands for List Open Files and shows open files and which process uses them. 

Also, Since Linux sees every object as a file, such as devices, directories, etc., unidentified open files prevent users from modifying them.

Here at LinuxAPT, we shall look into the usage of the LSOF command.


LSOF Command syntax

The general syntax of this command is given below:

$ lsof [option]

Here, you can replace "option" with any of the options that are available with this command.


Examples of using LSOF Command in Linux

1. List All the Open Files in Linux

Here, we will be using the LSOF command in Linux for simply listing down all of the open files with the below command:

$ lsof

Running this command without any options prints the details about all the currently opened files on the terminal.


2. List All the Open Files Belonging to a Particular User in Linux

In this example, we will use the LSOF command to print the details about all the open files belonging to a particular Linux user since a Linux system can have multiple users at the same time.

The below command will do the job:

$ lsof –u system

The LSOF command with the "-u" flag followed by the username prints all the open files belonging to the specified user. 

Here, we wanted to print the open files belonging only to the Linux user named "system". You can replace it with the username of the user whose open files you wish to list down.

All the open files belonging to the specified user of our Linux system will be displayed.


3. List All the Open Files Associated With a Specific Process ID in Linux

In this example, we intend to print all the open files that are associated with a specific process ID. 

The below command for instance when executed will do the job:

$ lsof –p 1129

Whenever the LSOF command is paired up with the "-p" flag followed by a particular process ID, then all the open files associated with that particular process ID are printed on the terminal. 

You can replace "1129" with the process ID of the process whose associated open files you want to print.

All the open files associated with the specified process ID of our Linux system will be displayed.


4. List All the Open Files Associated With the Parent Processes in Linux

Now, we simply want to list down all those open files that are associated with the parent processes in Linux. The below command will do the Job:

$ lsof –R

The "-R" flag is used with the LSOF command for listing all the open files associated only with the parent processes in Linux.

All the open files associated with the parent processes of our Linux system will be displayed.


5. List All the Open Files Associated With the Network Connections Following a Particular Protocol in Linux

In this case, we will list down all the open files associated with the network connections of Linux that follow a particular protocol such as TCP or UDP. The below LSOF command will be helpful:

$ sudo lsof –i tcp

This command requires "sudo" privileges to execute. Moreover, you can simply replace "tcp" with "udp" if you want to display all the open files associated with this particular protocol.

All the open files of our Linux system belonging to the specified protocol will be displayed.


[Need help in fixing Linux system issues ? We can help you. ]


Conclusion

This article covers the usage of the LSOF command in Linux. In fact, With lsof, you can find different processes locking up a file or directory, a process listening on a port, a user's process list, what all files a process is locking.


How to install lsof in Linux ?

lsof isn't available by default on most Linux distributions but can be easily installed. So Use the below command to install lsof:

1. For CentOS / RHEL / Fedora, run the below command:

$ sudo yum install lsof

2. For CentOS/RHEL 8, you can use the DNF command:

$ sudo dnf install lsof

3. For Ubuntu / Debian, run the below command:

$ sudo apt install lsof


How to get lsof supported options ? 

You can get a summarized list of lsof supported options using -? or -h flag:

$ lsof -?

More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com