How to Find and Free Used Ports on Mac with lsof and kill Commands

Learn how to use lsof on macOS to check which TCP ports are in use and safely kill processes with the kill command. A practical guide for developers to fix port already in use errors.

CChia1104
Notes1 minutes read

Check Used Ports on Mac

You can use the lsof command on Mac to check which ports are currently in use.

bash
lsof -i tcp:<PORT>

After running the command, your terminal will display information similar to the example below. What we need here is the PID (Process ID).

bash
COMMAND   PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    41303 chia1104   20u  IPv6 0x58ddc79557aad159      0t0  TCP *:hbci (LISTEN)

Next, you can terminate the process by entering this command in the terminal:

bash
kill -9 <PID>

The -9 flag will immediately force the process to stop without giving it a chance to clean up.
If you want to allow the process to close gracefully, use -15 (TERM) or -3 (QUIT) instead.

Alternative

You can also kill multiple ports at once with a single command:

bash
kill -9 $(lsof -ti:3000,3001)

Written by: Chia1104 CC BY-NC-SA 4.0

Chia1104
©
Chia1104