JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

lsof Command in Linux

Author:JIYIK Last Updated:2025/03/23 Views:

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

We use lsofthe lsof command to verify the ports in use on the Linux operating system. Using this lsof command, we can find out whether a particular port is in use on the Linux operating system.

Let's start with the lsof command in Linux.


lsof Command in Linux

lsofRepresents the List Open Files command in Linux which is used to determine if a port is in use . This command returns the process to the user who is using the system file in Linux.

It can help us determine why the file system is still in use and cannot be unmounted.

As we all know, Linux operating system treats everything as files and folders. So the files or folders used in Linux system are very important.

When we work on Linux Operating System, we use several files and folders. Some of these files or folders are visible to the user while some are not.

lsof command provides a list of files or folders opened on Linux operating system. This command provides us with information to find which process in the system has opened a file.

In just one step, it will list all the open files in the output console. The lsof command can not only list common regular files, but also directories, shared libraries, special files in the system, and more.

lsofcommand can be used in conjunction with the grep command for advanced searching and listing of files in Linux.


List Ports in Linux Using lsof Command

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

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

$ sudo apt install lsof

Install lsof utility in Linux systems

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


List All Open Files in Linux Using lsof Command

As Linux users, sometimes we want to know the port number that a certain file has opened for a particular process. All Linux operating system ports are associated with a process ID or a service.

We will see different methods that can be used to determine which port a process has a file open on.

As shown in the following example, we have 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 as shown in the following image.

Sample code:

$ lsof

Use lsof command to list files and ports

The above image shows the details of an opened file. The details include the process ID, user associated with the process, FD (file descriptor), and file size.

All of these provide detailed information about the files opened by the command, process ID, user, etc.

Use lsof command in Linux to list all files opened by a user in case more than one user uses the Linux system. Then everyone has different requirements and they use different files.

If we want to find out the list of files opened by a particular user, we use the following command.

grammar:

$ lsof -u username

Example code:

$ lsof -u abid

Use lsof -u to list all files opened by the user

The above image shows that the command lsof -u abidlists all files opened by the user named abid.

We can also see 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 in Linux Using lsof Command

To list all Internet and network files, we use the -i option with the lsof command. We use the lsof command in the terminal as follows:

$ sudo lsof -i

Use the lsof -i command to list all Internet files

After running lsof -ithe command, we can see that it displays the service name and the numeric port in the output.


List all files by specific process using lsof Command in Linux

For example, let's say a particular process called Mysql is running and we want to find out all the files opened by this process. We can find this out by using the lsof command:

$ lsof -c Mysql

This lsofcommand will list all the files opened by a particular process specified in the command. In this way, we can find the files opened by any process just by giving the process name in the command.


Find Processes Running on a Specific Port in Linux Using lsof Command

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

We can find the process running on any particular port by just providing the port number in the command as shown in the sample code.

Sample code:

$ sudo lsof -i :30

Check listening ports in Linux using lsof command

In Linux systems, network ports are identified by their number, IP address, and the type of protocol used for communication, such as TCP or UDP. There are different ways to view listening ports in Linux, and in this article, we will use the lsof command to view listening ports.

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

Command to display all TCP ports running on our system:

$ sudo lsof -nP -iTCP -sTCP:LISTEN

In the command above, we use -n to not change the port number to a name. We also use -p to display the numeric address of the port.

We use -iTCP -sTCP:LISTENto display only network files with TCP status.

Output:

The lsof command is used to display a list of all TCP ports.

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 this article helped you understand how to use lsof command to list processes or files in Linux.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

ps aux command in Linux

Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM

If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou

NTP in Linux

Publish Date:2025/03/23 Views:58 Category:OPERATING SYSTEM

NTP is a core protocol used by most IT infrastructures. Its purpose is to synchronize date and time information. However, it is very important to configure NTP for your servers, clients, and other network devices. NTP uses a UDP port number

Creating a Progress Bar in Bash

Publish Date:2025/03/23 Views:94 Category:OPERATING SYSTEM

A progress bar is a visual indicator that shows the progress of a task, such as a long-running script or command. It can be used to provide feedback to the user about the status of a task and can also help estimate the time remaining before

Redirecting Stderr and Stdout to a file in Bash

Publish Date:2025/03/23 Views:187 Category:OPERATING SYSTEM

In this article, we will discuss standard output and standard error in Linux. We will see how to redirect standard output and standard error in Bash. Let us start by understanding the terms standard output and standard error in Linux. Stand

Deleting Duplicate Lines in Bash

Publish Date:2025/03/23 Views:123 Category:OPERATING SYSTEM

Duplicate entries can cause a variety of problems in Bash scripts, such as incorrect or inconsistent results, and they can also make the script difficult to maintain. Removing duplicate entries from a script is often necessary to avoid thes

Count unique lines in a file in Linux

Publish Date:2025/03/23 Views:190 Category:OPERATING SYSTEM

Counting unique lines in a file is a common task in Linux, and there are a number of different tools and methods that can be used to perform this operation. In general, the appropriate method depends on the specific requirements and constra

Counting files in a directory in Bash

Publish Date:2025/03/23 Views:178 Category:OPERATING SYSTEM

Counting how many files are in a directory is a common task in Bash, and there are a number of different tools and methods that can be used to perform this operation. In general, the appropriate method depends on the specific requirements a

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial