SkillPilotAI

lsof and fuser

lsof (list open files) shows which process holds which file and port, because on Unix a network socket is also a file. fuser works the other way: give it a file, mount or port and it returns the PIDs using it, and can kill them.

71.1 Why is it used?

  • Find the process behind 'port 8080 already in use' together with its user and PID.
  • Find deleted-but-still-open files that keep disk space busy (lsof +L1).
  • See every open file of one process: cwd, program, libraries, log files, sockets.
  • Free a port or unmount a busy filesystem by finding (and stopping) the processes with fuser.
  • Linux: lsof comes as its own package, fuser is in psmisc. macOS has lsof built in, but its fuser is a different BSD tool (not covered). Windows has neither; use netstat -ano.

71.2 How it works

  • Every process has an FD (file descriptor) table; a TCP socket is just another entry in it.
  • lsof reads these tables (from /proc on Linux) and prints one line per open file.
  • fuser starts from a name (file, mount, port) and finds the processes whose FD tables point to it.
  • Without root you only see your own processes, so use sudo to see other users' files and sockets.
Every process has an FD table: a socket is a file
  1. 1.srv 192.168.91.10: socat (user alice, PID 37) listens on 8080, FD 5 = TCP socket in LISTEN

  2. 2.cli 192.168.91.20 sends a SYN through the Docker bridge n071-lan

  3. 3.The SYN reaches srv and the connection is made

  4. 4.socat forks child PID 83, which opens a new FD 6 (ESTABLISHED)

  5. 5.lsof reads exactly this FD table: PID 37 FD 5u LISTEN, PID 83 FD 6u ESTABLISHED

lsof: process -> files. fuser: file/port -> PIDs.

71.3 lsof: combining selections

  • Without options lsof lists every open file (196 lines in the lab).
  • Selections like -i and -u alice are ORed by default, so you get every file of alice too.
  • -a turns them into AND: only the network files of alice.
  • Give a file name to see who holds it; after -- even a name starting with - works.
Terminal
lsof | wc -llsof -i -u alicelsof -a -i -u alicelsof /var/log/app.logcd /tmp; lsof -- -strange.txt
Output
$ lsof | wc -l196$ lsof -a -i -u aliceCOMMAND PID  USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEsocat    37 alice 5u  IPv4 1604960      0t0  TCP *:http-alt (LISTEN)socat    83 alice 6u  IPv4 1605106      0t0  TCP srv:http-alt->n071-cli.n071-lan:41384 (ESTABLISHED)$ lsof /var/log/app.logCOMMAND PID USER FD   TYPE DEVICE SIZE/OFF   NODE NAMEpython3  31 root 2w   REG  0,127       64 164521 /var/log/app.log$ cd /tmp; lsof -- -strange.txtCOMMAND PID USER FD   TYPE DEVICE SIZE/OFF   NODE NAMEsleep    67 root 5r   REG  0,127        0 164526 -strange.txt

71.4 lsof -i: network files

  • -i shows all Internet sockets, but turns IPs and ports into names (srv:http).
  • -nP keeps everything numeric and is much faster (no DNS or service lookups).
  • Address form: [4|6][protocol][@host][:port], e.g. -i6, -iTCP:8080, -i @192.168.91.20.
  • Up to 100 -i options can be given; they are ORed. -U shows UNIX domain sockets.
Terminal
lsof -nP -ilsof -nP -i6lsof -nP -iTCP:8080lsof -nP -i @192.168.91.20lsof -U | head -5
Output
$ lsof -nP -iCOMMAND PID  USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEpython3  31  root 3u  IPv4 1607027      0t0  TCP *:80 (LISTEN)python3  31  root 4u  IPv4 1607785      0t0  TCP 192.168.91.10:80->192.168.91.20:46882 (ESTABLISHED)socat    37 alice 5u  IPv4 1604960      0t0  TCP *:8080 (LISTEN)python3  49  root 3u  IPv4 1605998      0t0  TCP *:9090 (LISTEN)python3  49  root 4u  IPv4 1605999      0t0  TCP 192.168.91.10:9090->192.168.91.20:37600 (CLOSE_WAIT)socat    55  root 5u  IPv4 1607742      0t0  UDP *:53socat    61  root 5u  IPv6 1607104      0t0  TCP *:8443 (LISTEN)socat    83 alice 6u  IPv4 1605106      0t0  TCP 192.168.91.10:8080->192.168.91.20:41384 (ESTABLISHED)$ lsof -nP -i6COMMAND PID USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEsocat    61 root 5u  IPv6 1607104      0t0  TCP *:8443 (LISTEN)

71.5 lsof -s: TCP state filter

  • -sTCP:LISTEN gives only listening sockets; -sTCP:^LISTEN gives everything except LISTEN.
  • -sTCP:CLOSE_WAIT catches sockets the peer closed but the app did not (an app bug).
  • State names depend on the OS: on this Linux build SYN_RECV and FIN_WAIT2 work, SYN_RCDV, FIN_WAIT_2, IDLE and BOUND are unknown.
  • In the lab lsof 4.99.7 crashed (exit 139) on the UDP state filter -sUDP:Idle.
  • -s alone (no protocol) shows the SIZE column of a file; put the file after --.
