Table of Contents
Introduction
lsof command meaning “List open files“. This command will not find CentOS7/RHEL. We will install lsof command example as below:
$ sudo yum install lsof
In the realm of Linux administration, understanding the tools at your disposal is key to effective system management. The lsof command, which stands for “List Open Files”, is an indispensable utility that provides crucial visibility into the system’s file usage. By listing information about files opened by processes, lsof helps administrators manage resources, troubleshoot system issues, and ensure secure operations. This guide aims to demystify the lsof command through practical examples, enhancing your system management toolkit.
Basic Usage
lsof
This will display a list of all open files and the processes that are using them.
lsof command examples
List open files
$ lsof -n
Kill a process running on port 8443
$ lsof -i :8443 | awk '{print $2}' | tail -n 1 | xargs kill
# or
$ lsof -i :8443 | awk 'NR > 1 {print $2}' | xargs --no-run-if-empty kill
Show the 15 Largest Open Files in Linux.
$ lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB" " " $9 " " $1 }' | sort -n -u | tail -n 15
List User-Specific Opened Files. This will display a list of all open files that are being used by the specified user.
$ lsof -u huupv
Search by PID
$ lsof -p 1
Exclude User with ^ Character
$ lsof -i -u^root
List TCP Port ranges 8000-9000
$ lsof -i TCP:8000-9000
Conclusion
The lsof command is a powerful tool in the Linux administrator’s arsenal, offering deep insights into the system’s interaction with files. From tracking down process-specific files to managing system resources, lsof facilitates a wide range of administrative tasks.
By mastering its usage through the examples provided, you enhance your capabilities in system management, contributing to the overall efficiency and security of your operations. Dive into these examples to leverage lsof effectively, ensuring your Linux systems run smoothly and securely. I hope will this your helpful. Thank you for reading the DevopsRoles page!