Locked out of SSH after a firewall change?
You enabled a firewall, moved the SSH port or tightened
sshd_config, and the next ssh hangs or is refused.
The device is still running, and it is not next to you. Which way back in
exists depends on what you broke, so start by working that out.
Work out what you cut
- Timeout. The address is right and nothing answers. Usually a firewall rule, a cloud security group, or an address that changed. The SSH server may be running fine.
-
Connection refused. The host answers and nothing listens on
that port. sshd is stopped, failed to start on a bad config, or listens on a
different
PortorListenAddressthan the one you are trying. -
Permission denied (publickey). sshd is fine and refuses you
on purpose:
PasswordAuthentication nobefore a key was in place, anAllowUsersorDenyUsersline, a changedPermitRootLogin, or a wrong mode on the home directory or~/.ssh. If nothing changed and ssh simply refuses a key, the publickey how-to covers the everyday case.
nc -vz host 22 from your laptop separates the first two:
"refused" answers at once, a timeout does not. If you did not just change a
firewall or sshd, the
timed out and refused how-to
is the everyday version of the first two.
Ways back in
A console
A keyboard and screen on the device, a hypervisor or cloud provider's serial or VNC console, or a rescue mode. It fixes all three cases because it does not use the network path you broke. It is also the one thing you cannot count on for a device in someone else's building.
Someone on site
They can reboot it, which undoes any firewall rule that was never saved. It
does not undo one that was, or a bad sshd_config, and it costs a
person's time.
Mount the disk elsewhere
Boot a live system or move the disk or SD card to another machine, and edit the file. It works for every case and takes the device down while you do it.
A second path that does not use the one you changed
Something already set up that reaches the device another way: a management network, a second interface, a VPN, a reverse tunnel, or a hosted relay. It has to exist before the change. Each one is also a thing to keep running; the comparison goes through what they ask of you.
Make the next change safe
These habits cost a minute and need nothing installed.
- Keep your current session open and test the change from a second one. An established SSH connection usually survives a firewall change and an sshd reload, so you still have a way to undo it.
-
Check the config before reloading.
sudo sshd -tprints nothing when the file is valid. Then reload, rather than restart. The service issshon Debian and Ubuntu andsshdon most other systems:sudo sshd -t && sudo systemctl reload ssh. -
Set a timer that undoes a firewall change before you make
it, on a systemd machine:
sudo systemd-run --on-active=5min ufw disable. It prints the timer's name. If the change worked, stop that timer; if it locked you out, the firewall switches off after five minutes. Use the matching command for your own firewall. - Do not move the SSH port and tighten the firewall in one step. Change one thing, prove it from a second session, then the next.
RescueShell as the second path
An agent on the device keeps one outbound HTTPS connection to the relay. When
you open a shell, the agent connects to the device's own SSH server at
127.0.0.1 port 22 and your session runs there. That decides which
lockouts it survives, so read the next two lists before you rely on it.
It still works when you cut:
- Inbound SSH with a firewall rule or a security group, because the agent's connection goes out and the session runs over the loopback address.
- An allow-list that no longer includes your address, or a device whose public address changed.
It needs the firewall to leave two things alone: loopback traffic, and outbound HTTPS to the relay. A default-deny rule on outgoing traffic cuts the agent too.
It does not help when:
- sshd is stopped, or failed to start on a bad configuration.
-
sshd no longer listens on
127.0.0.1:22: a changedPort, or aListenAddressthat leaves out the loopback address. -
sshd now refuses the login. The session still goes through the device's own
sshd and its rules, so public-key login being off, or an account being left
out by
AllowUsers, still refuses it. - The device is off or has lost its network.
So it covers the most common self-inflicted lockout, a firewall or a security group, and not a broken sshd. It has to be installed first. A device you are already locked out of is a job for the console.
1. Check the device
uname -m
openssl version
command -v curl
sudo sshd -t
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. OpenSSH has to be
installed and running, and 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, while you still have access:
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.
The agent starts at boot under systemd, OpenRC, procd on OpenWrt or an init
script, whichever the device uses. Check it with
rescueshell-agent --status. The installer adds a drop-in file for
sshd and does not edit your main sshd_config.
3. Prove it before you change anything
When the device shows as connected under Devices, open a shell from the browser, or from your terminal with the CLI, with your usual SSH session still open. Only then make the firewall or sshd change. After it, open a shell the same way and check that it still works.
The device needs outbound HTTPS and nothing inbound. When the link drops, the agent retries every 60 seconds, five times, then waits 30 minutes and starts over, so the device comes back by itself, whatever address it got.
What it does not do
- It cannot rescue a device you cannot reach today. Install it before the change, not after.
- It is a shell on the device, not a console. It does not see boot, a stopped sshd or a kernel panic, and a shell needs a working OpenSSH.
- One open shell per device at a time. Port forwarding through a session is off, so it does not give you a route to another machine on the device's network.
- The relay is a dependency. If it is unreachable you cannot open a shell.
- A network where web traffic only leaves through an HTTP proxy is not supported. 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.