lsof: lists open files
Show all connections with -i
Show only TCP (works the same for UDP)
-i :port shows all networking related to a given port
To show connections to a specific host, use @host
Show connections based on the host and the port using @host:port
Grepping for "LISTEN" shows what ports your system is waiting for connections on
Grepping for "ESTABLISHED" shows current active connections
lsof -i| grep ESTABLISHED
Show what a given user has open using -u
See what files and network connections a command is using with -c
Pointing to a file shows what's interacting with that file
The -p switch lets you see what a given process ID has open, which is good for learning more about unknown processes
The -t option returns just a PID
Using-a allows you to combine search terms, so the query below says, "show me everything running as daniel connected to 1.1.1.1"
lsof -a -u daniel -i @1.1.1.1
Using the -t and -c options together you can HUP processes
kill -HUP `lsof -t -c sshd`
You can also use the -t with -u to kill everything a user has open
kill -9 `lsof -t -u daniel`
lsof +L1 shows you all open files that have a link count less than 1, often indicative of a cracker trying to hide something