Why is this port taken β and can I free it? One command that explains who holds a port and stops it the right way.
π’ port Β βΒ π§© process Β· container Β· service Β βΒ π clean stop
When a server fails with address already in use, you normally combine lsof or ss, docker ps, systemctl and ps to find out who is responsible. portwhy answers it in one step: it follows the chain from the port to the process, its parents, and the container or service behind it β then offers to stop the cause, never silently.
port βββΆ process βββΆ parents βββΆ Docker β systemd β launchd β plain process
β
explanation + advice βββΆ [--kill] stop it, then verify the port is free
| Method | Command | |
|---|---|---|
| πΊ | Homebrew (macOS) | brew install JtheGunner/tap/portwhy |
| π₯ | Binary (macOS, Linux) | Download the archive for your platform from Releases and put portwhy on your PATH |
| πΉ | From source | go install github.com/JtheGunner/portwhy/cmd/portwhy@latest |
portwhy is one static binary for macOS and Linux (amd64 and arm64).
portwhy # overview of every listening port, grouped by service
portwhy 3000-3010 # overview of a range
portwhy --source docker # overview of the ports held by Docker containers only
portwhy 3000 # explain one port in detail
portwhy 3000 5432 # several ports in detail
portwhy 3000 --kill # explain, then stop the cause after confirmationWithout arguments, and for ranges, portwhy prints a compact table: one row per service or process, with its ports, protocol, bind address and owner. Free ports are left out. β marks sockets reachable from the network, and a summary line counts busy ports, services and hidden owners.
$ portwhy
Service / process Ports Proto Bind Owner
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Spotify 1900, 5353 udp 0.0.0.0 PID 13250
Antigravity 3000, 3100, 3306, 3890 tcp 127.0.0.1 PID 3983
β Docker container "db" (postgres:16) 5432 tcp 0.0.0.0 postgres (PID 812)
Homebrew service "php@8.4" 9000 tcp 127.0.0.1 php-fpm (PID 3884) +2 more
β Owner not visible 22, 88, 137, 138, 445 tcp 0.0.0.0 run with sudo
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
13 busy ports Β· 4 services Β· β 8 reachable from the network Β· 5 ports with hidden owner
βΉ Details: portwhy 1900 Airy view: --expanded Filter: --source docker|launchd|process
Add --expanded for an airier layout with a titled rule per service, one row per port and a summary box. It suits a short list better than a busy machine.
$ portwhy --expanded
ββββββββββββββββββββββββββββββββββββββββ Spotify Β· 2 ports βββββββββββββββββββββββββββββββββββββββββ
β 1900 udp 0.0.0.0 PID 13250
β 5353 udp 0.0.0.0 PID 13250
The hint under the all-ports overview names the sources currently in use, so you can see what to filter by. --source accepts docker, systemd, launchd and process (a plain process without a service). Repeat the flag or separate values with commas to combine them. With --kill, the filter also limits which ports are stopped.
A named port gets a detailed block that names the service, lists its facts and shows how it would be stopped. A free port says so:
$ portwhy 9000 3000
βββββββββββββββββββββ :9000 Β· Homebrew service "php@8.4" βββββββββββββββββββββ
βΉ tcp 0.0.0.0
Process php-fpm (PID 3884) +2 more
Uptime 3d 0h
Label homebrew.mxcl.php@8.4
β Stop brew services stop php@8.4
β Reachable from the network (bound to all interfaces).
βββββββββββββββββββββββββββββββββββ :3000 ββββββββββββββββββββββββββββββββββββ
β free
Several processes of one service on the same port (a master and its workers) share one block. On a terminal, lines fit the terminal width, capped at 100 columns, and a long command wraps over at most three lines. Piped output uses 78 columns and is never shortened. Use --wide to use the full terminal width and keep long values intact. Terminals without Unicode support (TERM=dumb or a non-UTF-8 locale) get plain ASCII symbols and lines.
With --kill, every port gets a status line (β stopped, βΉ left untouched, β failed) on stderr, followed by a Result box.
Tip
Add --dry-run to --kill to see exactly what would be executed without stopping anything.
--plain prints one line per listening socket, without grouping, colour or shortening, for grep, awk and cut. Columns are separated by a tab, in this fixed order:
| # | Column | Example |
|---|---|---|
| 1 | port | 5432 |
| 2 | protocol (tcp or udp) |
tcp |
| 3 | address | 0.0.0.0 |
| 4 | PID (0 if the owner is not visible) |
812 |
| 5 | source that explained the socket (docker, systemd, launchd, process) |
docker |
| 6 | process name (- if the owner is not visible) |
postgres |
Lines are ordered by port, protocol, address and PID. A socket with a hidden owner gets its own line. A port with no listener prints nothing. Exit codes are unchanged, and --plain cannot be combined with --json (exit code 2).
portwhy --plain | awk -F'\t' '$5 == "docker" { print $1 }'| Flag | Effect | |
|---|---|---|
| π | -k, --kill |
stop the cause of a busy port after confirmation |
| β | -y, --yes |
skip the confirmation prompt (requires --kill) |
| π₯ | --force |
send SIGKILL immediately instead of SIGTERM (requires --kill) |
| π | --dry-run |
show what --kill would do without doing it |
| π | --source <name> |
only show ports held by this source: docker, systemd, launchd or process (repeatable, comma-separated) |
| π€ | --json |
print machine-readable JSON ("version": 1) |
| π | --plain |
print one tab-separated line per socket: port, protocol, address, PID, source, name (cannot be combined with --json) |
| π¨ | --no-color |
disable coloured output (NO_COLOR, TERM=dumb and non-TTY output are honoured too) |
| πͺ | --expanded |
show the overview of all ports in the airy layout, one row per port |
--wide |
use the full terminal width and keep long values intact | |
| π·οΈ | -v, --version |
print the version |
| Code | Meaning |
|---|---|
0 |
at least one busy port was explained and nothing failed |
1 |
all requested ports are free |
2 |
error (invalid input, unreadable data, unsupported OS) |
3 |
a kill failed |
When several apply, the precedence is 2 > 3 > 0 > 1.
Important
Stopping is always an explicit, confirmed action. The prompt appears only when stdin and stdout are terminals.
- Each source is stopped the way it expects:
docker stop,systemctl stop,launchctl bootout,brew services stop, or a signal for a plain process. - portwhy never stops PID 1 or itself, re-checks the process start time before signalling (PID reuse), and verifies afterwards that the port is free.
- A process owned by another user is reported with a
sudohint. portwhy never escalates silently. - A port held by a process that portwhy cannot see is never reported as free.
- Hints warn when the cause will come back, for example a Docker restart policy or a systemd
Restart=setting. A systemd explanation says whether the port holder is the main process of the unit and warns thatsystemctl stopends the whole unit.
Tab completes flags and the values of --source (portwhy --source d<Tab> β docker).
With Homebrew the completions for bash, zsh and fish are installed automatically. Restart your shell to pick them up. For a manual install, load the script once:
| Shell | Enable for the current session | Enable permanently | |
|---|---|---|---|
| π | bash | source <(portwhy completion bash) |
portwhy completion bash > ~/.local/share/bash-completion/completions/portwhy |
| π | zsh | source <(portwhy completion zsh) |
portwhy completion zsh > "${fpath[1]}/_portwhy" |
| π | fish | portwhy completion fish | source |
portwhy completion fish > ~/.config/fish/completions/portwhy.fish |
Note
bash needs the bash-completion package, and zsh needs compinit enabled in ~/.zshrc.
Run portwhy completion <shell> --help for more details on each shell.
Requires Go 1.27 or newer.
go build ./...
go vet ./...
golangci-lint run
go test ./...
go test -tags e2e ./e2e # end-to-end tests against real local listeners
go run ./cmd/portwhy --versionPull requests are also checked by doc-drift-checker, which warns when code linked to a README section changed but the section did not. The links live in .docdrift.yaml; the check only warns and never fails the build.
See CHANGELOG.md.
Issues and pull requests are welcome. Please open an issue first for larger changes, and run go vet ./..., golangci-lint run and go test ./... before submitting.
MIT β see LICENSE