What's running on port X?

unix
Posted on: 2014-08-25

Ever wondered what’s running on a particular port? lsof (list of open files) is a handy command for asking questions like that. After all, in Unix-speak, a socket is a file.

Handy as it is, I don’t use lsof often enough to remember its flags, so I added this little alias to my zsh configuration.

# See what is listening on a given port: `whoport 80`
function whoport(){ lsof -i :$1 } # may need sudo

That should give you output something like this, which shows I have Redis running on port 6379 and some Ruby processes connected to it. (Note: this is on OSX.)

$: whoport 6379

COMMAND     PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ruby      19109 yourname 13u  IPv4 0xbaac0f9da5f0de31      0t0  TCP localhost:53398->localhost:6379 (ESTABLISHED)
ruby      19109 yourname 14u  IPv4 0xbaac0f9da0621e31      0t0  TCP localhost:53399->localhost:6379 (ESTABLISHED)
redis-ser 19110 yourname  4u  IPv4 0xbaac0f9da16b7649      0t0  TCP *:6379 (LISTEN)
redis-ser 19110 yourname  5u  IPv4 0xbaac0f9da16c7649      0t0  TCP localhost:6379->localhost:53390 (ESTABLISHED)
...