Troubleshooting

"bind: Address already in use": the local port for an SSH tunnel is taken

ssh -L needs a free port on your computer. When another program holds it, Linux and macOS say Address already in use. Windows' built-in ssh says Permission denied, or sometimes binds anyway without a word.

Level
Beginner
Reading time
9 min
Applies to
Windows, macOS, Linux, OpenSSH command line
Transputty steps checked against
Transputty 0.1 (development build, not released)

Before you start

  • The ssh -L command (or tunnel settings) that fails
  • A terminal on the computer where the tunnel runs
  • Administrator rights only if you need to stop a system service

Short answer

"Address already in use" from ssh -L means another program is already listening on the local port. Either choose a different local port, for example ssh -N -L 5433:db.internal:5432 … instead of 5432, or find and stop the other program. On Windows, find it with netstat -ano | findstr :5432; on macOS and Linux, with lsof -iTCP:5432 -sTCP:LISTEN.

The last section shows how Transputty, an SSH tunnel manager for Windows 11, reports and fixes a busy port. Transputty is in development and has no public build yet; see release status.

The exact error messages

On Linux and macOS, and with the ssh that ships with Git for Windows:

bind [127.0.0.1]:5432: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 5432
Could not request local forwarding.

With the OpenSSH client built into Windows (C:\Windows\System32\OpenSSH\ssh.exe), the first line is different:

bind [127.0.0.1]:5432: Permission denied
channel_setup_fwd_listener_tcpip: cannot listen to port: 5432
Could not request local forwarding.

Both mean the same thing: ssh could not open the listening port. PuTTY was not tested for this page; its Event Log is the place to look for the equivalent failure.

In every case ssh then logs in and stays connected without the forward. The session looks healthy, and your application gets "connection refused" or, worse, reaches whatever program does own the port.

Quick diagnosis

Symptom Likely cause Check
Address already in use or Permission denied on bind, and a process listens on the port Another program holds the port netstat -ano or lsof
Only the bind line, without the two lines after it One of 127.0.0.1 and ::1 was taken; ssh bound the other List listeners for both addresses
No error, but the application reaches the wrong server Windows let ssh bind next to another listener, or the app uses localhost and found the other one netstat -ano shows two owners
Permission denied on Windows and no process listed The port is in a range reserved by Hyper-V or WSL 2 netsh … show excludedportrange
Permission denied for a port below 1024 on Linux or macOS Low ports need root Use a port above 1024

Find what holds the port

Windows

In Command Prompt or PowerShell:

netstat -ano | findstr :5432
tasklist /FI "PID eq 4121"

Look for lines marked LISTENING; the last column is the process ID, which tasklist turns into a name. The PowerShell equivalent:

Get-NetTCPConnection -LocalPort 5432 -State Listen | Select-Object LocalAddress, LocalPort, OwningProcess
Get-Process -Id 4121

Note the LocalAddress column. 0.0.0.0 and :: mean every address; 127.0.0.1 and ::1 are the two loopback addresses, and they can have different owners.

macOS and Linux

lsof -iTCP:5432 -sTCP:LISTEN
ss -ltnp | grep :5432

lsof works on both systems; ss is Linux only. Run them with sudo to see processes that belong to other users.

Common culprits

  • A database installed on your own computer. A local PostgreSQL service holds 5432, MySQL or MariaDB 3306, SQL Server 1433. This is the usual reason a tunnel to the same kind of database fails.
  • An earlier tunnel that is still running. A forgotten ssh or plink in another window or in the background still owns the port. On Windows look for ssh.exe or plink.exe in the tasklist output.
  • Docker. A container published with -p 5432:5432 takes the host port; the owner appears as Docker's proxy or backend process, not as the database.
  • Another tunnel tool or a database client's built-in SSH tunnel using the same number.

Windows surprises worth knowing

Windows does not guard a listening port the way Linux and macOS do, and we saw three results in testing with the built-in ssh:

  1. No error at all. When another program listened on all addresses (0.0.0.0:26502) without asking for exclusive use, ssh -L 26502:… started with no error and took the loopback address for itself. Which listener a client reaches then depends on the address it uses, as Microsoft's socket documentation describes.
  2. Two tunnels on one port. Two ssh.exe processes started with the same -L port both ended up listening, with no warning, even with ExitOnForwardFailure=yes. Every test connection went to the first one.
  3. Half a forward. -L 5432:… without a bind address listens on 127.0.0.1 and ::1. If only one is taken, ssh prints a single bind line, keeps the other address and does not treat the forward as failed.

So on Windows a clean start does not prove the port was free. Check netstat -ano before starting a tunnel on a well-known port, and write the bind address explicitly, as in -L 127.0.0.1:5433:db.internal:5432, so there is exactly one listener to reason about.

Reserved port ranges: refused with no process listed

Hyper-V and WSL 2 (and Docker Desktop, which runs on them) reserve blocks of TCP ports at startup. A port inside a block cannot be bound by any program, and nothing shows up in netstat, because no process owns it. List the ranges:

netsh interface ipv4 show excludedportrange protocol=tcp

The blocks are often a hundred ports wide and can move after a restart, so a port that worked last week may be reserved today. If your port is inside a listed range, choose one outside. Ranges marked with an asterisk are administered exclusions set by an administrator or an installer.

Fixes

Pick another local port. This is almost always the right answer, because the local port is arbitrary:

ssh -N -L 127.0.0.1:5433:db.internal:5432 deploy@bastion.example.com

Then point the application at 127.0.0.1:5433. Only the first number changes; the destination stays 5432.

Stop the other program if the port number must stay as it is, for example because an application has it hard-coded. Close the old tunnel window, stop the container or stop the local service. Think twice before stopping a local database that other software on your computer uses.

