Sebastian De Deyne
Designer & developer at Spatie

Finding out which ports are in use

Sometimes you want to spin up a process, but the port it wants to bind to is already in use. Or a port isn’t listening to a process as you expected. lsof is a debugging life saver in these situations.

lsof -i -P -n | grep LISTEN

This will list all processes listening to a port.

nginx      514 sebastiandedeyne    7u  IPv4 0x2718856ef232ee5b      0t0  TCP 127.0.0.1:80 (LISTEN)
nginx      514 sebastiandedeyne    8u  IPv4 0x2718856ef233026b      0t0  TCP 127.0.0.1:443 (LISTEN)
nginx      514 sebastiandedeyne    9u  IPv4 0x2718856ef2330c73      0t0  TCP 127.0.0.1:60 (LISTEN)

If you want to find a process on a specific port, you can chain another grep.

lsof -i -P -n | grep LISTEN | grep 80
nginx      514 sebastiandedeyne    7u  IPv4 0x2718856ef232ee5b      0t0  TCP 127.0.0.1:80 (LISTEN)

If you enjoyed this post, you might be interested in my newsletter. I occasionally send a dispatch with personal stories, things I’ve been working on in the past month, and other interesting tidbits I come across online.