lsof Cheatsheet - Linux Process & Port Diagnostics Reference

Occupied ports, locked files, a full disk with no large file in sight — all three share one question: which process holds this resource. The sheet is organised along four axes: by process to expand a known PID into its open files and sockets, by port to go the other direction, by file to find its holders, and by type for pipes and memory maps. The least appreciated entry is +L1 under troubleshooting: a deleted file that is still open keeps consuming disk, df will never show it, and only lsof +L1 finds the process holding the dead inode. The division of labour is worth stating — ss and netstat are faster but network-only, fuser can kill directly with -k but gives no connection detail, and /proc is ground truth only if you assemble it yourself. Reach for it when an application reports Address already in use, a log file cannot be written, or umount reports device busy. Budget for permissions: without root you see only your own descriptors, and half a process table is worse than none because it invites false conclusions.

SysOps·35 commands·Last updated 2026-07-21

By Process 6

lsof -p 1234
Files opened by a given PID
lsof -c nginx
Files opened by the nginx process
lsof -c /^nginx/
Match process name by regex
lsof -u tom
Files opened by a given user
lsof -u ^root
Exclude the root user
lsof -p 1234,5678
Files opened by multiple PIDs

By Port 6

lsof -i :80
Process using port 80
lsof -i :8080 -i :8443
Check multiple ports
lsof -i tcp:80
TCP port 80 only
lsof -i udp:53
UDP port 53 only
lsof -i @192.168.1.1:80
Specific IP and port
lsof -i :1-1024
All privileged ports (1-1024)

By File 4

lsof /var/log/syslog
Who opened a given file
lsof +D /var/log/
All open files under a directory (recursive)
lsof +d /var/log/
Current directory only (non-recursive)
lsof /dev/sda1
Who is using a given device

Network 8

lsof -i
All network connections
lsof -i -n
No name resolution (faster)
lsof -i -P
No port-name resolution (numeric ports)
lsof -i -n -P
Combined (fastest)
lsof -i tcp@192.168.1.1:22
SSH connections to a host
lsof -i -sTCP:ESTABLISHED
Established connections only
lsof -i 4 -P -n
IPv4 connections only (no resolution)
lsof -i 6 -P -n
IPv6 connections only

By Type / Format 4

lsof -t
Output PIDs only (good for pipes)
lsof -F pfc
Output formatted fields (PID/command/user)
lsof -a -p 1234 -c nginx
AND-combine conditions
lsof -Or -p 1234 -p 5678
OR-combine conditions

Troubleshooting 7

lsof -i :80 | grep LISTEN
Find the process listening on port 80
lsof +L1
Deleted-but-open files (when disk is full)
lsof -nP -iTCP -sTCP:LISTEN
All listening TCP ports
lsof -i :22 | grep ESTABLISHED
Established SSH connections
lsof -u tom | wc -l
Count files opened by a user
kill $(lsof -t -i :8080)
Kill the process occupying port 8080
lsof -i :80 -r 5
Monitor port 80 every 5 seconds

Command Examples

Disk full but du finds nothing

lsof +L1

+L1 列出已删除但仍被进程打开的文件。文件被 rm 了但空间没释放,就是因为还有进程持有它。重启对应进程或重启机器才回收。

Output

COMMAND   PID USER   FD   TYPE DEVICE    SIZE/OFF     NODE NAME
nginx     1420  www   12w  REG  254,1  4823445760    1234 /var/log/nginx/access.log (deleted)

See which files under a directory are open

lsof +D /var/log/

+D 递归整个目录,较慢但完整;只想看当前层用 +d。删除或移动文件报 "device or resource busy" 时,这条能直接定位占用进程。

List all listening TCP sockets, fastest form

lsof -nP -iTCP -sTCP:LISTEN

-n 不解析域名、-P 显示数字端口,配合状态过滤是 lsof 最快的形式。ls 目录量大时输出会很宽,加 | awk 精简列。

Output

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx     1420  www    6u  IPv4 265781      0t0  TCP *:80 (LISTEN)
node      3120  tom   18u  IPv4 265781      0t0  TCP *:3000 (LISTEN)

Common Pitfalls

  • lsof output is enormous; always scope it with -i, -p, -u or +D, or pipe it through a filter.
  • Seeing other users' processes requires root; a normal user only sees their own.
  • When deletion fails with "device or resource busy", do not force it — find the holder with lsof first; a forced delete still will not free the space.
  • lsof +D recurses the whole directory and is very slow on large trees; prefer lsof | grep there.
  • For network connection work prefer ss, which reads kernel netlink and is orders of magnitude faster.

Tips

  • lsof needs root to see all users' files; ordinary users see only their own.
  • For port conflicts, first find the PID with lsof -i :port, then kill it or change config.
  • When the disk is full but no large file shows, use lsof +L1 to find deleted-but-open files.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us