Exciting news! TCMS official website is live! Offering full-stack software services including enterprise-level custom R&D, App and mini-program development, multi-system integration, AI, blockchain, and embedded development, empowering digital-intelligent transformation across industries. Visit dev.tekin.cn to discuss cooperation!
Learn how to use the lsof command (List Open Files) on Unix-like systems. Master PID retrieval, process management, port occupancy checks, and disk space troubleshooting with detailed parameters and practical examples.

lsof (List Open Files) is a powerful command-line tool that lists all "open files" in the system, including regular files, directories, and network sockets. This article expands on the original content by adding PID (Process ID) retrieval, process start/stop operations, and complete parameter explanations, helping you fully leverage lsof to solve real-world problems efficiently. It’s ideal for system administrators and developers looking to enhance system debugging capabilities.
lsof [options] [filename/arguments]Regular users can only view their own process information.
Root users have access to all system processes.
Output fields include COMMAND (process name), PID (Process ID), USER (user), FD (file descriptor), TYPE (file type), and NAME (path/network information).
In daily work, it’s common to retrieve PIDs via process names or execution paths, then perform start, stop, or restart operations. Combine lsof with other commands for efficient task completion.
Scenario: Obtain the PID when the process name is known (e.g., nginx, java).
Method: Use the -c option to match the process name and -t to output only the PID.
# Get PIDs of all processes containing "nginx" in the name
lsof -t -cnginx
# Example output (multiple PIDs displayed line by line)
1234
1235
Scenario: Locate the PID when the executable file path is known (e.g., /usr/bin/nginx).
Method: Specify the path directly as an argument and use -t to output the PID.
# Get PID via executable file path
lsof -t /usr/bin/nginx
# Example output
1234
Scenario: Terminate abnormal processes after retrieving their PIDs.
Method: Combine with the kill command for batch operations.
# Kill all processes containing "nginx" in the name
kill -9 $(lsof -t -c nginx)
# Kill processes by specified path (e.g., /usr/bin/java)
kill -9 $(lsof -t /usr/bin/java)
Scenario: Restart a process after modifying configurations (using nginx as an example).
Method: Stop the process first, then start it (startup methods vary by service).
# Stop nginx
kill -9 $(lsof -t -c nginx)
# Start nginx (adjust path as needed)
/usr/bin/nginx
Below is a concise overview of each option’s function and examples, organized by parameter format:
| Parameter | Function | Example |
|---|---|---|
-? | Display brief help | lsof -? |
-a | Logical AND for multiple options (default is OR) | lsof -u root -a -c nginx (root user AND process name contains "nginx") |
-b | Avoid blocking operations | lsof -b +D /mnt/network (no blocking when viewing network directories) |
-C | Hide process name version suffixes | lsof -C -c nginx-1.21 (displays as "nginx") |
-h | Compress duplicate output | lsof -h -i :80 (omits duplicate process names/PIDs) |
-l | Show UID instead of username | lsof -l -u 1000 (USER column shows 1000) |
-n | Do not resolve IPs to domain names | lsof -n -i (displays IPs instead of domain names) |
-N | Show NFS-related files | lsof -N /mnt/nfs (view NFS-mounted files) |
-O | Hide file offsets | lsof -O -p 1234 (hides SIZE/OFF column) |
-P | Do not resolve ports to service names | lsof -P -i (displays 80 instead of "http") |
-R | Show parent process ID (PPID) | lsof -R -c nginx (adds a PPID column) |
-t | Output only PIDs | lsof -t -i :80 (returns only PIDs for port 80) |
-U | Show UNIX domain sockets | lsof -U (lists all UNIX sockets) |
-v | Display version information | lsof -v |
-V | Show detailed version and configuration | lsof -V |
-X | Exclude network files | lsof -X -p 1234 (shows only local files) |
-A A | Restrict file descriptor types | lsof -A p -c nginx (shows only pipe files) |
-c c | Filter by process name prefix | lsof -c nginx (process names starting with "nginx") |
+c c | Limit process name display length | lsof +c 5 -c nginx (process name max 5 characters) |
+|-d d | Filter by file descriptors (+include, -exclude) | lsof +d 0-2 (include FDs 0-2); lsof -d 3 (exclude FD 3) |
+|-D D | Filter by directories (+recursive, -non-recursive) | lsof +D /var/log (recursive directory); lsof -D /var (non-recursive) |
+|-e s | Exclude file systems (+include, -exclude) | lsof -e /proc (exclude /proc file system) |
+|-E | Show/hide process environment variables | lsof +E -p 1234 (show environment variables for process 1234) |
+|-f [cfgGn] | Control file system info display | lsof +f g -i (show GID for network files) |
-F [f] | Custom output format (f = fields, e.g., p=PID) | lsof -F pn -i :80 (outputs PID and filename) |
-g [s] | Filter by process group ID (PGID) | lsof -g 123 (processes with PGID 123) |
-i [i] | Filter network connections | lsof -i tcp:80 (TCP port 80) |
-k k | Filter by kernel threads | lsof -k 1 (show only kernel threads) |
-K k | Filter by thread ID (TID) | lsof -K 456 (thread with TID 456) |
+|-L [l] | Filter by link count (+less than l, -greater than l) | lsof +L1 (files with link count <1, i.e., deleted files) |
+|-m m | Filter by memory mappings | lsof +m /dev/shm (include memory mappings for /dev/shm) |
+|-M | Show/hide NFS mount point info | lsof +M -N (show detailed NFS mount points) |
-o [o] | Show file offsets (o = precision) | lsof -o 3 -p 1234 (offsets with 3 decimal places) |
-p s | Filter by PID | lsof -p 1234,5678 (processes with PIDs 1234 and 5678) |
+|-r [t] | Periodic refresh (+continuous, -one-time; t = seconds) | lsof -r 2 -i :80 (refresh port 80 every 2 seconds) |
-s [p:s] | Filter by network status | lsof -i tcp -sTCP:LISTEN (TCP listening status) |
-S [t] | Set network timeout (t = seconds) | lsof -i @remote -S 5 (5-second timeout) |
-T [t] | Show TCP info (t = type, e.g., q=sequence number) | lsof -i tcp -T q (show TCP sequence numbers) |
-u s | Filter by user | lsof -u root,www-data (root and www-data users) |
+|-w | Show/hide warning messages | lsof -w -i (hide warnings) |
-x [fl] | Show file lock info (f=file, l=lock) | lsof -x f -p 1234 (file locks for process 1234) |
+|-X | Include/exclude X11-related files | lsof -X (exclude X11 files) |
-z [z] | Filter by UID/GID range | lsof -z 1000-2000 (UIDs 1000-2000) |
-Z [Z] | Show SELinux context | lsof -Z -i :80 (SELinux context for port 80) |
-- | Separate options from filenames | lsof -- -i (view file named "-i") |
[names] | Filename/path | lsof /var/log/syslog (view processes opening this file) |
# Find processes occupying port 80
lsof -i:80
# Kill the occupying process
kill -9 $(lsof -t -i :80)
# Locate deleted but still occupied files
lsof +L1
# Restart occupying processes to free up space
systemctl restart $(lsof -t +L1 | xargs ps -p | awk 'NR>1 {print $4}')
# Find processes occupying /var/www
lsof +D /var/www
# Terminate processes and delete the directory
kill -9 $(lsof -t +D /var/www) && rm -rf /var/www
# Real-time monitor TCP connections for "testuser" (refresh every 3 seconds)
lsof -nP -itcp -utestuser -r 3
The core value of the lsof command lies in linking processes, files, and networks. With the added PID retrieval and process management techniques in this article, you can further improve system debugging efficiency. Whether locating PIDs via process names/paths or troubleshooting ports and disk issues with parameter combinations (e.g., -c + -t, +L1, -i), flexible use of lsof will help you resolve problems quickly. For complete parameters, refer to man lsof and gradually master this "Swiss Army knife" of system troubleshooting.