In this article we will try to teach you how to Use lsof to Track Down Open Files on Linux. LINUX is an operating system or kernel distributed under an open source license. Its functionality list is much like UNIX. The kernel is a program at the heart of the Linux operating system that takes care of basic things such as: B. to let hardware communicate with software. The lsof command stands for LiSt Open Files and shows open files and which process is using them. Since Linux considers every object like devices, directories, etc. as a file, unidentified open files prevent users from modifying them.
In addition, the sheer number of files makes it difficult to find malicious processes. The lsof command helps identify these processes so you can kill them. “lsof” stands for List Open Files. It is a Linux utility for listing all open files on a system. This command can be combined with various parameters to change its output as desired. Below we have mention the steps Use lsof to Open Files on Linux.
How to Open Files on Linux Using lsof
Installing lsof
lsof isn’t available by default on most Linux distributions but can be easily installed. Use the below command to install lsof:
$ sudo yum install lsof
- for CentOS/RHEL 8, you can use the DNF command
$ sudo dnf install lsof
$ sudo apt install lsof
Common Usage
List all open files
Running lsof without any options will list all files that are currently open by active processes.
$ sudo lsof | less
List by filename
To list all processes that have opened a specific file, we can specify file-name as an argument:
$ sudo lsof {file-name}
List open files by username
In a multi-user system, you can filter the list of files by specific user-owned processes, using -u flag followed by username.
$ sudo lsof -u {username}
List open files by process
We can also list files opened by a particular process by using -c option followed by the process name.
$ sudo lsof -c {process-name}
List open files by PID
Alternatively, to list files opened by a process but instead of process-name you want to specify its ID, you can use -p flag followed by process-id.
$ sudo lsof -p {process-id}
List open files containing directory
To list processes that opened files under a specific directory, use +D option followed by directory path.
$ sudo lsof +D {path}
Repeat mode
lsof can be run in repeat mode. In repeat mode, lsof will generate and print output at regular intervals. Again, there are two repeat modes supported by lsof, i.e., with -r and +r flags. With -r flag, lsof repeats to execute until it receives an interrupt/kill signal from the user while with +r flag, lsof repeat mode will end as soon as its output has no open files. Additionally, we can specify time delay with -r or +r flag.
$ sudo lsof {arguments} -r{time-interval}
List open files with network protocol
lsof supports the listing of any type of Linux files which includes network sockets etc. As such we can list details of open network connections using -i flag.
$ sudo lsof -i
List open files by port
We can also filter the output of lsof with -i flag by port number using command syntax as below:
$ sudo lsof -i :{port-number}
List open files by IPv4/IPv6
There’s an option to filter network connections listing by limiting it to either IPv4 or IPv6. Use below command syntax to get only IP v4 listing:
$ sudo lsof -i4
Final Words
We hope our article on how to Use lsof to Open Files on Linux distros will help you and resolve all your problems. Linux is a Unix-like, open-source, and community-developed operating system (OS) for computers, servers, mainframes, mobile devices, and embedded devices. It is supported on almost all major computing platforms including x86, ARM and SPARC, making it one of the most widely supported operating systems. If you want to know how to Open Files on Linux Using lsof then follow the steps mentioned above.
I hope you understand this article, How to Use lsof to Open Files on Linux.