SSH into an OpenWrt router behind CGNAT
A router on a 4G modem, a satellite link or a customer's network often has no public address, so nothing can connect in. The router has to dial out.
Why port forwarding and DDNS fail
Port forwarding opens a port on the router's public address. Behind CGNAT the router has none: the address on its WAN side is private or shared with other customers, and the carrier's equipment sits in front of it. A forward on the router is never reached, because the connection stops at the carrier.
Dynamic DNS does not help for the same reason. It keeps a name pointed at an
address, and the address it would publish is not one anyone can reach. To
check, compare the WAN address on the router's status page with your public IP
from any "what is my IP" site. If they differ, or the WAN address is between
100.64.0.0 and 100.127.255.255, see
SSH behind CGNAT.
Some links also give a public IPv6 address. That can work if the router's firewall allows it and the machine you connect from has IPv6 too. Many do not, so this page assumes the router has to dial out.
Three ways to dial out from a router
Reverse SSH to your own server
The router runs ssh -R to a server you rent, and you log in to
that server, then to the port that leads back. autossh is in the
OpenWrt package feed and redials when the link drops. Run it from a procd
service so it starts at boot. A sketch, which we have not run on a router:
#!/bin/sh /etc/rc.common
START=95
USE_PROCD=1
start_service() {
procd_open_instance
procd_set_param command /usr/sbin/autossh -M 0 -N \
-i /etc/tunnel_key -R 22022:localhost:22 tunnel@your-server.example
procd_set_param respawn
procd_close_instance
}
- You run and patch the server, and one port on it stands for each router.
-
OpenWrt's own
sshis normally Dropbear's client, which takes different options and key formats from OpenSSH. Check which clientautosshwill call on your build. - The router needs a key that can log in to your server. Anyone who gets that key can try it there.
A VPN mesh package
WireGuard and Tailscale both have OpenWrt packages. A mesh joins the router to a network of your other machines, so you get more than a shell: any port, a web interface, device to device traffic.
-
It needs a client, and on most builds a kernel module or a userspace mode,
on a device that may have little flash and RAM. Look at
df -h /overlayandfreebefore you start. - The router joins your network and stays part of it. On a customer's router that is a bigger thing to agree to than one outbound connection.
A hosted relay
An agent on the router keeps one outbound HTTPS connection to the relay, and you open a shell from the browser or a CLI. You run no server. The relay is a dependency: if it is unreachable, you cannot open a shell.
The three compared goes through where each one wins. For one router and a server you already run, a reverse tunnel is fine. For a network, not a shell, use a mesh.
RescueShell on OpenWrt
Four things decide if this fits your router. This is what the product does today.
1. Check the CPU
uname -m
cat /etc/openwrt_release
The installer builds exist for amd64, arm64, armv7 and armv6, picked from
uname -m. A router that reports mips or
mipsel, which covers a lot of older boards, is not supported: the
installer stops with unsupported CPU and installs nothing. Check
first.
2. OpenSSH, not Dropbear
A shell is a session on the router's own OpenSSH server. Dropbear is not supported, and there is no password fallback. The installer never installs packages itself. It installs the agent either way and prints what to run, so you can also run it first and read the output. The commands, including moving Dropbear off port 22, are in install on a host without OpenSSH. In short:
uci set dropbear.@dropbear[0].Port='2222'
uci commit dropbear
/etc/init.d/dropbear restart
opkg update
opkg install openssh-server openssh-keygen curl ca-bundle openssl-util
/etc/init.d/sshd enable
/etc/init.d/sshd start
Do that from the serial console or a second session: restarting Dropbear can
drop the one you are in. OpenSSH takes more flash than Dropbear. On x86_64 we
put it at a few MB, and other architectures differ, so check
df -h /overlay first. A router with very little flash may not fit
it. We have no figures for the agent itself on a router.
3. Install
Create a token in the dashboard, then run this on the router as root:
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
Two changes for OpenWrt:
-
Add
--state-dir /etc/rescueshell. OpenWrt keeps/varin memory, and the installer refuses to keep the device's credential where a reboot would erase it. -
piis not a user on a router, and--trust-keys-fromonly reads that user's~/.ssh/authorized_keys, not the key file Dropbear uses. Use--trust-keys-from rootif root has keys in that file. Otherwise use--trust-key 'ssh-ed25519 AAAA… you@laptop' --allow-user root, or copy the Add device command from the web app, which fills in the keys you registered. See add a device.
The installer lists each key it will trust and asks you to confirm. Our servers cannot add a key to that list.
4. What it sets up
- On OpenWrt the installer sets the agent up as a procd service. It starts on boot, and procd starts it again if it stops.
- The restart comes after 30 seconds. If it dies five times in quick succession within an hour, procd gives up.
- If the relay refuses the device's credential, the agent stops instead of retrying, and nothing is deleted. See the credential was rejected.
-
/etc/init.d/rescueshell-agent runningshows if it runs.stopstops it for now anddisablekeeps it from starting at boot. -
One sshd drop-in in
/etc/ssh/sshd_config.d/. Your mainsshd_configis not edited. - A router you set up before this start-up existed moves to it when you run the install command again.
When the link drops, the agent retries every 60 seconds, five times, then waits 30 minutes and starts over. It needs outbound HTTPS and nothing inbound. On a mobile link, see SSH over 4G or LTE for what that costs in data.
What we tested, and what it does not do
- We checked the package names, the init script and the configuration in the OpenWrt 24.10 package feed. We ran the installer and the boot service on an OpenWrt 23.05 image in a container, not on a router. OpenWrt is not in our device matrix.
- It is a shell on the router, with one open shell per device at a time. Port forwarding through a session is off, so it does not reach the router's web interface or the network behind it. For that, use a mesh.
- The agent cannot use a network where web traffic only leaves through an HTTP proxy.
If the shell does not open, see the shell does not open.
Try it on one router
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.