TROUBLESHOOTING
July 27, 2026

How to Recover a Server You're Locked Out Of

5 min read
Author
CloudStick Team
Backend Developer
Share this article
How to Recover a Server You're Locked Out Of
CloudStick
Locked Out

Why This Happens

Almost every SSH lockout traces back to one of five causes, and none of them mean your server or its data is damaged — you just can't get in the front door anymore. The first is a lost or overwritten SSH key: a new laptop, a wiped ~/.ssh folder, or a key pair regenerated without updating authorized_keys on the server.

The second is password authentication being disabled while your key silently stops working — a permissions change on the key file, a wrong path in your SSH client config, or an agent that dropped the identity. The third is a firewall rule from CSF or UFW that blocks your own IP after a hardening pass, usually because a "deny all except these IPs" rule went in without your current address on the allow list. The fourth is fail2ban banning your own IP after a string of failed login attempts, often triggered by an old script or a typo'd password on a previous session. The fifth, and the scariest-looking, is an edited sshd_config with a syntax error that prevents the SSH daemon from restarting at all — the service dies, and there is no listener on port 22 for anything to connect to.

It helps to say plainly what this article is not about: none of these five causes touch your files, your databases, or your running websites. Nginx, PHP-FPM, and MySQL keep serving traffic to visitors exactly as before — the only thing broken is the door you use to administer the box. That distinction matters because it changes the urgency: you have time to work through the recovery steps methodically instead of panicking about data loss.

The Console Lifeline

Every mainstream cloud provider ships a browser-based console that bypasses SSH entirely, and it is the tool that gets you out of all five scenarios above. DigitalOcean calls it the Droplet Console, Vultr and Linode call it the Console/VNC, and Hetzner offers the same thing through its Cloud Console — all of them connect to the virtual machine the same way a physical monitor and keyboard would connect to a bare-metal box, completely independent of the network stack, firewall rules, and the SSH daemon.

Log in to your provider's dashboard, open the server in question, and look for "Console," "VNC," or "Recovery" in the sidebar. You'll get a terminal window rendered directly in your browser, logging you in as root without touching port 22 at all. This is the single most important fact in this article: if you have access to your cloud provider's dashboard, you are never truly locked out.

One quirk worth knowing: some providers route console input through a virtual keyboard layout that doesn't match yours, which can mangle special characters in passwords. If you hit repeated "incorrect password" errors in the console when you know the password is right, try typing it into a plain text field first to check for substituted characters before assuming the account itself is broken.

Check Firewall & Fail2ban

Once you're in through the console, the first thing to rule out is a firewall rule or ban blocking your IP. List the active rules and search for your address, then remove the specific rule rather than flushing the whole table.

# See every active iptables rule (works even if it was set by CSF or UFW)
sudo iptables -L -n --line-numbers
# If you use CSF, check whether your own IP is on the deny list
sudo csf -g <your-ip>
# Remove the IP from the CSF deny list if it shows up
sudo csf -dr <your-ip>
# Check fail2ban for an active jail ban on your IP
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip <your-ip>

If you don't know your current public IP, running curl ifconfig.me from a second device (or a phone on mobile data) tells you exactly what to search for.

Validate sshd Config

If the firewall and fail2ban checks come back clean, the SSH daemon itself is probably refusing to start because of a bad edit to /etc/ssh/sshd_config. Never restart sshd blind — validate the syntax first, since a failed restart on a service you can only reach over the network is exactly how lockouts get worse.

# Test the config file for syntax errors before touching the service
sudo sshd -t
# No output means the config is valid. Errors point to the exact line:
/etc/ssh/sshd_config line 42: Bad configuration option: PermitRootLogins
# Fix the offending line, re-test, then restart
sudo nano /etc/ssh/sshd_config
sudo sshd -t && sudo systemctl restart sshd

If you don't know what changed, checking sudo systemctl status sshd and sudo journalctl -u sshd -n 50 from the console will usually show the exact parse error and line number.

Add a New SSH Key

If your original key is lost, gone, or was overwritten, generate a fresh key pair locally and paste the public half straight into ~/.ssh/authorized_keys from the console session — no SSH connection required to make the edit.

# On your local machine, generate a new key pair
ssh-keygen -t ed25519 -C "recovery-key"
cat ~/.ssh/id_ed25519.pub
# On the server, from the console session, add the public key
mkdir -p ~/.ssh && chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA... recovery-key" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

CloudStick's dashboard includes a browser-based SSH Terminal on every managed server, which is worth keeping in mind as a standing fallback — it authenticates through your CloudStick account session rather than your local SSH client or key files, so it stays available precisely in the scenarios covered here, where the thing that's broken is your local key access, not the server itself.

Prevent the Next Lockout

The single highest-leverage habit for avoiding all of this is testing before you commit. Whenever you edit sshd_config, add a firewall rule, or change fail2ban settings, keep your current SSH session open in one terminal and open a brand-new connection in a second terminal to confirm it still works before you close the first one.

TIP

Never restart sshd from the only session you have open. Make your change, run sudo sshd -t to check the syntax, restart the service, then immediately open a second SSH connection from a fresh terminal window while the first one stays connected. If the new session fails, your original session is still live and you can revert the change on the spot — no console detour needed.

Beyond that habit, keep at least two SSH keys registered on every server, note your office and home IPs before running any hardening script, and confirm fail2ban's maxretry and bantime settings aren't so aggressive that a single mistyped password locks you out for hours. Getting locked out of SSH is disruptive but never fatal — your provider's console, a syntax check before every restart, and a second terminal before you close the first are all it takes to always have a way back in.

Leave a comment
Full Name
Email Address
Message
Contents