lsof
Browse / lsof Command Cheatsheet
lsof Command Cheatsheet
A handy reference for the lsof command, covering basic usage, common filters, network inspection, process management, and output interpretation.
Basic Usage & Filters
Basic Listing
|
List all open files for all active processes. This can produce a very large output. |
|
Pipe the output to |
|
List files opened by a specific user. Example:
|
|
List all network connections (sockets). Includes TCP, UDP, and raw sockets. |
|
List files opened by processes running a specific command. Example:
|
|
List files opened by a specific process ID. Example:
|
|
List files opened with a specific file descriptor. Example:
(File descriptor 1 usually refers to standard output) |
|
List files EXCEPT those with a specific file descriptor. Example:
(Exclude standard output) |
Network Filters
|
List all open TCP network connections. |
|
List all open UDP network connections. |
|
List processes listening on or connected to a specific port. Example:
(List processes using port 80) |
|
List TCP connections on a specific port. Example:
|
|
List network activity related to a specific host address. Example:
|
|
List network activity related to a specific host and port. Example:
|
|
List network activity within a port range. Example:
|
|
Disable port number to name mapping (shows numeric ports). |
Process & File System Filters
|
Combine options with Example:
|
|
List files opened by everyone EXCEPT a specific user. Example:
|
|
List files opened by all processes EXCEPT a specific PID. Example:
(Exclude |
|
List processes that have a specific file open. Example:
|
|
List all open files within a directory (and its subdirectories). Example:
|
|
List only files opened directly in the specified directory (no recursion). Example:
|
|
Output filenames only. Useful for scripting. Example:
|
|
Display |
|
Include user ID numbers in the output. |
Advanced Usage & Output
Output Fields Explained
|
The command name (often truncated). |
|
Process ID. |
|
Task ID (Thread ID) if the |
|
User ID or name of the process owner. |
|
File Descriptor number and type. Examples: |
|
Type of node associated with the file. Examples: |
|
Device numbers for the file. |
|
Size of the file or the file offset. |
|
Node number of the local file system file or the inode number. |
|
Name of the file, network address, etc. |
Common Use Cases & Tips
|
Find processes accessing a specific device. |
|
List files that are currently linked to but have been deleted (shows |
|
Find processes listening on a specific TCP port. Example:
|
|
Show TCP options. Use Example:
(Show TCP window sizes) |
|
Output only PIDs using a specific port. Useful for scripting (e.g., killing processes). Example:
|
|
Repeat Example:
(Monitor port 80 every 5 seconds) |
|
Avoid kernel blocking. |
|
Disable host name resolution ( Good practice for general use unless names are required. |
Troubleshooting Examples
Find which process is using a port:
Find the |
Find why a disk is full (deleted files):
Look for large files marked |
Identify network connections for a specific process:
Replace |
See all files opened by user ‘daemon’:
|
Check if a specific file is open and by whom:
|
Determine which process has a lock on a file: Look for output lines related to the file and check the |