Reverse SSH Tunneling: Directly Access Internal Servers Without Bastion Hosts
This guide explains how to set up a reverse SSH tunnel through a public relay server to securely access internal network terminals directly from external machines, eliminating repeated bastion host logins while maintaining security through loopback binding and key-based authentication.
Reading font
Three Roles
The setup involves three roles:
A (Target Terminal) : Internal server to be accessed (e.g., user a_admin)
R (Public Relay) : Server with a fixed public IP (e.g., 203.0.113.10) running OpenSSH
B (Ops Computer) : Administrator's workstation
Example addresses (replace with actual values):
R public IP: 203.0.113.10 R tunnel account: tunnel_a R operator account: operator_b R loopback port reserved for A: 22022 (bound to 127.0.0.1:22022, only reachable from R itself)
A login account:
a_adminPrinciple: Reverse SSH Tunnel
Production firewalls typically allow outbound connections from internal hosts and their return traffic, but block unsolicited inbound connections. Reverse SSH uses this rule:
A initiates SSH to R
A requests R to open a listening port on R's loopback interface ( 127.0.0.1:22022)
B authenticates to R, then connects to 127.0.0.1:22022 Traffic is forwarded through A's established SSH connection to A's port 22 Thus B never directly connects to A's internal IP, and R needs no route to A's internal network.
Network and Permission Prerequisites
A can reach 203.0.113.10:22 outbound; if policy forbids SSH egress, request a formal rule — do not bypass by changing ports
A's SSH service runs normally and allows only authorized accounts
B can reach R's SSH service
R has a fixed public IP, patched OS, time sync, and log collection
A, B, R each use distinct SSH keys and accounts; never share root, passwords, or copy the same private key
If A cannot run ssh and no existing proxy/gateway exists, no command on B alone can reach A — a channel must be initiated from A's side.
Configure Relay R
3.1 Create Two Separate Accounts
On R (OpenSSH, service may be ssh or sshd):
sudo sshd -t
sudo systemctl status ssh || sudo systemctl status sshdCreate accounts:
sudo useradd --create-home --shell /bin/bash tunnel_a
sudo useradd --create-home --shell /bin/bash operator_bPlace A's public key in /home/tunnel_a/.ssh/authorized_keys; place B's public key in /home/operator_b/.ssh/authorized_keys. Private keys must stay on A and B respectively — never upload to R.
3.2 Restrict Tunnel Account to Reverse Forwarding Only
Append to /etc/ssh/sshd_config on R (port 22022 is an example; change consistently on A and B):
# Keep reverse forwarding listening only on relay localhost, not exposed to internet
GatewayPorts no
Match User tunnel_a
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey
AllowTcpForwarding remote
PermitTTY no
X11Forwarding no
AllowAgentForwarding no
PermitListen 127.0.0.1:22022Validate and reload:
sudo sshd -t && sudo systemctl reload ssh
# or if service name is sshd:
sudo sshd -t && sudo systemctl reload sshdPermitListenlocks tunnel_a to 127.0.0.1:22022. Older OpenSSH versions lacking this directive require upgrade or equivalent access control.
Firewall/security group on R only needs:
TCP 22 from A's egress CIDR and B's egress CIDR (for tunnel creation and jump login)
TCP 22022 not opened to public — it binds only to
127.0.0.1Establish Reverse Tunnel on A
First, generate a dedicated tunnel key on A (via existing bastion if needed):
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_tunnel_a -C "a-to-relay"Add ~/.ssh/id_ed25519_tunnel_a.pub to R's tunnel_a authorized_keys. Verify R's host key fingerprint on first connect.
Test tunnel manually:
ssh -N -T \
-i ~/.ssh/id_ed25519_tunnel_a \
-o ExitOnForwardFailure=yes \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-R 127.0.0.1:22022:127.0.0.1:22 \
[email protected]Parameter meanings: -N: no remote command, only forwarding -T: disable pseudo-terminal allocation ExitOnForwardFailure=yes: fail immediately if remote port cannot be opened, avoiding silent failures ServerAliveInterval=30, ServerAliveCountMax=3: keepalive probes to detect dead links -R 127.0.0.1:22022:127.0.0.1:22: on R's loopback port 22022 forward to A's localhost:22
Keep terminal open. On R verify listener: sudo ss -lntp | grep 22022 Expected: 127.0.0.1:22022 only, not 0.0.0.0:22022.
For persistence, create systemd service /etc/systemd/system/a-to-relay.service:
[Unit]
Description=Reverse SSH tunnel from A to relay
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=a_admin
ExecStart=/usr/bin/ssh -N -T -i /home/a_admin/.ssh/id_ed25519_tunnel_a -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -R 127.0.0.1:22022:127.0.0.1:22 [email protected]
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.targetAdjust User=, key path, and ssh binary path to actual values. Enable:
sudo systemctl daemon-reload
sudo systemctl enable --now a-to-relay.service
sudo systemctl status a-to-relay.serviceConfigure B and Connect to A
On B, add to ~/.ssh/config:
Host relay-prod
HostName 203.0.113.10
User operator_b
IdentityFile ~/.ssh/id_ed25519_operator_b
IdentitiesOnly yes
Host a-via-relay
HostName 127.0.0.1
Port 22022
User a_admin
ProxyJump relay-prod
IdentityFile ~/.ssh/id_ed25519_a_admin
IdentitiesOnly yes
HostKeyAlias a-internal-targetThen simply run: ssh a-via-relay What happens: B authenticates to R as operator_b, R connects to its own 127.0.0.1:22022, traffic traverses the reverse tunnel to A, where A performs final authentication as a_admin. B passes two independent identity checks (R and A).
Verification and Troubleshooting
Check commands:
# A: view tunnel service logs
sudo journalctl -u a-to-relay.service -n 100 --no-pager
# R: confirm port binds only to loopback
sudo ss -lntp | grep 22022
# B: verbose SSH to see connection path and failure points
ssh -vvv a-via-relaySecurity Note
Exposing the tunnel port publicly (e.g., binding 0.0.0.0:22022 on R) would allow direct ssh -p 22022 [email protected], saving one step but placing A's SSH endpoint on the public internet — increasing scan, brute-force, and misconfiguration risk. The loopback-only design avoids this.
Reverse SSH tunneling does not "punch through" the firewall from outside; it relies on A actively establishing an authenticated outbound connection. The single command ssh a-via-relay hides a chain: R jump authentication → R loopback port → reverse tunnel → A final authentication.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.