Table of Contents

Linux lsof Tutorial for Beginners With Examples

lsof is great utility. lsof is list of open files. Before we delve into utilization of lsof, lets check if lsof is available in our environment or not.

lsof
bash: lsof: command not found

Ok we got the error "bash: lsof: command not found". Lets install it first.

yum -y install lsof

If we just run lsof, we will get list of all the open files. Lets run lsof and pipe it to less. Checkout the snapshot below.

How to list files opened by specific user

Let say we want to list all the opened files by user root. Run following command.

lsof -u root | less

How to list opened files by ip address

For ipv4 addresses. use -i 4

lsof -i 4

For ipv6 addresses use -i 6 option

lsof -i 6

To find a specific ip address just pipe the result of above command to egrep as shown below.

lsof -i 4 | egrep '218.92.0.145'

Checkout more about how to use lsof to detect listening ports

How to list opened files by specific process id

lsof -p pid

lsof -p 12

The result of above command is shown in the snapshot below.

How to list process ids of opened file

Lets say we want to know the file which is opened and we want to find all the processes that are opened for a file.

example: find all the processes for the opened file /run/systemd/journal/socket

lsof -t /run/systemd/journal/socket

As we see in the above snapshot, the process ids associated with file /run/systemd/journal/socket are 1 and 528

How to find opened files for listening port range

Lets say we want to find all the files which are opened for the ports ranging from 8000 to 8005.

lsof -i :8000-8005

Please checkout the snapshot below to see the output of above command.

Wrap up!

lsof is very useful command to find lot of detailed information about the opened files by different Linux processes. I hope above examples will help getting started.

Related Topics:

lsof detect listening port on Linux

netstat detect listening port on Linux


Related Posts

1
2
3
4
5