Back to article
lsof Exposes Anyone Connected to Your Linux Computer
Who is Connected to Your Machine?
April 30, 2009
When discussing lsof and open files a few weeks ago, I also mentioned that lsof -i will show you information about which IP sockets are open.
This can be useful in checking for security problems or just to keep an eye on how much your machines are interacting with the wider internet. Here's a little sample output:
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
You'll quite often see two reports from a single command, one for TCP and one for UDP. The PID and user is shown for each IP socket, so if you see something suspicious, you can investigate further (e.g., with ps -l 5763 to get more information on the mysql process here, or by looking at /proc/5763/).
For a real-time look at what's being opened where, try watch lsof -i (hit Ctrl-C to exit). To check out who's connecting to your web server, specify the port with lsof -i :80: You'll get a list that shows which IP addresses or hosts are looking at your web server right now. You can also check connections to a particular host with lsof -email@example.com (or give the IP address instead).
You can get similar information on which Unix sockets are open on your machine, with lsof -U. Unix sockets handle inter-process communication, a bit like IP sockets but within a single machine rather than across a network. This information can be useful when using strace to track down bugs.
Article courtesy of Serverwatch.com