DEV Community

matt
matt

Posted on

SSH -L: Connection Refused vs Administratively Prohibited

If ssh -L returns connection refused or administratively prohibited, the message is not generic. It identifies which layer failed: the local listener on your machine, the SSH server's forwarding policy, or the server's connection to the final destination.

This tutorial shows how to reproduce the failure in the foreground, classify the exact SSH channel message, and run the right checks in the right order. The key detail is that a -L tunnel can appear to start successfully and fail only when your database client connects.

Table of Contents

Reproduce the Failure and Classify the Message

Run the tunnel in the foreground with maximum client debug output:

ssh -vvv -N -L 15432:db.internal:5432 bastion.example.com
Enter fullscreen mode Exit fullscreen mode

-N tells SSH not to run a remote command, keeping the session purely a tunnel. Each -v increases verbosity, up to three. The OpenSSH ssh(1) manual documents -L, -N, -v, -G, and -J, including the crucial detail that -L allocates a local listening socket and makes the destination connection from the remote machine.

Keep that session running and trigger the tunnel from a second terminal:

psql -h 127.0.0.1 -p 15432 -U app appdb
Enter fullscreen mode Exit fullscreen mode

Substitute your own client if you are not using PostgreSQL, but keep the host as 127.0.0.1 and the port as the local side of -L.

Why trigger it separately? -L sets up the local listener first, then opens the remote channel only when a local connection arrives. That is why the tunnel can look healthy at startup and fail when the database client connects. For a working baseline and the same remote-side mental model, this walkthrough of SSH local port forwarding with -L shows the successful command and explains why the destination is reached from the SSH server.

Classify the failure from the phrase after open failed: or from the client's bind message:

Message Failing layer First check
bind [127.0.0.1]:15432: Address already in use Local client Local listener and process
channel 0: open failed: administratively prohibited: open failed SSH server policy Effective sshd configuration and key restrictions
channel 0: open failed: connect failed: Connection refused Destination reachability DNS and TCP probe from the SSH server
ssh: connect to host bastion port 22: Connection refused SSH transport sshd on the bastion, port, firewall

Channel numbers and surrounding text vary between OpenSSH versions, so classify by the phrase rather than the channel number. RFC 4254 defines reason code 1 as SSH_OPEN_ADMINISTRATIVELY_PROHIBITED and code 2 as SSH_OPEN_CONNECT_FAILED, which is why the same -L command can produce two very different failures.

Administratively Prohibited: A Server-Policy Error

An administratively prohibited channel failure means your SSH connection and authentication succeeded, but the server refused to open the forwarding channel. For a -L tunnel, the client is requesting a direct-tcpip channel, and the server's forwarding policy said no.

Start with the effective configuration rather than reading the file from top to bottom. On the bastion:

sudo sshd -T -C user=deploy,addr=203.0.113.10,host=bastion-client.example |
  grep -Ei '^(allowtcpforwarding|permitopen|disableforwarding)[[:space:]]'
Enter fullscreen mode Exit fullscreen mode

sshd -T prints the effective configuration, and -C applies the Match rules for the simulated connection. The sshd(8) manual documents -T and -C, along with -t syntax checking and the authorized_keys options restrict, no-port-forwarding, and permitopen=. Replace the -C values with the username and source address of the failing session.

Check the three forwarding directives in order:

  1. DisableForwarding yes wins over everything. The sshd_config(5) manual says it overrides all other forwarding-related options, so an AllowTcpForwarding yes elsewhere will not help.
  2. AllowTcpForwarding must permit local forwarding. no blocks all TCP forwarding, remote allows only -R, and local allows -L while blocking -R. For a -L tunnel, use yes, all, or local.
  3. PermitOpen is a destination allowlist. By default all destinations are permitted. PermitOpen none blocks everything, while PermitOpen db.internal:5432 permits only that literal destination and port.

PermitOpen compares the destination host and port supplied by the client. It does not resolve an alias to an address before matching, so PermitOpen db.internal:5432 will not match a client request for 127.0.0.1:5432, even if both names point to the same machine.

