Lsof examples

lsof is one of the not well known but very powerfull utility in Linux.

man lsof

basically says that lsof: list open files. But there is more.

You can use it for managing and tracking network connections, list open ports, identify connections currently being made to your system, and determine what resources a process is using. Not only that, but you can also determine what processes a particular user has and find detailed information about file and directory usage. Here some example usages:

List all open files in system:

lsof

List all network connections:

lsof -i

List all UDP connections:

lsof -iUDP

List connections on specific port number:

lsof -i :80

List connections on specific host:

lsof [email protected]

List connection on specifics host and port:

lsof [email protected]:80

List all of the ipv6 connections:

lsof -i 6

List all files opened by user fred:

lsof -u fred

List all files which opened by specific command:

lsof -c mysqld

List all process which using a specific file:

lsof /var/run/mysqld/mysqld.sock

List all processes which opens files under specific directory:

lsof +d /etc/passwd

List all the open files used by a specific process with process id:

lsof -p 8729