rescueshell Start trial

SSH over WireGuard to a device behind CGNAT

WireGuard is a good way to reach a device that has no public address, but only if one end of it does. Behind CGNAT neither your laptop nor the device does, so you need a third machine with a public IP. Here is the setup, what it takes on, and the shorter path if all you need is a shell.

Why a direct WireGuard link does not work

A WireGuard peer needs to know where to send packets: an Endpoint, which is an address and a UDP port. A device behind CGNAT has no address you can reach, and neither does a laptop on a home or mobile connection. The two cannot find each other. To check whether you are behind CGNAT, see SSH behind CGNAT or use the CGNAT checker.

What works is a hub: a small server with a public IP address that both sides connect out to. The device dials the hub, so the CGNAT sees outbound traffic and lets the replies back. Your laptop dials the hub too. Once both are peers of the hub, they can reach each other through it, by their addresses on the VPN.

The hub setup

This is the standard layout. We have not run this exact configuration for this page, so check it on a test machine first. Generate a key pair on each of the three machines:

umask 077
wg genkey | tee privatekey | wg pubkey > publickey

Keep privatekey where it is. Copy only the public keys between machines. On the hub, in /etc/wireguard/wg0.conf:

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = HUB_PRIVATE_KEY

[Peer]
# the device
PublicKey = DEVICE_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32

[Peer]
# your laptop
PublicKey = LAPTOP_PUBLIC_KEY
AllowedIPs = 10.8.0.3/32

On the device, in the same file name. The keepalive is the important line: a CGNAT or any NAT forgets a quiet UDP flow, and without it the hub can no longer send to the device once the entry has gone.

[Interface]
Address = 10.8.0.2/24
PrivateKey = DEVICE_PRIVATE_KEY

[Peer]
PublicKey = HUB_PUBLIC_KEY
Endpoint = hub.example.com:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25

Your laptop's file is the same with Address = 10.8.0.3/24 and its own private key. Then:

WireGuard is in the Linux kernel from 5.6. On an older kernel, or a router image without the module, you need the module package or a userspace build, and a tun device.

What you take on

What you get for it

Everything a network gives you, not only a shell. Any port and any protocol: a web interface on the device, a database, file copies, devices that talk to each other. It uses your own software end to end, there is no vendor in the path, and for a handful of machines you already understand every part. If you need to reach a network, not a device, this is the right tool.

If all you need is a shell

Then a VPN is more than the job needs. A hosted relay does less: the device keeps one outbound HTTPS connection to the relay, so there is no hub, no UDP and no address plan. Your shell is joined to it there. The three-way choice, including a reverse SSH tunnel, is in reverse SSH vs a VPN mesh vs a hosted relay.

RescueShell steps

The agent on the device connects out to our relay. You pick the device by name, in the browser or the CLI, and the session goes to the device's own OpenSSH server, which checks an SSH key from the device's own list of trusted keys. Our servers cannot add a key to that list.

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. See add a device for the other options.

3. Open a shell

When the device shows as connected under Devices, open a shell from the browser, or run rescueshell in your terminal and pick the device and an account. See the CLI. The CLI uses your own SSH key and checks the device's host key for you.

The two can run side by side. The agent adds no route and opens no port, so it should not get in the way of a tunnel, and the relay then is a second way in when the VPN or its hub is down. We have not tested it next to WireGuard specifically.

What it does not do

How to choose

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.