SSH NAT traversal: when hole punching fails
NAT traversal is any trick for reaching a machine that has no address you can connect to. Some tricks need both ends to cooperate. When you only control one end, the dependable one is to have the device dial out.
Why a connection cannot just get in
A NAT router rewrites outgoing traffic and remembers what it sent. A packet
from outside is let in only if it matches something the inside sent first.
Nothing matches an unsolicited ssh to the device, so it is
dropped. Behind carrier-grade NAT there is a second router you do not control
between you and the first, so a port forward on yours is never reached. To
check, see SSH behind CGNAT.
The usual approaches, and where each fails
Port forwarding
A rule on the router sends one port to the device. It needs a router you can configure and a public address on its WAN side. It is the simple answer when both hold, and no answer when the router is someone else's or the address is shared. See SSH without port forwarding.
UDP hole punching with STUN
Both peers ask a STUN server what public address and port their NAT gave them, swap that through a third party, and send packets at each other at the same moment. Each NAT then sees what looks like a reply and lets the other side in. It works only if each NAT maps a device to the same outside port whoever it talks to. A NAT that picks a new port per destination, often called symmetric, breaks it, and two such NATs usually cannot be punched at all. Carrier-grade NAT can behave this way. A firewall that drops outbound UDP breaks it too.
Plain SSH runs over TCP and does nothing of this itself, so punching needs software at both ends that sets up the path and carries SSH over it, such as a mesh VPN client on both machines.
A relay when punching fails
Tools that punch holes usually keep a relay as the fallback, and carry the traffic through it when no direct path can be made. That is the part that makes them work on hard NATs. It also means that on the networks where you needed traversal, the path you get is a relay's. The comparison has the mesh version of this.
A reverse SSH tunnel
The device runs ssh -R to a server with a public address, and you
connect to that server. No punching is needed, since the device makes an
ordinary outbound TCP connection. You run the server and keep the tunnel
alive; see
why autossh tunnels drop.
What they have in common
Everything that works on a hard NAT ends with the device making the first connection to something reachable, and traffic then passing through it. The difference between the options is who runs that something, and what else comes with it. The question to ask is whether you need a direct path at all. For a shell on a device in the field, you usually do not.
RescueShell: no traversal, a relay in the middle
There is no hole punching and no STUN. An agent on the device holds one outbound HTTPS connection to our relay, and your shell is joined to it there. Both sides connect out, so the NAT type does not matter.
1. Check the device
A shell is a session on the device's own OpenSSH server, so it needs one
running, with your key in an account's ~/.ssh/authorized_keys.
The installer copies the trusted keys from there. Check the rest:
uname -m
openssl version
command -v curl
It needs Linux on amd64, arm64, armv7 or armv6, and curl or wget. Any other
CPU stops with unsupported CPU. The release signature is checked
with OpenSSL 3; without it, pass --sha256. Dropbear is not
supported. For a device with no OpenSSH, see
install without OpenSSH.
2. Install
In the web app, open Devices, press Add device, name it and press Create pass. Then run this on the device as root, over SSH or at its console:
curl -fsSL https://releases.rescueshell.com/latest/install.sh | sh -s -- \
--base-url https://releases.rescueshell.com/latest --relay https://app.rescueshell.com \
--name pump-07 --trust-keys-from pi
Change pi to the account whose keys you log in with, and
pump-07 to a name for the device. The installer lists each key it
will trust by fingerprint and asks you to confirm, then asks
Registration pass or token:. Paste the pass; nothing shows as you
do. It ends with a host key: line. Keep it, because the browser
asks for it the first time you open a shell. Our servers cannot add a key to
the device's list. See add a device for the
other options.
The agent starts at boot, and rescueshell-agent --status shows
its state. If the link drops, it retries every 60 seconds, five times, then
waits 30 minutes and starts over.
3. Open a shell
When the device shows as connected under Devices, open a shell from the browser, or from your terminal with the CLI. Try it from a different network than the device's, such as your phone's hotspot.
What it does not do
- It is a shell on one device, one open shell at a time. Port forwarding through a session is off, so it is not a route to the device's network or to a web page on it. A mesh is the tool for that.
- Traffic always goes through the relay. It is a dependency: if it is unreachable you cannot open a shell, and we offer no uptime SLA. A direct peer-to-peer path, where one can be made, has no such middle.
- OpenSSH only.
- The device needs outbound TCP 443 and WebSocket allowed. A network where web traffic only leaves through an HTTP proxy does not work. See what to ask the IT team.
If the shell does not open, see the shell does not open.
Try it on one device
The first device is free to try with no card, for 30 days from when it connects, not from when you sign up. Start a trial.