Skip to main content

Checking Open Ports

·254 words·2 mins
Sebastian Scheibe
Author
Sebastian Scheibe
Table of Contents

Checking Open Ports on Your System
#

Need to check which applications are listening on a port? Here’s how you can do it on macOS.

Using lsof
#

The lsof command lists process IDs, ports, and network addresses:

lsof -a -PiTCP -sTCP:LISTEN

Example output:

lsof -a -PiTCP -sTCP:LISTEN
COMMAND     PID    USER       FD   TYPE   DEVICE SIZE/OFF NODE NAME
rapportd   1063   user        9u  IPv4    ...    0t0     TCP *:59797 (LISTEN)
ControlCe  1105   user        8u  IPv4    ...    0t0     TCP *:7000 (LISTEN)
com.docke  95796  user      133u  IPv6    ...    0t0     TCP *:8000 (LISTEN)

Killing a Process
#

Use pkill with the process ID to terminate a process:

pkill -P <PID>

For example:

pkill -P 95796

port-report – A Better Alternative
#

For a more user-friendly tool, try port-report. It provides detailed information, including:

  • Port
  • Address
  • Protocol
  • Process ID (PID)
  • User
  • Command
  • Working directory
  • Full executable path

Processes are sorted by port in ascending order, and you can kill a process by pressing Enter.

Example output:

┌─────┬─────────┬────────┬─────┬────┬───────────────┬──────────────────────────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────┐
│Port │Address  │Protocol│PID  │User│Command        │Working Directory                         │Executable Path                                                                          │
├─────┼─────────┼────────┼─────┼────┼───────────────┼──────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────┤
│5000 │*        │IPv6    │1105 │501 │ControlCenter  │/                                         │/System/Library/CoreServices/ControlCenter.app/Contents/MacOS/ControlCenter              │
├─────┼─────────┼────────┼─────┼────┼───────────────┼──────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────┤
│7679 │localhost│IPv6    │79552│501 │Google Drive   │/                                         │/Applications/Google Drive.app/Contents/MacOS/Google Drive                               │
├─────┼─────────┼────────┼─────┼────┼───────────────┼──────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────┤
│11434│localhost│IPv4    │1841 │501 │ollama         │/                                         │/Applications/Ollama.app/Contents/Resources/ollama                                       │
├─────┼─────────┼────────┼─────┼────┼───────────────┼──────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────┤
│59797│*        │IPv6    │1063 │501 │rapportd       │/                                         │/usr/libexec/rapportd                                                                    │
├─────┼─────────┼────────┼─────┼────┼───────────────┼──────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────┤
│60073│localhost│IPv6    │97495│501 │datagrip       │/                                         │/Users/sebastianscheibe/Applications/DataGrip.app/Contents/MacOS/datagrip                │
├─────┼─────────┼────────┼─────┼────┼───────────────┼──────────────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────┤
│64605│localhost│IPv6    │13099│501 │java           │/Users/sebastianscheibe/DataGripProjects/M│/Users/sebastianscheibe/Applications/DataGrip.app/Contents/jbr/Contents/Home/bin/java    │
└─────┴─────────┴────────┴─────┴────┴───────────────┴──────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────┘
╔════════════════════════════════════════════════════════════════════════╗
║                                                                        ║
║          Are you sure you want to kill process 1841 (ollama)?          ║
║                                                                        ║
║                               Yes     No                               ║
║                                                                        ║
╚════════════════════════════════════════════════════════════════════════╝

For installation and more details, visit the Github repository