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:
-
On the hub, allow inbound UDP 51820 in its firewall, and turn on forwarding
so the hub passes traffic between its peers:
sysctl -w net.ipv4.ip_forward=1, set permanently in/etc/sysctl.d/. If the hub's firewall drops forwarded packets by default, allow them onwg0. -
On each machine, start it with
wg-quick up wg0, and make it start at boot withsystemctl enable --now wg-quick@wg0on a systemd machine. -
Check the link with
wg show, which lists the last handshake per peer, thenssh pi@10.8.0.2from the laptop.
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
- The hub: a server with a public IP to rent, patch and watch. If it is down, every device behind it is out of reach.
- Keys and addresses: each new device is a new peer on the hub with its own address, and a record of which is which. Every machine that can reach a device is another peer.
- The device is on a network. With the config above everything on the VPN can try to reach everything else, including the device's other services, so you write the firewall rules that limit it.
- UDP. Some networks block outbound UDP, and then the device cannot reach the hub at all. A WireGuard tunnel does not fall back to TCP or port 443.
- The customer's agreement. A device that joins a VPN is a bigger ask of the network's owner than a single outbound web connection. See what to ask a customer's IT.
- Your own SSH setup still applies: keys, accounts and sshd settings on the device.
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
- 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 network: no web interface on the device, no other machines on its LAN. A VPN does that.
-
It is not a plain
sshcommand with an address. You use the web app or the CLI. - 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.
- OpenSSH only, on Linux.
- The device needs outbound TCP 443 and WebSocket allowed. A network where web traffic only leaves through an HTTP proxy does not work.
How to choose
- You need ports and protocols, or a network, and you will run a server: your own WireGuard hub.
- You need only a shell, on devices in other people's networks, with no server to keep running: a hosted relay.
- Both: keep the VPN for traffic, and a relay as the way in when it is down.
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.