Terminal
lsof -nP -iTCP -sTCP:LISTENlsof -nP -iTCP -sTCP:CLOSE_WAITlsof -nP -iTCP -sTCP:SYN_SENT   # cli
Output
$ lsof -nP -iTCP -sTCP:LISTENCOMMAND PID  USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEpython3  31  root 3u  IPv4 1607027      0t0  TCP *:80 (LISTEN)socat    37 alice 5u  IPv4 1604960      0t0  TCP *:8080 (LISTEN)python3  49  root 3u  IPv4 1605998      0t0  TCP *:9090 (LISTEN)socat    61  root 5u  IPv6 1607104      0t0  TCP *:8443 (LISTEN)$ lsof -nP -iTCP -sTCP:CLOSE_WAITCOMMAND PID USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEpython3  49 root 4u  IPv4 1605999      0t0  TCP 192.168.91.10:9090->192.168.91.20:37600 (CLOSE_WAIT)$ lsof -nP -iTCP -sTCP:SYN_SENT   # cliCOMMAND PID USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEbash     46 root 3u  IPv4 1609764      0t0  TCP 192.168.91.20:60588->192.168.91.99:7070 (SYN_SENT)

71.6 Process, user, command and FD selection

OptionSelects
-p 1234,456 / -p ^1234These PIDs / exclude a PID
-u alice,548 / -u ^rootUser name or UID / exclude a user
-c socat / -c '/^py/i'Command name prefix / regex
-RAdds a PPID column
+c 0Full command name (default is 9 characters)
-d 0-2 / -d cwdOnly these FDs (stdin, stdout, stderr) / working directory
+d /var/log / +D /homeDirectory top level only / the whole tree (slow)
+L1Files that are deleted but still open
Terminal
lsof -nP -R -i -a -c socatlsof -p 31 -a -d 0-2lsof +L1
Output
$ lsof -nP -R -i -a -c socatCOMMAND PID PPID  USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEsocat    37    0 alice 5u  IPv4 1604960      0t0  TCP *:8080 (LISTEN)socat    55    0  root 5u  IPv4 1607742      0t0  UDP *:53socat    61    0  root 5u  IPv6 1607104      0t0  TCP *:8443 (LISTEN)socat    83   37 alice 6u  IPv4 1605106      0t0  TCP 192.168.91.10:8080->192.168.91.20:41384 (ESTABLISHED)$ lsof -p 31 -a -d 0-2COMMAND PID USER FD   TYPE DEVICE SIZE/OFF    NODE NAMEpython3  31 root 0r   CHR    1,3      0t0       4 /dev/nullpython3  31 root 1w  FIFO   0,15      0t0 1601409 pipepython3  31 root 2w   REG  0,127       64  164521 /var/log/app.log$ lsof +L1COMMAND PID USER FD   TYPE DEVICE SIZE/OFF NLINK   NODE NAMEsleep    74 root 4w   REG  0,127  5000000     0 164527 /tmp/backup.tmp (deleted)
  • PPID 37 for child 83: socat forks one child per connection.
  • The stderr (FD 2w) of python goes to /var/log/app.log.
  • The 5 MB /tmp/backup.tmp is deleted (NLINK 0) but still holds disk space until PID 74 closes it.

71.7 Reading the FD and TYPE columns

ColumnValueMeaning
FDcwd / rtd / txt / memCurrent dir / root dir / program code / memory-mapped file
FD3uFD number 3 + mode: r read, w write, u read+write
TYPEREG / DIRRegular file / directory
TYPECHR / BLK / FIFOCharacter device / block device / pipe
TYPEIPv4 / IPv6Network socket
TYPEunixUNIX domain socket
Output
$ lsof -nP -p 31COMMAND PID USER  FD   TYPE  DEVICE SIZE/OFF    NODE NAMEpython3  31 root cwd    DIR   0,127     4096  136564 /tmppython3  31 root rtd    DIR   0,127     4096  164579 /python3  31 root txt    REG   0,127    14080  137649 /usr/bin/python3.12python3  31 root mem    REG   0,127  6049528  138020 /usr/lib/libpython3.12.so.1.0python3  31 root   0r   CHR     1,3      0t0       4 /dev/nullpython3  31 root   1w  FIFO    0,15      0t0 1601409 pipepython3  31 root   2w   REG   0,127       64  164521 /var/log/app.logpython3  31 root   3u  IPv4 1607027      0t0     TCP *:80 (LISTEN)python3  31 root   4u  IPv4 1607785      0t0     TCP 192.168.91.10:80->192.168.91.20:46882 (ESTABLISHED)

