C.W.K.
Stream
Lesson 09 of 10 · published

lsof: Who's Using What

~10 min · lsof, files, ports

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

List Open Files — and sockets, devices, everything

lsof shows files open by which processes. On Unix everything is a file (network sockets, ttys, pipes), so lsof answers questions like "what's holding port 8080?" and "who has this disk open so I can't unmount?"

Daily recipes

  • lsof -i :8080 — what listens on / is connected to port 8080.
  • lsof -i TCP:LISTEN — every TCP listener.
  • lsof -nP -iTCP -sTCP:LISTEN — listeners with numeric IPs and ports (faster, no DNS).
  • lsof /Volumes/Backup — what's keeping that volume busy.
  • lsof -p PID — every fd held by a process.
  • lsof -u $USER — files held by your user.
  • lsof | grep deleted — files unlinked but still open (common cause of "disk full but I deleted everything").

The classic incidents lsof solves

  1. Port already in use. lsof -i :3000, see the PID, kill the stale dev server.
  2. Can't unmount. lsof /Volumes/Disk, see who's open, close them.
  3. df shows full but du doesn't. lsof | grep deleted finds processes holding deleted files; restart the process to release.

The faster modern alternative

ss on Linux replaces a lot of lsof -i. On macOS, lsof is still the answer. Either way, alias the verbose form: alias ports='lsof -nP -iTCP -sTCP:LISTEN' and ports becomes a one-word answer to "what's listening?"

Code

Find what's on a port·bash
lsof -i :3000
lsof -nP -iTCP -sTCP:LISTEN | head
alias ports='lsof -nP -iTCP -sTCP:LISTEN' >> ~/.zshrc

External links

Exercise

Run lsof -nP -iTCP -sTCP:LISTEN. Identify each listener — Pippa, Spotify, Vite, etc. Then start a quick listener: nc -l 9999 &. Run lsof -i :9999 and confirm. kill %1 to stop.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.