lsof Command in Linux
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 lsof
the 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
lsof
Represents 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.
lsof
command 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
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
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
The above image shows that the command lsof -u abid
lists 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
After running lsof -i
the 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 lsof
command 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:LISTEN
to display only network files with TCP status.
Output:
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.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u