lsof Command in Linux



The lsof command in Linux is a powerful tool that stands for "List Open Files." It provides detailed information about files that are currently open by various processes. This command is invaluable for system administrators and developers alike, as it helps in troubleshooting, monitoring, and managing system resources.

Table of Contents

Here is a comprehensive guide to the options available with the lsof command −

Understanding lsof Command

The lsof command lists information about files that are open by processes. In Unix-like operating systems, everything is considered a file, including directories, devices, and network sockets. This makes lsof a versatile tool for gathering information about system activity and diagnosing issues.

Syntax of lsof Command

The basic syntax for the lsof command is −

lsof [options] [file/directory/PID]

Without any options, the command lists all open files.

lsof Command Options

Here are some of the most commonly used options with the lsof command −

Options Description
-a Combines multiple search criteria.
-c [string] Lists all files opened by a particular command.
d [FD] Lists files using a specific file descriptor.
-i [4/6] − Displays files opened by IPv4/IPv6 network connections.
-n Disables hostname resolution.
-p [PID] − Lists files opened by a specific process ID.
-r [seconds] − Repeats the listing every specified number of seconds.
-t − Outputs only the process IDs.
-u [username] − Lists files opened by a specific user.
-V Displays the version of lsof.

Examples of lsof Command in Linux

Let's explore some practical examples to understand how to use the lsof command effectively.

List All Open Files

This command lists all open files by all processes. The output includes columns like COMMAND, PID, USER, FD (file descriptor), TYPE, DEVICE, SIZE/OFF, NODE, and NAME −

lsof
lsof Command in Linux1

List Files Opened by a Specific User

Replace username with the actual username to list all files opened by that user −

lsof -u username
lsof Command in Linux2

List Files Opened by a Specific Process

Replace PID with the actual process ID to list all files opened by that process −

lsof -p PID
lsof Command in Linux3

List Files Opened by a Specific Command

Replace command with the name of the command to list all files opened by it −

lsof -c command
lsof Command in Linux4

List Network Connections

This command lists all files opened by network connections. You can specify IPv4 or IPv6 by using -i 4 or -i 6

lsof -i
lsof Command in Linux5

List Files Opened on a Specific Port

Replace port with the actual port number to list files opened on that port −

lsof -i :port
lsof Command in Linux6

List Files Opened by TCP Connections

This command lists all files opened by TCP connections −

lsof -i tcp
lsof Command in Linux7

List Files Opened by UDP Connections

This command lists all files opened by UDP connections −

lsof -i udp
lsof Command in Linux8

List Files in a Specific Directory

Replace /path/to/directory with the actual directory path to list all files opened in that directory −

lsof +D /path/to/directory
lsof Command in Linux9

List Files Using a Specific File Descriptor

Replace FD with the actual file descriptor to list using it −

lsof -d FD
lsof Command in Linux10

List Files with a Specific Type

This command combines multiple to list files opened by a specific user and using a specific file descriptor −

lsof -u username -a -d FD
lsof Command in Linux11

List Files Opened by a Specific Process Name

Replace process_name with the actual process name to list files opened by it −

lsof -c process_name
lsof Command in Linux12

List Files Opened by a Specific PID and User

This command lists files opened by a specific process ID and user −

lsof -p PID -u username
lsof Command in Linux13

List Files Opened by a Specific Network Connection Type

Replace port with the actual port number to list files opened by TCP connections on that port −

lsof -i tcp:port
lsof Command in Linux14

List Files Opened by a Specific Command and User

This command lists files opened by a specific command and user −

lsof -c command -u username
lsof Command in Linux15

Advanced Features of lsof Command

In addition to the basic options and examples, lsof provides advanced features that can be useful in specific scenarios. Let's explore some of these features with practical examples.

Combining Multiple Criteria

This command combines multiple to list files opened by a specific user and a specific process ID −

lsof -u username -a -p PID
lsof Command in Linux16

Disabling Hostname Resolution

This command disables hostname resolution, which can speed up the output when listing network connections −

lsof -n
lsof Command in Linux17

Repeating the Listing

This command repeats the listing every 5 seconds, providing a real-time view of open files −

lsof -r 5
lsof Command in Linux18

Outputting Only Process IDs

This command outputs only the process IDs of the processes that have open files −

lsof -t
lsof Command in Linux19

Listing Files Opened by a Specific User and Command

This command lists files opened by a specific user and a specific command −

lsof -u username -c command
lsof Command in Linux20

Listing Files Opened by a Specific User and Network Connection

This command files opened by a specific user and TCP connections −

lsof -u username -i tcp
lsof Command in Linux21

Listing Files Opened by a Specific Process and Network Connection

This command lists files opened by a specific process ID and UDP connections −

lsof -p PID -i udp
lsof Command in Linux22

Listing Files Opened by a Specific Command and Network Connection

This command lists files opened by a specific command and TCP connections −

lsof -c command -i tcp
lsof Command in Linux23

Listing Files Opened by a Specific User, Command, and Network Connection

This command lists files opened by a Specific User, Command, and Network Connection −

lsof -u username -c command -i udp
lsof Command in Linux24

Conclusion

The lsof command is an essential tool for anyone working with Linux systems. It provides a wealth of information about open files and the processes accessing them, making it invaluable for troubleshooting and system management.

By mastering the various options and examples provided in this tutorial, you'll be well-equipped to leverage the full power of the lsof command in your daily tasks.

Advertisements