Lab 04: lsof — Open Files & Sockets

Time: 40 minutes | Level: Advanced | Docker: docker run -it --rm --privileged ubuntu:22.04 bash


Overview

lsof (List Open Files) is one of the most powerful diagnostic tools on Linux. Because "everything is a file" — including network sockets, pipes, devices, and directories — lsof provides a unified view of all resource usage by every process. This lab covers process inspection, socket analysis, deleted file detection, and security auditing.


Step 1: Install lsof and Explore Its Version

apt-get update -qq && apt-get install -y lsof
lsof -v 2>&1 | head -3

📸 Verified Output:

lsof version information:
    revision: 4.93.2
    latest revision: https://github.com/lsof-org/lsof

💡 lsof reads from /proc/ and kernel structures. Many operations require root to see all processes. Run as root in this lab.


Step 2: Understand lsof Output Format

List files opened by the current shell process:

📸 Verified Output:

Column meanings:

Column
Meaning

COMMAND

Process name

PID

Process ID

USER

Owner of the process

FD

File descriptor (cwd=current dir, txt=executable, mem=memory-mapped, numbers=open FDs)

TYPE

REG=regular file, DIR=directory, CHR=char device, FIFO=pipe, IPv4/IPv6=socket

DEVICE

Major:minor device numbers

SIZE/OFF

File size or offset

NODE

Inode number

NAME

File path or socket description


Step 3: List Files by Process (-p) and Command (-c)

📸 Verified Output:

💡 -t (terse) outputs just PIDs, one per line. Combine with kill: kill $(lsof -t -u baduser) to kill all processes owned by a user.


Step 4: Network Sockets with -i

lsof -i lists all network connections:

📸 Verified Output (in a minimal container):

On a real system with services running:

💡 Use lsof -i :PORT to find which process owns a port. This is faster than netstat -tlnp and works even when netstat isn't installed.


Step 5: Find Deleted Files Still Held Open

When a file is deleted but still open by a process, disk space isn't freed. lsof reveals these "ghost" files:

📸 Verified Output:

Common scenario: A log file gets rotated/deleted, but the service still writes to the old FD. Disk fills up even though ls shows nothing. The fix is systemctl reload service to reopen log files.

💡 After finding the process holding the deleted file, kill -HUP $PID (SIGHUP) often causes services to reopen their log files — releasing the old inode.


Step 6: List Files in a Directory with +D

Find all processes with files open inside a directory (useful before unmounting):

📸 Verified Output:

💡 lsof +D /path is recursive and can be slow on large directories. Use lsof +d /path (lowercase d) for non-recursive listing of just that directory.


Step 7: Security Audit Use Cases

Security red flags in lsof output:

  • Processes with txt entries pointing to /tmp or /dev/shm (executables in temp dirs)

  • (deleted) entries for executable files (malware hiding its binary)

  • Unexpected outbound connections from system processes

  • High FD count for a single process (possible FD leak or DoS)

💡 Run lsof -u nobody -i to see all network activity by the nobody user — often used by web servers. Unexpected outbound connections here signal potential compromise.


Step 8: Capstone — Security Audit of a Running System

Scenario: Perform a complete open-file security audit.

📸 Verified Output:


Summary

Command
Purpose

lsof

List all open files system-wide

lsof -p PID

Files opened by a specific process

lsof -c bash

Files opened by processes named "bash"

lsof -u root

Files opened by a specific user

lsof -i

All network connections

lsof -i :PORT

Find process using a specific port

lsof -i TCP -sTCP:LISTEN

All TCP listeners

lsof +D /path

All files open inside a directory (recursive)

lsof +d /path

Files open in directory (non-recursive)

lsof -t -u user

Just PIDs for user's open files

lsof | grep '(deleted)'

Find deleted files still held open

lsof -n

Don't resolve hostnames (faster output)

Last updated