How to Find a Used TCP Port and Kill the Associated Process on Your Mac

Published: Jan 30, 2019
Updated: May 3, 2021

Sometimes the port you need is already used by another process. To find the offending process:

sudo lsof -i tcp:<port>

Now, usually you’d just close out the app that was using the port. But let’s say you’re feeling destructive and you wanna kill the process:

sudo kill <pid>

For fun, the above commands can be combined into this one-liner:

sudo kill $(sudo lsof -i tcp:<port> | tr -s " " | cut -d " " -f 2 | sed -n 2p)

Credits to this stackoverflow post.