Make failures loud. Add ExitOnForwardFailure:

ssh -N -o ExitOnForwardFailure=yes -L 127.0.0.1:5433:db.internal:5432 deploy@bastion.example.com

With it, ssh exits with an error (status 255 in our test) when it cannot bind, instead of staying connected without the forward. Scripts, autossh and service units should always set it. It cannot help in the silent Windows cases above, because there ssh does not see a failure.

Ports below 1024

On Linux, binding a port below 1024 requires root, and ssh run as a normal user fails with bind [127.0.0.1]:443: Permission denied (older versions say Privileged ports can only be forwarded by root.). macOS has relaxed this rule in some cases; if you get the error there, the fix is the same. Use a high local port such as 8443 and connect to that. Do not run ssh as root just to bind a low port. Windows has no such rule: a forward on port 81 started without elevation in our test.

How Transputty reports a busy port

In Transputty a busy port fails one rule, not the connection. In version 0.1, per the source code and feature inventory:

  1. The connection shows "Connected" and "1 of N rules failed". The affected rule shows "Port in use" and the other rules keep working. The Log tab records the event with the code BIND_IN_USE.
  2. A callout under the row and in the inspector reads "Can't listen on 127.0.0.1:5432", followed by "Another app is using it: postgres.exe (PID 4121)."
  3. Three buttons follow: Use :5433 (the next free port; the number varies), Retry and Edit rule….
  4. Use :5433 restarts the rule on that port without reconnecting, saves the change and confirms with a message such as "db now listens on 127.0.0.1:5433". Update your application to the new port. To keep the original port, close the other program and click Retry.
A real conflict on the capture machine: the connection is 'Connected' with '1 of 2 rules failed', the rule's pill reads 'Port in use', and the callout names a program and its PID and offers a Use button for the next free port, Retry and Edit rule. The app is connected to local test servers with fictional host names.

Two caveats. The lookup that names the owning program covers IPv4 listeners only and depends on what Windows reports, so the callout may say just "Another app is using it." And when Windows refuses the port with an access error (Winsock error 10013) and Transputty finds no program listening on it, the 0.1 callout is titled "Port N needs administrator rights" (code BIND_PERMISSION). That title is misleading on Windows, which does not require administrator rights for any port, including those below 1024. Error 10013 usually means the port is inside one of the reserved ranges above, or another program holds it for exclusive use. Running as administrator does not help; use the callout's Use :… button or choose a port outside the reserved ranges.

Transputty also tries to avoid the problem up front: when you start a connection from a preset whose usual port is taken, the editor picks the next free port and says, for example, "5432 is in use by postgres.exe (PID 4121), so Transputty picked 5433." If the application is still refused after the port is sorted out, continue with SSH tunnel connection refused; for the basics of the command, see the local port forwarding tutorial or your first SSH tunnel on Windows.

Common questions

How do I find what is using a port on Windows?

Run netstat -ano | findstr :5432 and read the process ID in the last column of the LISTENING line, then run tasklist /FI "PID eq 4121" with that number to see the program name. In PowerShell, Get-NetTCPConnection -LocalPort 5432 -State Listen shows the same owning process ID.

Why does ssh stay connected when the port forward failed?

By default ssh treats a failed forward as a warning. It prints the bind error, logs in and keeps the session open without the forward, so the tunnel looks alive while nothing listens for your application. Add -o ExitOnForwardFailure=yes so that ssh exits with an error when it cannot set up a requested forward.

Why does Windows ssh say Permission denied instead of Address already in use?

Windows' built-in OpenSSH client asks to share the address when it binds. When another program already holds the port, Windows answers that request with an access error, which ssh prints as bind: Permission denied. The meaning is the same as Address already in use on Linux and macOS: pick another port or stop the other program.

The port is refused but netstat shows nothing listening. Why?

On Windows, Hyper-V and WSL 2 reserve ranges of TCP ports that no program may bind, and reserved ports do not appear in netstat. Run netsh interface ipv4 show excludedportrange protocol=tcp to list them. If your port falls inside a range, choose a port outside the listed ranges.

Can I use the same local port for two SSH tunnels?

No. A listening port on an address belongs to one tunnel at a time. Give each tunnel its own local port, for example 5433 and 5434; the remote ports can be identical. Be careful on Windows, where a second ssh.exe may bind the same port without an error and your application reaches only one of them.

How this tutorial was checked

2026-11-26: The Windows behaviour was reproduced on Windows 11 as a standard, non-elevated user against a real OpenSSH server on the same PC. With Windows' built-in ssh (OpenSSH_for_Windows 9.5p2): the 'Permission denied' bind message and its two follow-up lines when PostgreSQL 18 or a test listener held the port; ssh staying connected without the forward, and exiting with status 255 under ExitOnForwardFailure=yes; the half bind where only ::1 was free; no error at all when another program held 0.0.0.0 on the port without exclusive use; two ssh.exe processes both listening on one port; and a forward on port 81 without elevation. With the ssh from Git for Windows (OpenSSH 10.2p1) the same test printed 'Address already in use'. The netstat, tasklist, Get-NetTCPConnection, Get-Process and netsh commands were run. Not reproduced: a Hyper-V or WSL reserved range blocking a port (the PC had neither), the lsof and ss commands, any Linux or macOS behaviour, PuTTY and Docker port publishing. The Transputty text is quoted from the 0.1 source code and was not observed in the app.

Sources and further reading

Transputty keeps SSH tunnels running on Windows

Local, remote and SOCKS5 forwards in one window, automatic reconnection with ports that stay bound, and a clear reason when something fails. In development; no public build yet.