Getting back into a server after losing SSH access
A lost key, a typo in the SSH configuration or a firewall rule can all lock you out. Rescue mode gets you back in without touching your data.
Why can I not log in any more?
The error message points at the cause. Permission denied (publickey) means the server is reachable but does not accept your key: it is missing, the wrong one is offered, or file permissions are off. Connection refused means nothing listens on the port, so the SSH service is stopped or moved. A timeout usually means a firewall drops your packets. Run the client in verbose mode to see which keys it offers and where it stops.
ssh -v -i ~/.ssh/id_ed25519 admin@192.0.2.40 2>&1 | grep -E 'Offering|Authentications|denied|refused|timed out'
Is there a quick fix before using rescue mode?
Sometimes. Check that you use the right user, address and key file, and that your own IP has not changed if the firewall allows only certain addresses. If another person or a deployment tool still has working access, they can add your new public key in seconds. If none of that helps, rescue mode is the dependable route.
How do I add a new key from rescue mode?
Start rescue mode from the console, log in to the rescue system with the root password it shows, mount your root filesystem and append a new public key to the right authorized_keys file. Create the new key pair on your own computer first, as described in Create SSH keys.
lsblk -f mount /dev/md2 /mnt mkdir -p /mnt/root/.ssh cat >> /mnt/root/.ssh/authorized_keys <<'EOF' ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...replace-with-your-key recovery-2026 EOF chmod 700 /mnt/root/.ssh && chmod 600 /mnt/root/.ssh/authorized_keys
For a normal user, write to /mnt/home/USER/.ssh/ and fix the owner with the numeric IDs from the mounted system, because the rescue system has its own user list:
grep '^admin:' /mnt/etc/passwd | cut -d: -f3,4 chown -R 1000:1000 /mnt/home/admin/.ssh
What if the SSH configuration itself is broken?
Fix it in the mounted files and check the syntax with the server's own sshd through a chroot. Typical culprits are an AllowUsers line that excludes you, PermitRootLogin no when root is the only account, or a new port that the firewall blocks.
grep -rnE 'AllowUsers|PermitRootLogin|PasswordAuthentication|^Port' /mnt/etc/ssh/sshd_config /mnt/etc/ssh/sshd_config.d/ for d in dev proc sys; do mount --bind /$d /mnt/$d; done chroot /mnt sshd -t
No output from sshd -t means the configuration is valid.
And if a firewall rule locked me out?
Edit or move aside the saved rules so they do not load at the next boot. You can add the right rule properly once you are back in.
| Tool | Saved rules file | Quick rescue action |
|---|---|---|
| nftables | /mnt/etc/nftables.conf | Add an accept for port 22 near the top of the input chain |
| iptables-persistent | /mnt/etc/iptables/rules.v4 and rules.v6 | Rename the files so no rules load |
| ufw | /mnt/etc/ufw/ufw.conf | Set ENABLED=no |
| firewalld | /mnt/etc/firewalld/zones/ | Disable with chroot /mnt systemctl disable firewalld |
Do I need to care about SELinux?
On AlmaLinux, Rocky Linux and Fedora, yes. A new authorized_keys file written from rescue mode has no SELinux label, and sshd will refuse to read it. Either relabel the file from a chroot, or schedule a full relabel at the next boot.
chroot /mnt restorecon -Rv /root/.ssh # or touch /mnt/.autorelabel
How do I return to normal?
Unmount everything in reverse order, then choose Boot from hard disk in the console. The server restarts into your system, and your new key should work at once. Remove the lost key's line from authorized_keys on this and every other server, and remove it from the SSH keys list in your account so it is not offered at the next reinstall.
umount /mnt/dev /mnt/proc /mnt/sys umount /mnt
Is a reinstall ever the better option?
When the data on the server is disposable or fully backed up, yes: a console reinstall with your new key takes minutes and leaves a clean system. When the data matters, rescue mode keeps everything as it was. Either way, keep a second key on another device listed on each server from now on; it turns the next lost laptop into a non-event. The IPMI and rescue mode article explains why rescue mode is the out-of-band path on our machines.
Related pages
Create SSH keys
Generate an Ed25519 key pair on Windows, macOS or Linux, add it to your account before ordering, and manage keys on a running server.
Read moreChange the root password
Set a new root password on a running server, or reset a forgotten one from the rescue system, and decide whether root needs a password at all.
Read moreIPMI and rescue mode
What out-of-band management is, and how console power control, reinstall and rescue boot cover the same needs on our machines.
Read moreBuild an iptables firewall
A default-deny host firewall with iptables or nftables: allow SSH and web, rate-limit new connections, cover IPv6 and keep the rules across reboots.
Read moreQuestions and answers
Can support add a new SSH key for me?
You add it yourself from rescue mode in about ten minutes. The console starts the rescue system and shows its root password.
Will rescue mode erase my data?
No. Rescue mode boots a separate system from the network and leaves your drives untouched until you mount them.
Why is my new key still refused?
Check that the file is on one line, that .ssh is 700 and authorized_keys 600 with the right owner, and on SELinux systems that the file is relabelled.
Does changing SSH keys in my account update a running server?
No. Account keys are installed at delivery and reinstall. On a running server, add keys to authorized_keys yourself.
How do I avoid being locked out again?
Keep a second key on another device on every server, keep a session open while changing SSH or firewall settings, and arm an automatic undo for firewall changes.