How to Check and Manage Listening Network Ports in macOS Using Terminal

Whether testing local web servers, debugging Node.js or Python APIs, setting up Docker containers, or troubleshooting mysterious system network conflicts, Mac developers frequently encounter the dreaded “Error: listen EADDRINUSE: address already in use” notification. When a background daemon or forgotten process locks a specific network port (such as 3000, 8080, or 5000), identifying which application holds the socket binding is essential. On macOS, you can quickly inspect and terminate listening network connections using native Terminal utilities.

Why Check Listening Ports on Mac?

A listening port is an open socket waiting for incoming network traffic. Monitoring open ports provides multiple diagnostic and security benefits:

  • Resolving Local Development Collisions: Discover zombie server processes holding ports 3000, 8000, or 5432 after an editor or terminal window was abruptly closed.
  • Security Auditing: Identify unexpected background services or third-party applications broadcasting on your local network.
  • AirPlay Port Conflicts: Diagnose conflicts with macOS native features like AirPlay Receiver, which binds to port 5000 and 7000 by default.

Method 1: Using lsof to List All Active Listening TCP Ports (Recommended)

The lsof (List Open Files) command is the most effective tool on macOS for discovering open network sockets and correlating them directly to their parent Process ID (PID):

  1. Open Terminal on your Mac (press Cmd + Space, type Terminal, and hit Enter).
  2. To view every TCP socket currently in a listening state, execute the following command:
    sudo lsof -iTCP -sTCP:LISTEN -n -P
  3. Enter your macOS administrator password when prompted.

Understanding the Command Flags

  • -iTCP: Restricts the query strictly to TCP network files.
  • -sTCP:LISTEN: Filters results so only sockets actively listening for incoming connections are reported (ignoring established client connections).
  • -n: Inhibits DNS hostname lookups, making command execution instantaneous and printing raw IP addresses (e.g., 127.0.0.1 or *).
  • -P: Inhibits network port name conversions, printing numerical port values (e.g., :8080) rather than protocol aliases (e.g., http-alt).

Method 2: Finding Which Process is Using a Specific Port Number

If your build script failed specifically because port 3000 or 8080 is blocked, you can target that exact port number directly:

lsof -i :3000

Terminal outputs a concise table displaying the active binding:

COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node    48215 santh   22u  IPv6 0x8a9271c79812b123      0t0  TCP *:3000 (LISTEN)

From this output, note the COMMAND (e.g., node) and the PID (Process ID, e.g., 48215).

Method 3: Using netstat to Inspect Network Sockets

The BSD netstat utility built into macOS provides a system-wide view of network protocol tables:

netstat -anvp tcp | grep LISTEN

While netstat provides detailed protocol states and send/receive queue capacities, lsof remains superior for general development because it displays human-readable process names and exact user ownership.

How to Terminate (Kill) a Process Holding a Port

Once you have identified the Process ID (PID) locking your port, you can gracefully stop or force-terminate it:

1. Graceful Termination (SIGTERM)

kill 48215

This asks the program to save its state, flush open database buffers, and exit cleanly.

2. Forceful Termination (SIGKILL)

If the background process is unresponsive or hung in a loop, force-kill it immediately using the -9 flag:

kill -9 48215

3. Kill by Port Shortcut (One-Liner)

For fast daily development workflows, you can combine lsof with kill in a single pipeline to free port 3000 instantly:

kill -9 $(lsof -t -i :3000)

The -t flag instructs lsof to output only the raw PID number, which is passed directly to kill.

Troubleshooting macOS Port 5000 and 7000 (AirPlay Receiver)

In macOS Monterey, Ventura, Sonoma, and Sequoia, users frequently discover port 5000 occupied by a system daemon named ControlCenter. This is caused by Apple’s built-in AirPlay Receiver:

  1. Open System Settings > General > AirDrop & AirPlay.
  2. Locate the AirPlay Receiver setting.
  3. Toggle the switch to Off.
  4. Port 5000 will be instantly released, making it available for local Docker containers and Flask backends without port collisions.

Mastering network socket inspection commands in Terminal gives you complete mastery over running services, ensuring seamless local development on macOS.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.