71.8 Output format, repeat mode and -F for scripts

  • -l prints the UID (1000 for alice) instead of the login name.
  • -t prints only PIDs: perfect for kill $(lsof -t -i :8080).
  • -Tqs adds queue lengths and state (QR=0 QS=0); -V reports what was not found.
  • +fg adds a FILE-FLAG column (W,AP = write, append); +fc/+ff/+fn are macOS only.
  • -r 1c2 repeats every 1 s for 2 rounds with a ======= marker; +r repeats until the result is empty.
  • -F prints one field per line with a letter prefix (p PID, c command, f FD, n name, P protocol, T TCP info); -F? lists all letters, -F0 uses NUL.
Terminal
lsof -t -i :8080lsof -nP -V -i :9999lsof -nP -i :8080 -sTCP:LISTEN -r 1c2lsof -nP -iTCP:8080 -a -c socat -F pcfnPT
Output
$ lsof -t -i :80803783$ lsof -nP -V -i :9999lsof: Internet address not located: :9999$ lsof -nP -i :8080 -sTCP:LISTEN -r 1c2COMMAND PID  USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEsocat    37 alice 5u  IPv4 1604960      0t0  TCP *:8080 (LISTEN)=======COMMAND PID  USER FD   TYPE  DEVICE SIZE/OFF NODE NAMEsocat    37 alice 5u  IPv4 1604960      0t0  TCP *:8080 (LISTEN)=======$ lsof -nP -iTCP:8080 -a -c socat -F pcfnPTp37csocatf5PTCPn*:8080TST=LISTENTQR=0TQS=0p83csocatf6PTCPn192.168.91.10:8080->192.168.91.20:41384TST=ESTABLISHEDTQR=0TQS=0
  • Performance: -b avoids blocking kernel calls (dead NFS server), -S 5 sets a timeout, -X skips TCP/UDP sockets on Linux.
  • Not on this Linux build: -D (device cache) gave 'unsupported option', -Z (SELinux) gave 'illegal option character', -z is for Solaris zones.

71.9 fuser: options

  • fuser 8080/tcp prints only PIDs; -v adds USER, ACCESS and COMMAND like ps.
  • -u adds the user name, -s only sets the exit code (0 = in use, 1 = free).
  • -m NAME lists every user of a whole filesystem; -M acts only if NAME really is a mount point.
  • -k kills (default signal SIGKILL), so give -TERM first; -i asks before every kill, -w kills only writers.
  • -n chooses the name space: file, tcp or udp. A socket is local_port,remote_host,remote_port.
Terminal
fuser -v 8080/tcpfuser -u 80/tcpfuser -v -n tcp 8080,192.168.91.20fuser -k -TERM 8080/tcpfuser -v 8080/tcp; echo "exit $?"
Output
$ fuser -v 8080/tcp                     USER        PID ACCESS COMMAND8080/tcp:            alice        37 F.... socat                     alice        83 F.... socat$ fuser -u 80/tcp80/tcp:                 31(root)$ fuser -v -n tcp 8080,192.168.91.20                     USER        PID ACCESS COMMAND8080,192.168.91.20/tcp:                     alice        83 F.... socat$ fuser -k -TERM 8080/tcp8080/tcp:               37    83$ fuser -v 8080/tcp; echo "exit $?"exit 1
fuser: port to PID, and -k frees the port
  1. 1.fuser asks: who uses 8080/tcp?

  2. 2.Answer on srv: 8080/tcp: 37 83

  3. 3.fuser -k -TERM 8080/tcp stops both socat processes

  4. 4.cli sends a new SYN through the bridge to srv

  5. 5.Nobody listens on 8080 any more: the client gets connection refused

71.10 fuser access letters

LetterMeaningLab example
rRoot directoryfuser -v / -> .r...
cCurrent directoryfuser -v /home/alice -> ..c..
eRunning executablefuser -v /usr/bin/socat -> ...e.
mmmap'ed file / shared libraryfuser -v /lib/ld-musl-x86_64.so.1 -> ....m
f / FOpen filefuser -v /var/log/app.log -> F....

71.11 Common mistakes

  • Forgetting -a: lsof -i -u alice is an OR and prints every file of alice.
  • Running without sudo and concluding 'nobody uses the port' when another user's process does.
  • Using fuser -k without -TERM: the default is SIGKILL, so the app gets no chance to clean up.
  • Deleting a big log to free disk: the space comes back only when the holding process closes or restarts.
  • Thinking lsof sees everything: orphan sockets like FIN_WAIT2 with no owning process show only in ss.

71.12 Interview tips

  • 'Port 8080 already in use': sudo lsof -nP -iTCP:8080 -sTCP:LISTEN finds the PID and user; then systemctl stop the service or fuser -k -TERM 8080/tcp.
  • Disk full but a deleted file freed nothing: lsof +L1, then restart the process holding it.
  • lsof = process -> files, fuser = file/port -> PIDs; on Unix a socket is a file descriptor.
  • Always use -nP for speed and -t / -F when the output goes into a script.