The lsof Command in Linux

Abid Ullah Jul 27, 2022
  1. the lsof Command in Linux
  2. Use the lsof Command to List Ports in Linux
  3. List All Open Files Using the lsof Command in Linux
  4. List All Open Files by a User Using the lsof Command in Linux
  5. List All Internet Files Using the lsof Command in Linux
  6. List All Files by a Specific Process Using the lsof Command in Linux
  7. Find Processes Running on Specific Ports Using the lsof Command in Linux
  8. Check the Listening Ports Using the lsof Command in Linux
The lsof Command in Linux

In this Linux article, we will learn about the lsof command in Linux operating system. We will see how to use this command for different purposes in Linux.

We use the lsof command to verify ports that are in use on Linux operating system. Using this lsof command, we can find out if a port is in use or not on Linux operating system.

Let’s begin with what the lsof command is in Linux.

the lsof Command in Linux

The lsof stands for the List Open Files command used in Linux to determine whether a port is in use or not. This command returns processes to the user used by the system file in Linux.

It helps us to determine why a file system remains in use and cannot be unmounted.

As we know, Linux operating system considers everything as a file and folder. So the file or a folder used in the Linux system is very important.

Several files and folders are used when we work on a Linux operating system. Some of these files or folders would be visible to the user, and some would not.

The lsof command provides a list of files or folders that are opened on the Linux operating system. This command gives us information to find the files that are opened by which process in the system.

With just one go, it lists all the open files in the output console. The lsof command not only lists common regular files, but it can also list directories, shared libraries, special files in the system, and many other things.

The lsof command can be combined with the grep command and used for advanced searching and listing files in Linux.

Use the lsof Command to List Ports in Linux

The lsof command helps us list all the open files or folders on our Linux system. So, we can use this command to view all the processes open on a specific port in our Linux system.

To use the lsof command, we need to install the lsof utility if it is not already installed on our Linux system by using the following command in the terminal.

$ sudo apt install lsof

Installing lsof utility in the Linux system

After installing the utility of the lsof command on our system, we can now use it to list files and ports in our Linux system.

List All Open Files Using the lsof Command in Linux

As Linux users, sometimes we want to know what port number a specific process is opened by a file. All the Linux operating system ports are associated with a process ID or service.

We will see different methods we can use to determine which port a file opens a process.

As shown in the example below, we used the lsof command. It will show all the files opened by any process in our Linux system.

As we can see, it shows a long list of files opened by processes in the Linux system shown in the image below.

Example code:

$ lsof

Using the lsof command to list files and ports

The above figure shows that there are details of files that are opened. The details include processes ID, the user associated with the process, FD (File Descriptor), and the file size.

All this gives detailed information about the file opened by the command, process ID, user, etc.

List All Open Files by a User Using the lsof Command in Linux

In case more than one user is using a Linux system. Then each has different requirements, and they use different files.

If we want to find out a list of files that a specific user opens, we use the following command.

Syntax:

$ lsof -u username

Example code:

$ lsof -u abid

Using the lsof -u listing all the files opened by a user

The above figure shows that the command lsof -u abid lists out all the files opened by a user named abid.

We can also see the different types of files (CHR, REG, DIR). CHR is a special character file, REG is a regular file, and DIR is a directory.

List All Internet Files Using the lsof Command in Linux

To list all internet and network files, we use the -i option with the lsof command. We use the lsof command in the terminal like this:

$ sudo lsof -i

Using the lsof-i command to list all internet files

After running the lsof -i command, we can see that it shows service names and numeric ports in the output.

List All Files by a Specific Process Using the lsof Command in Linux

For example, a specific process called Mysql is running, and we want to find out all files opened by this process. We can find that out by using the lsof command like this:

$ lsof -c Mysql

This lsof command will list all the files opened by a specific process named in the command. This way, we can find files opened by any process just by giving the process name in the command.

Find Processes Running on Specific Ports Using the lsof Command in Linux

If we want to discover all the running processes on a port, we use the following lsof command with option -i. The below example lists all the running processes of port 30.

We can find processes running on any specific port just by giving the port number in the command, as shown in the example code.

Example code:

$ sudo lsof -i :30

Check the Listening Ports Using the lsof Command in Linux

In the Linux system, the network port is identified by its number, IP address, and type of protocol used for communication, such as TCP or UDP. We can check the listening ports in different ways in Linux, and in this article, we will use the lsof command to check the listening ports.

As we know, the lsof is a powerful command in Linux that can provide information about files opened by processes. In Linux, we use the terminal’s lsof command to get all the listening TCP ports.

Command used to show all the TCP ports running on our system:

$ sudo lsof -nP -iTCP -sTCP:LISTEN

In the above command, we used -n to not change the port numbers into names. We also used -p to show the numerical addresses of the ports.

We used -iTCP -sTCP:LISTEN to show only network files with TCP state.

Output:

The lsof Command used to show all TCP ports list

After using the above command in the terminal, we can see all the TCP ports running in our Linux system in the output.

We hope you find this article helpful in understanding how to use the lsof command in Linux to list processes or files.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.

LinkedIn