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!

Complete Guide to the lsof Command: Master System Troubleshooting from Beginner to Pro

2025-11-16 8 mins read

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-command-parameter-and-process-management-guide
 

On Unix-like systems, 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.

1. Basic Syntax and Core Concepts of lsof

1.1 Basic Syntax

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).

2. Retrieving Process PIDs and Managing Processes with lsof

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.

2.1 Retrieve PID by Process Name

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

2.2 Retrieve PID by Execution Path

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

2.3 Stop/Kill a Process

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)

2.4 Restart a Process

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

3. Complete Parameter Explanations (SYNOPSIS)

Below is a concise overview of each option’s function and examples, organized by parameter format:

ParameterFunctionExample
-?Display brief helplsof -?
-aLogical AND for multiple options (default is OR)lsof -u root -a -c nginx (root user AND process name contains "nginx")
-bAvoid blocking operationslsof -b +D /mnt/network (no blocking when viewing network directories)
-CHide process name version suffixeslsof -C -c nginx-1.21 (displays as "nginx")
-hCompress duplicate outputlsof -h -i :80 (omits duplicate process names/PIDs)
-lShow UID instead of usernamelsof -l -u 1000 (USER column shows 1000)
-nDo not resolve IPs to domain nameslsof -n -i (displays IPs instead of domain names)
-NShow NFS-related fileslsof -N /mnt/nfs (view NFS-mounted files)
-OHide file offsetslsof -O -p 1234 (hides SIZE/OFF column)
-PDo not resolve ports to service nameslsof -P -i (displays 80 instead of "http")
-RShow parent process ID (PPID)lsof -R -c nginx (adds a PPID column)
-tOutput only PIDslsof -t -i :80 (returns only PIDs for port 80)
-UShow UNIX domain socketslsof -U (lists all UNIX sockets)
-vDisplay version informationlsof -v
-VShow detailed version and configurationlsof -V
-XExclude network fileslsof -X -p 1234 (shows only local files)
-A ARestrict file descriptor typeslsof -A p -c nginx (shows only pipe files)
-c cFilter by process name prefixlsof -c nginx (process names starting with "nginx")
+c cLimit process name display lengthlsof +c 5 -c nginx (process name max 5 characters)
+|-d dFilter by file descriptors (+include, -exclude)lsof +d 0-2 (include FDs 0-2); lsof -d 3 (exclude FD 3)
+|-D DFilter by directories (+recursive, -non-recursive)lsof +D /var/log (recursive directory); lsof -D /var (non-recursive)
+|-e sExclude file systems (+include, -exclude)lsof -e /proc (exclude /proc file system)
+|-EShow/hide process environment variableslsof +E -p 1234 (show environment variables for process 1234)
+|-f [cfgGn]Control file system info displaylsof +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 connectionslsof -i tcp:80 (TCP port 80)
-k kFilter by kernel threadslsof -k 1 (show only kernel threads)
-K kFilter 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 mFilter by memory mappingslsof +m /dev/shm (include memory mappings for /dev/shm)
+|-MShow/hide NFS mount point infolsof +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 sFilter by PIDlsof -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 statuslsof -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 sFilter by userlsof -u root,www-data (root and www-data users)
+|-wShow/hide warning messageslsof -w -i (hide warnings)
-x [fl]Show file lock info (f=file, l=lock)lsof -x f -p 1234 (file locks for process 1234)
+|-XInclude/exclude X11-related fileslsof -X (exclude X11 files)
-z [z]Filter by UID/GID rangelsof -z 1000-2000 (UIDs 1000-2000)
-Z [Z]Show SELinux contextlsof -Z -i :80 (SELinux context for port 80)
--Separate options from filenameslsof -- -i (view file named "-i")
[names]Filename/pathlsof /var/log/syslog (view processes opening this file)

4. Practical High-Frequency Scenarios

4.1 Troubleshoot Port Occupancy

# Find processes occupying port 80
lsof -i:80
# Kill the occupying process
kill -9 $(lsof -t -i :80)

4.2 Unreleased Disk Space

# 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}')

4.3 Unable to Delete a Directory Due to Occupancy

# 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

4.4 Monitor Network Connections for a Specific User

# Real-time monitor TCP connections for "testuser" (refresh every 3 seconds)
lsof -nP -itcp -utestuser -r 3

5. Conclusion

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.

Tags: lsof command, process management, PID retrieval, port occupancy, Linux commands, system troubleshooting, process start/stop

Image NewsLetter
Icon primary
Newsletter

Subscribe our newsletter

Please enter your email address below and click the subscribe button. By doing so, you agree to our Terms and Conditions.

Your experience on this site will be improved by allowing cookies Cookie Policy