Find and Free Used Ports on macOS with lsof and kill

Use lsof to see which process holds a TCP port on macOS and kill it to fix 'port already in use' errors.

CChia1104
  • Notes
  • 1 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)
Last updated

Written by: Chia1104 CC BY-NC-SA 4.0