🐧Go deeper — read the bookLinux for C++ Interviews: tracing a live process— runnable code & full walkthrough →

linux · medium

Who Owns This Port / FD? — lsof & /proc/PID/fd

lsof maps descriptors to resources: find a port's owner (lsof -i / fuser), detect a leak by counting /proc/PID/fd against ulimit -n (ends in EMFILE), and solve 'df full but du empty' — a deleted-but-still-open file held by a process (lsof +L1).

🔑 Key line

lsof -i :PORT / fuser = who owns a port. /proc/PID/fd count vs ulimit -n = leak detection (ends in EMFILE). lsof +L1 = deleted-but-open files (df full, du empty).

The code

sudo lsof -i :8080 # which process owns a port
sudo fuser 8080/tcp # just the PID (-k to kill)
ls /proc/1234/fd | wc -l # fd count vs ulimit -n (leak check)
# df full but du empty? a deleted file is still open:
sudo lsof +L1 # files with link count 0

What this lesson walks through

  1. 01Which process owns a port?
  2. 02Detect a descriptor leak
  3. 03'Disk full' but du finds nothing

lsof -i :PORT or fuser PORT/tcp names the process and PID holding a port — fuser -k even kills it.

See it animated — step by step, at your own pace

Unlock the full interactive walkthrough of Who Owns This Port / FD? — lsof & /proc/PID/fd and 100+ animated C++ interview lessons.

← Previous
Is the Data on the Wire? — tcpdump Capture & Filters
Next →
Processes & Signals — ps, kill, jobs, systemctl