Configuration may also be split across sshd_config.d/*.conf files through Include, so reading one file can miss the effective value. Because sshd generally uses the first obtained value for a keyword, a later Match block may not override an earlier global setting. sshd -T -C is the authoritative check.

Check Key-Level Restrictions

The account's authorized_keys file can override an otherwise permissive server configuration:

sudo grep -nE 'restrict|no-port-forwarding|permitopen=' ~deploy/.ssh/authorized_keys
Enter fullscreen mode Exit fullscreen mode

restrict disables port forwarding along with other features, and no-port-forwarding blocks TCP forwarding entirely. Either can produce administratively prohibited even when sshd_config allows tunnels. The sshd(8) manual also documents permitopen=, which adds a per-key destination allowlist and performs no pattern matching or name lookup on the host.

If the key needs forwarding, add the port-forwarding option alongside restrict only when policy permits. Otherwise, use a separate key without restrict for the tunnel.

A ForceCommand alone is not a tunnel block. The server manual explicitly says ForceCommand does not limit TCP forwarding unless forwarding is also disabled. If tunnels must be blocked, use DisableForwarding yes or the specific forwarding directives.

Apply a Narrow Policy

A targeted Match block is safer than opening every destination:

Match User deploy Address 203.0.113.0/24
    AllowTcpForwarding local
    PermitOpen db.internal:5432
Enter fullscreen mode Exit fullscreen mode

Keep DisableForwarding out of the block unless you need to turn it off explicitly. If a global DisableForwarding yes appears earlier, a later no may not take effect because of first-value handling.

Validate the configuration and reload the SSH service:

sudo sshd -t
sudo systemctl reload ssh
Enter fullscreen mode Exit fullscreen mode

Use sshd as the service name on RHEL-family systems. Open a new SSH session after reload, because existing sessions may retain the old policy.

AllowTcpForwarding no is not a substitute for shell access control: the manual notes that users with shell access can install their own forwarders. Use PermitOpen to limit destinations, and DisableForwarding for accounts that need no forwarding at all.

Connect Failed and Connection Refused: Destination Errors

A connect failed message means the SSH server accepted the channel request and then failed to connect to the final destination. The local listener is working; the remote side is not.

First, separate the two Connection refused messages:

  • ssh: connect to host bastion port 22: Connection refused means your client could not reach sshd. Check the SSH listener on the bastion, the configured port, and network or firewall rules. This is not a channel error.
  • channel 2: open failed: connect failed: Connection refused means your client reached sshd and asked it to connect to db.internal:5432; the destination refused.

The connect(2) manual reports ECONNREFUSED when a stream socket "found no one listening on the remote address." A firewall rule that rejects with a TCP reset can look the same; a rule that drops the packet usually produces a timeout instead.

Run the checks from the SSH server, not your workstation, because that is where the destination connection originates:

getent ahosts db.internal
nc -v -z -w 5 db.internal 5432
Enter fullscreen mode Exit fullscreen mode

getent ahosts shows how the bastion's resolver expands the name. If it returns nothing, fix DNS, /etc/hosts, or the search domain on the bastion, or use the private IP address in the -L destination.

nc -v -z -w 5 uses netcat's scan mode, verbose output, and a five-second timeout, as documented in the nc(1) manual. A refused result points to no listener or a rejecting firewall; a timeout points to a dropped packet, route, or security-group rule. connect failed: Name or service not known is a resolver failure on the SSH server.

Then check the listener on the database host:

sudo ss -ltnp 'sport = :5432'
Enter fullscreen mode Exit fullscreen mode

The ss(8) manual documents the -l, -t, -n, and -p flags used here. Pay attention to the bind address:

  • 127.0.0.1:5432 means only local clients on the database host can connect. If the bastion is a different host, the tunnel's remote side cannot reach it.
  • [::1]:5432 is the IPv6 loopback and is also local-only.
  • 0.0.0.0:5432 or [::]:5432 means the service listens on all interfaces; then check the host firewall, cloud security group, and routing from the bastion.
  • A Unix socket only is not reachable through a TCP -L forward.

Address family matters too: 127.0.0.1 does not reach a service listening only on ::1, and vice versa.

The 127.0.0.1 Destination Mistake

One common cause is using 127.0.0.1 as the destination when the database is not on the SSH server:

# Wrong when the database is on a separate host:
ssh -L 15432:127.0.0.1:5432 bastion.example.com

# Right when the database is reachable from the bastion by name or private IP:
ssh -L 15432:db.internal:5432 bastion.example.com
Enter fullscreen mode Exit fullscreen mode

In a -L command, the destination host is evaluated on the SSH server. If the database is on its own host and only listens on loopback, connect through the database host instead, provided you have SSH access to it:

ssh -J bastion.example.com -L 15432:127.0.0.1:5432 db.internal
Enter fullscreen mode Exit fullscreen mode

With -J, the final SSH connection terminates on db.internal, so 127.0.0.1 is now evaluated on the database host. If PermitOpen is enforced on that host, allow 127.0.0.1:5432 for this form.

If the database is reachable only by private IP, substitute that IP and confirm the bastion has a route to it.

Local Bind Errors: Address Already in Use

Local bind errors happen before any remote channel exists. The client cannot create the listening socket on your machine, so there is no point checking server policy yet.

Typical output looks like this:

bind [127.0.0.1]:15432: Address already in use
channel_setup_fwd_listener_tcpip: cannot listen to port: 15432
Could not request local forwarding.
Enter fullscreen mode Exit fullscreen mode

Find the process holding the local port:

ss -ltnp 'sport = :15432'
Enter fullscreen mode Exit fullscreen mode

If another process owns the port, stop it or move the local side of the tunnel:

ssh -vvv -N -L 127.0.0.1:15433:db.internal:5432 bastion.example.com
Enter fullscreen mode Exit fullscreen mode

Use an explicit 127.0.0.1: bind address. It keeps the listener on loopback and makes it obvious which address is in conflict. Binding to 0.0.0.0 exposes the tunnel to other machines, so do that only when it is intentional.

Cannot assign requested address is a different bind failure: the address you asked to bind is not available on the client. Use 127.0.0.1 or an address actually assigned to a local interface. Ports below 1024 also require elevated privilege, so choose an unprivileged local port unless you really need the low port.

Check for duplicate client configuration:

ssh -G bastion.example.com | grep -iE '^(localforward|exitonforwardfailure)'
Enter fullscreen mode Exit fullscreen mode

ssh -G prints the configuration after evaluating Host and Match blocks, so it reveals a second LocalForward on the same port from ~/.ssh/config or /etc/ssh/ssh_config.

Set ExitOnForwardFailure yes if you want the client to terminate when it cannot set up all requested forwardings, rather than continuing without a tunnel. The ssh_config(5) manual warns that this option does not apply to connections made over the forwarding, so it will not catch a destination-side Connection refused. Keep the foreground test for that.

After changing the local port or freeing it, verify the listener:

ss -ltnp 'sport = :15432'
nc -v -z 127.0.0.1 15432
Enter fullscreen mode Exit fullscreen mode

The first command should show your ssh process listening. The second should connect and may produce the remote channel error in the SSH debug output; that is expected if the destination problem is still unresolved. It proves the local side is working.

Verify the Fix and Make the Tunnel Durable

Once the foreground command stays up and your database client connects, move the settings into ~/.ssh/config:

Host db-tunnel
    HostName bastion.example.com
    User deploy
    LocalForward 127.0.0.1:15432 db.internal:5432
    ExitOnForwardFailure yes
Enter fullscreen mode Exit fullscreen mode

Then connect with:

ssh -N db-tunnel
Enter fullscreen mode Exit fullscreen mode

LocalForward uses the same [bind_address:]port host:hostport form as -L, and ssh_config(5) documents the client-side options.

On the server side, keep the policy narrow: AllowTcpForwarding local rather than yes when only -L is needed, and PermitOpen for the specific database host and port. This limits what a compromised account can reach through the bastion.

Finally, test the failure paths deliberately on a test system: stop the database listener and confirm you get connect failed: Connection refused; occupy the local port and confirm you get Address already in use; set PermitOpen none in a disposable test configuration and confirm you get administratively prohibited. Being able to predict the message is the fastest way to diagnose the next tunnel failure.

Conclusion

ssh -L failures are layer-specific. Address already in use is a local listener problem. administratively prohibited is the SSH server's forwarding policy. connect failed: Connection refused is the destination or the path to it. A top-level ssh: connect ... Connection refused is the SSH transport itself.

Reproduce the failure in the foreground with ssh -vvv -N -L, classify the phrase, and check only the layer that failed. On the server, use sshd -T -C to see the effective policy, including Match blocks, and inspect key restrictions before changing global settings. On the destination side, resolve and probe from the SSH server, then check the listener's bind address and firewall. A narrow PermitOpen plus AllowTcpForwarding local gives you working tunnels without opening every destination.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

Matt, the detail that reframes the whole post for me is the lazy channel open: the tunnel can look healthy and only fail when the database client connects, which is why the foreground ssh -vvv test matters — a backgrounded tunnel hides the layer that actually failed.

One place I had to slow down and re-read: you note sshd generally uses the first obtained value for a keyword, and PermitOpen is a destination allowlist. If a second database gets added later and someone appends a second PermitOpen line for it, does that line add to the allowlist, or does the first-value rule drop it? That decides whether your narrow Match block stays safe as the allowlist grows, or whether the new destination fails with 'administratively prohibited' even though the line is right there in the config.

Have you ever caught a tunnel failing that way — where sshd -T -C showed an effective policy that did not match what the config file seemed to say?