HxHippy

lsof

List open files and the processes that opened them

Overview

Lists information about files opened by processes. Since everything in Linux is a file (including network sockets), lsof is useful for debugging and system administration.

Syntax

lsof [options]

Common Options

-i [address]

Select by Internet address (shows network connections).

-p PID

Select by PID.

-u USER

Select by user name or UID.

-c COMMAND

Select by command name.

+D DIRECTORY

Recursively search all files in a directory.

-t

Terse output (PIDs only).

-n

Do not convert network numbers to host names.

-P

Do not convert port numbers to port names.

Examples

$ lsof

List all open files.

$ lsof -i :80

Show processes using port 80.

$ lsof -i :22

Show processes using SSH port.

$ lsof -i tcp

Show all TCP connections.

$ lsof -i -P -n

Show all network connections with numeric addresses/ports.

$ lsof -u nginx

Show all files opened by nginx user.

$ lsof -p 1234

Show all files opened by process 1234.

$ lsof -c nginx

Show all files opened by nginx processes.

$ lsof +D /var/log

Show all open files in /var/log directory.

$ lsof /var/log/syslog

Show what processes have syslog open.

$ kill -9 $(lsof -t -i :3000)

Kill all processes using port 3000.

lsoffilesopenprocessportsocketnetworkdebug