macOS Terminal Commands - List Listening Processes

Learn how to list all processes listening on specific ports on macOS using the lsof command. Essential for network troubleshooting and system administration.

macOS Terminal Command: List Listening Processes

This page provides a useful macOS terminal command for developers and system administrators. Understanding which processes are listening on which ports is crucial for network troubleshooting, identifying potential conflicts, and ensuring your applications are running as expected.

List Processes Listening on Ports

The following command uses lsof (list open files) combined with grep to filter the output and display only the processes that are in a LISTEN state, indicating they are actively waiting for incoming network connections.

sudo lsof -i -P | grep LISTEN

Command Breakdown:

  • sudo: Executes the command with superuser privileges, which is often necessary to view all system processes.
  • lsof -i -P:
    • -i: Selects the listing of network files (Internet connections).
    • -P: Inhibits the conversion of port numbers to port names, making the output more direct.
  • |: This is a pipe, which sends the output of the lsof command as input to the next command.
  • grep LISTEN: Filters the input stream and displays only lines containing the word "LISTEN".

Understanding the Output

The output will typically show columns such as:

  • COMMAND: The name of the process.
  • PID: The Process ID.
  • USER: The user running the process.
  • FD: File descriptor.
  • TYPE: Type of file (e.g., IPv4, IPv6).
  • DEVICE: Device numbers.
  • SIZE/OFF: Size or offset.
  • NODE: Node number.
  • NAME: The network address and port the process is listening on (e.g., *:80 (LISTEN) or localhost:5432 (LISTEN)).

For more advanced network diagnostics on macOS, consider exploring commands like:

  • netstat -tulnp: Another common command to display network connections, routing tables, interface statistics, etc. (though lsof is often preferred on macOS for its detailed output).
  • ifconfig: To view and configure network interfaces.
  • ping: To test network connectivity to a host.

Mastering these terminal commands can significantly enhance your ability to manage and troubleshoot your macOS system.