lsof Command
The lsof command (list open files) displays information about files opened by processes. In Unix-like systems, everything is treated as a file, so lsof shows regular files, directories, network sockets, pipes, and other file descriptors.
Syntax
Description
The lsof command is a powerful diagnostic tool that lists information about files opened by processes. It can show regular files, directories, network connections, pipes, and other file descriptors.
Key features:
- List all open files on the system
- Show files opened by specific processes
- Display network connections and listening ports
- Find processes using specific files or directories
- Monitor file system usage
- Troubleshoot "device busy" errors
Common Options
| Option | Description |
|---|---|
-a |
AND the selections (default is OR) |
-c command |
List files opened by processes with specified command name |
-d fd |
List files with specified file descriptor |
-i [protocol][@host][:port] |
List network connections (Internet files) |
-n |
Don't resolve hostnames (show IP addresses) |
-P |
Don't resolve port names (show port numbers) |
-p PID |
List files opened by specific process ID |
-t |
Terse output (PIDs only) |
-u user |
List files opened by specific user |
-r [time] |
Repeat mode (refresh every time seconds) |
+D directory |
Search directory tree for open files |
+d directory |
Search directory (not subdirectories) for open files |
Examples
List all open files
Display all open files on the system (requires root for complete output)
List files opened by a specific process
Show all files opened by process with PID 1234
List files opened by a command
Show files opened by all Firefox processes
List network connections
Display all network connections and listening ports
List specific port connections
Show connections on specific ports
List files opened by a user
Show all files opened by a specific user
Find processes using a file
Show which processes have the specified file open
Find processes using a directory
Show processes using files in the directory tree
Understanding lsof Output
Output Columns
| Column | Description |
|---|---|
| COMMAND | Process command name (first 9 characters) |
| PID | Process ID |
| USER | User who owns the process |
| FD | File descriptor (cwd, txt, mem, or number) |
| TYPE | File type (REG, DIR, CHR, BLK, FIFO, IPv4, IPv6) |
| DEVICE | Device numbers or protocol |
| SIZE/OFF | File size or offset |
| NODE | Inode number or protocol-specific identifier |
| NAME | File name or network address |
File Descriptor Types
| FD Value | Description |
|---|---|
| cwd | Current working directory |
| txt | Program text (executable) |
| mem | Memory-mapped file |
| rtd | Root directory |
| 0 | Standard input |
| 1 | Standard output |
| 2 | Standard error |
| 3+ | Other file descriptors |
Example Output
Example lsof output showing different file types and descriptors
Network Monitoring
Basic Network Commands
All network connections
Show all network connections (TCP and UDP)
TCP connections only
Show only TCP connections
UDP connections only
Show only UDP connections
Listening ports
Show only listening TCP ports
Established connections
Show only established TCP connections
Specific Port Monitoring
Web server ports
Monitor web server connections
SSH connections
Show SSH connections
Database connections
Monitor database connections
Network Troubleshooting
Find process using a port
Identify processes using specific ports
Monitor connections without DNS resolution
Show connections with IP addresses and port numbers (faster)
Process Monitoring
Process File Usage
Files opened by specific process
Monitor file usage by specific processes
User file usage
Monitor file usage by users
File System Monitoring
Files in directory
Find processes using files in directories
Specific file usage
Find processes using specific files
Troubleshooting "Device Busy" Errors
Find processes using a mount point
Identify processes preventing unmount
Find processes using deleted files
Find processes holding references to deleted files
Advanced Usage
Combining Options
AND logic with -a
Combine multiple criteria with AND logic
Exclude criteria
Exclude specific criteria from results
Continuous Monitoring
Repeat mode
Continuously monitor file usage and connections
Terse output for scripting
Use terse output for automation and scripting
Practical Use Cases
System Administration
Security monitoring
Monitor system security and detect anomalies
Performance troubleshooting
Diagnose performance issues related to file usage
Development and Debugging
Application debugging
Debug application file access patterns
Database troubleshooting
Troubleshoot database connectivity and file locking
Network Administration
Port management
Manage port allocation and service monitoring
Connection analysis
Analyze network connection patterns and states
Scripting Examples
System Monitoring Scripts
Port usage report
Generate comprehensive port usage reports
File descriptor monitoring
Monitor processes with high file descriptor usage
Cleanup Scripts
Kill processes using specific files
Safely kill processes using specific files
Network connection cleanup
Clean up processes using specific network ports
Troubleshooting
Common Issues
Permission denied
Handle permission limitations
Performance with large systems
Optimize lsof performance on busy systems
Alternative Commands
When lsof is not available
Alternative methods when lsof is unavailable
Cross-verification
Cross-verify lsof output with other tools
Best Practices
lsof Best Practices
- Use Specific Criteria - Narrow down searches to avoid overwhelming output
- Combine with Other Tools - Use with ps, netstat, and ss for comprehensive analysis
- Script Automation - Use terse output (-t) for automated processing
- Performance Optimization - Use -n and -P flags to avoid DNS/port lookups
- Security Monitoring - Regularly check for unusual network connections
- Resource Management - Monitor file descriptor usage to prevent exhaustion