Linux lsof Command
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
- COMMAND: Process name
- PID: Process ID
- USER: User running the process
- FD: File descriptor
cwd: Current working directorytxt: Program code filesmem: Memory-mapped files0u,1u,2u: Standard input, output, and error
- TYPE: File type (REG regular file, DIR directory, CHR character device, etc.)
- DEVICE: Device number
- SIZE/OFF: File size or offset
- NODE: File inode number
- NAME: Filename or mount point
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:
- Run directly
lsofIt scans the entire system and may consume considerable resources - Try to use precise filter conditions (such as
-p,-u,-ietc.) to narrow the query scope - Use in Scripts
-tThe option outputs only PIDs, reducing parsing overhead
Alternative tools
Although lsof is the most commonly used tool, in some scenarios you could consider:
- fuser: Quickly see which processes are using a particular file or socket
- ss/netstat: Dedicated to network connection information
- ps: Combine with
/procThe filesystem can also provide some information
Summary
lsof is the Swiss Army knife for Linux system administrators and developers; mastering it helps you:
- Quickly pinpoint resource occupancy issues
- Troubleshoot file- and network-related issues
- Understand the system's running state
- 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