Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux lsof Command

Linux lsof Command

Linux Command Reference Linux Command Reference


is a powerful command-line tool used to list all files currently open in the system and related process information. In Linux, the idea that "everything is a file" means lsof can display not only regular files but also network connections, device files, pipes, and sockets.


lsof Basic Syntax

lsof [选项] [文件名或目录]

Description of common options and parameters

Option Description
-a Combine multiple conditions with AND logic
-c <进程名> Display files opened by a specified process
-d <文件描述符> Display files for a specified file descriptor
-i Display network connection information
-n Do not resolve host names (show IP addresses)
-P Do not resolve port names (show port numbers)
-p Display files opened by a specified process ID
-u <用户名> Display files opened by a specified user
+D <目录> Recursively display open files in a directory
-t Output only process IDs (suitable for scripts)

Common operation examples

1. View All Open Files

lsof

This lists all open files in the system, including regular files, directories, library files, devices, and network connections.

2. View Files Opened by a Specified Process

Example

lsof

-p

1234

 

View files opened by the process with PID 1234

lsof

-c

nginx

View files opened by all nginx processes

3. View Network Connections

Example

lsof

-i

     

View all network connections

lsof

-i

:

80

 

View all connections using port 80

lsof

-i

TCP  

View all TCP connections

4. View Files Opened by a User

lsof -u root  # 查看 root 用户打开的文件

5. Find Files That Have Been Deleted but Are Still in Use

lsof | grep deleted

This often happens when a log file is deleted but the service is still running, so disk space is not released.

6. View Open Files in a Specified Directory

lsof +D /var/log  # 查看 /var/log 目录下被打开的文件


Output Field Explanation

Typical columns in lsof output are as follows:

COMMAND    PID   USER   FD      TYPE     DEVICE  SIZE/OFF    NODE NAME

- `cwd`: Current working directory - `txt`: Program code files - `mem`: Memory-mapped files - `0u`, `1u`, `2u`: Standard input, output, and error

Advanced application scenarios

1. Find the Process Occupying a Port

lsof -i :8080

When you want to start a service but the port is already occupied, this command can quickly find the process occupying the port.

2. Recover Deleted Files

When a file has been deleted but is still in use by a process, it can be recovered through the /proc filesystem:

Example

lsof

|

grep

deleted  

Find deleted files and processes

ls

-l

/

proc

//

fd  

View a process's file descriptors

cp

/

proc

//

fd

/

/

path

/

to

/

recovery  

Recover the file

3. Monitor File Access

watch -n 1 'lsof /path/to/file'

This lets you monitor in real time which processes are accessing a particular file.


Common Problem Solutions

1. Running Out of Disk Space but Finding No Large Files

lsof | grep deleted

Find files that have been deleted but are still held by processes, then restart the related processes to free up space.

2. Unable to Unmount a Device

lsof /mount/point

Find which processes are using the mount point, stop those processes, and then unmount it.

3. Troubleshooting Network Connection Issues

lsof -i -n -P

View all network connections to help troubleshoot port conflicts or connection problems.


Performance considerations

Although lsof is powerful, keep the following in mind when using it in a busy production environment:

  1. Run directly lsof It scans the entire system and may consume considerable resources
  2. Try to use precise filter conditions (such as -p, -u, -i etc.) to narrow the query scope
  3. Use in Scripts -t The option outputs only PIDs, reducing parsing overhead

Alternative tools

Although lsof is the most commonly used tool, in some scenarios you could consider:

  1. fuser: Quickly see which processes are using a particular file or socket
  2. ss/netstat: Dedicated to network connection information
  3. ps: Combine with /proc The filesystem can also provide some information

Summary

lsof is the Swiss Army knife for Linux system administrators and developers; mastering it helps you:

  1. Quickly pinpoint resource occupancy issues
  2. Troubleshoot file- and network-related issues
  3. Understand the system's running state
  4. Resolve all kinds of "file in use" problems

It is recommended to practice more in daily work, flexibly applying the various options and parameters to specific scenarios, and gradually mastering the full potential of this powerful tool.


Linux Command Reference Linux Command Reference