How to find what is using a port on a Mac, and stop it
“Port 3000 is already in use”? Find the process listening on a port on macOS with lsof, stop it safely, and see every listening port and its project.
By winterzxzzUpdated 4 min read
To find what is using a port on a Mac, open Terminal and run lsof -nP -iTCP:3000 -sTCP:LISTEN, with your port in place of 3000. It prints the name and process ID (PID) of whatever is listening; kill with that PID frees the port. That is the fix for “address already in use” or “port 3000 is already in use” when a dev server didn’t shut down. Below are the steps, what to watch out for, and how MacStat, the menu bar system monitor we make, shows every listening port with its project so you don’t need the command at all.
Find the process with lsof
- Open Terminal (Applications → Utilities).
- Run
lsof -nP -iTCP:3000 -sTCP:LISTEN.-iTCP:3000selects TCP port 3000,-sTCP:LISTENkeeps only listening sockets, and-nPshows numbers instead of looking up host and port names, which is faster. - Read the COMMAND and PID columns. A line such as
node 48211 … TCP *:3000 (LISTEN)means a Node process with PID 48211 owns the port. - To list every listening port at once, run
lsof -nP -iTCP -sTCP:LISTEN.
If the command prints nothing but the port is still taken, the owner may run as another user, such as root. lsof only sees your own processes unless you run it with sudo.
Check what it is before you stop it
A PID alone doesn’t tell you which project a process belongs to. Run ps -p 48211 -o pid,command to see its full command line, and lsof -p 48211 | grep cwd to see its working folder. Make sure it isn’t something you need, such as a database another project depends on.
Stop it
kill 48211asks the process to quit, so it can close files and connections cleanly.- If it is still listening after a few seconds,
kill -9 48211ends it immediately. Use it as a last resort; the process gets no chance to clean up. - If it comes straight back, a supervisor restarts it: a Homebrew service (
brew services list), a launch agent or a Docker container. Stop it there instead.
Port 5000 or 7000 taken by AirPlay
If lsof shows ControlCe (Control Center, cut short) on port 5000 or 7000, it isn’t a leftover server. Apple lists both ports as used by AirPlay, and on recent macOS versions the AirPlay Receiver, which lets other devices stream to your Mac, listens on them. Either run your dev server on another port, or turn off AirPlay Receiver in System Settings → General → AirDrop & Handoff if you don’t stream to your Mac.
Other ways to look
lsof -nP -i :3000(without-sTCP:LISTEN) also shows connections to that port, such as your browser talking to the dev server.- Activity Monitor finds the process by name or PID once you know it, and Quit Process there does the same as
kill.
Mind ports reachable from your network
In lsof output, 127.0.0.1:3000 or [::1]:3000 means only your Mac can connect. *:3000 means the process listens on every interface, so other devices on the same network can reach it too. That is what you want when testing on a phone, and not what you want for a database on café Wi-Fi.
See every listening port in the menu bar
MacStat’s Ports tab does the lookups above for you. It lists every listening TCP port (UDP if you turn it on) with:
- the process and the project folder it runs in, and how it was launched;
- a Network badge on ports that are reachable from other devices on your network;
- Open in Browser, Copy URL, Copy Network URL and a QR code to open it on a phone on the same Wi-Fi;
- Reveal Project Folder, Copy PID, and Stop Process.
A filter finds a port, process or project as you type, and listeners owned by macOS services are hidden unless you ask for them. Like lsof without sudo, MacStat can’t see ports owned by root without admin rights. In the Apps tab, each app’s detail view also lists the ports it listens on.
Related guides
A dev server that won’t quit often shows up as CPU load too: see how to check CPU usage on a Mac. For long builds, read how to keep your Mac awake, or browse all system monitoring guides.
Sources
man lsof(-i,-s,-n,-P,-p)man killandman pson macOS- Apple Support: TCP and UDP ports used by Apple software products
- Apple Support: AirPlay Receiver settings on Mac
- Homebrew documentation: brew services
- MacStat product page (features, requirements)
More on this topic: System monitoring guides · MacStat