Troubleshooting

Locked Out of SSH? Recover Server Access from Your Phone

If you are locked out of SSH, keep any working session open and check the exact error. Correct the client settings or cloud firewall when possible; otherwise use a provider recovery console to repair SSH configuration or credentials, then verify a fresh login before closing your recovery session.

CC Chen Chen· Founder·2026-09-14·11 min read

If you are locked out of SSH, keep any working session open and check the exact error. Correct the client settings or cloud firewall when possible; otherwise use a provider recovery console to repair SSH configuration or credentials, then verify a fresh login before closing your recovery session.

What should I do first if I am locked out of SSH?

Preserve access before changing anything else. If a terminal still responds, use that session to investigate. A firewall change may leave established connections working, but that depends on the rules and connection tracking; survival is not guaranteed. A detached tmux session alone cannot help you reconnect through a blocked SSH port.

Check the hostname or IP, port, username, and selected key in your phone’s connection settings. Compare them with your provider’s instance details and your known working configuration. Record the last change: enabling UFW, switching networks, editing SSH policy, rotating a key, or changing the port.

If no session works, choose the narrowest available recovery route:

  1. Fix a wrong client setting, or try another already authorized account or key.
  2. Correct a cloud firewall rule in the provider dashboard if that is what changed.
  3. Try an already configured private route through TermAI’s built-in Tailscale.
  4. Open the provider’s recovery or serial console.
  5. Use rescue boot or offline disk repair if the installed OS cannot give you a shell.

Keep a surviving phone session on its current network. Switching from Wi-Fi to cellular can drop it. Use another device for comparison when available; if all sessions are already dead, testing another network can help isolate the failure.

Why is SSH refusing my connection or timing out?

The error tells you which layer to investigate, not the exact cause.

Error or symptomWhat it suggestsFirst check
Connection timed outNo timely response; possible filtering, routing failure, wrong address, or unavailable hostInstance state, destination IP, cloud rules, phone network
Connection refusedAn active rejection; often no listener on that port, sometimes a firewall rejectSaved port and server listener
Permission denied (publickey)SSH reached authentication but rejected the offered identityUsername, selected key, server authorization
Too many authentication failuresToo many failed authentication attempts in that connectionSelect the intended key; avoid offering unrelated identities
Host key verification failedThe client cannot establish the expected server identityVerify the fingerprint independently before changing trust
Connects, then closes or stallsPossible account restrictions, resource exhaustion, or session startup failureAuthentication and system logs through another access path

A website loading does not prove SSH is healthy, and a failed ping does not prove the server is down. If only the phone fails, compare its current source address with the allowlist and ban records before modifying SSH authentication. See SSH IP allowlists from a phone.

A changed host key after rescue boot or a rebuild can be expected, but the change still needs verification through a trusted provider channel. Do not disable host-key checking to make the warning disappear. The host-key verification guide covers that separate recovery step.

How do I access my server when SSH is down?

Use a console that is independent of the installed SSH service. A dashboard button labelled “SSH” or “Connect” may still depend on the same broken daemon and network path.

ProviderRecovery routePrerequisite or limitation
DigitalOceanRecovery Console; recovery ISO for deeper repairRecovery Console differs from Droplet Console and may require a local password. Recovery Console documentation
AWS EC2EC2 Serial ConsoleAccount access, IAM permissions, and supported instance prerequisites apply; a useful guest login or recovery procedure is still needed. EC2 Serial Console documentation
Google Compute EngineInteractive serial consoleInteractive access must be enabled and permitted; viewing serial output alone does not provide a shell. Google serial-console guide
AzureSerial ConsoleCheck access permissions, boot diagnostics, and guest login requirements. Azure Linux Serial Console guide
Hetzner CloudConsole, or Rescue for a separate OSRescue requires a reboot; its SSH endpoint still needs network reachability. Hetzner Rescue instructions

On EC2, an already configured Systems Manager Session Manager connection can also provide a shell independently of SSH. It needs its own working agent, permissions, and service connectivity. EC2 Instance Connect supplies SSH access credentials but is not a replacement for a broken SSH daemon. AWS describes alternatives in its key-pair documentation.

Open the provider dashboard in your phone’s browser and confirm that you can complete MFA. Console access does not automatically grant a root shell. If you cannot authenticate inside the guest, follow that provider’s password-reset or rescue procedure.

Command scope: the commands below run on the installed Linux server through a surviving privileged session or recovery console. They assume systemd; UFW and Fail2ban examples apply only when those tools manage the relevant policy. Replace example addresses, ports, and accounts. Run one block at a time.

How do I undo a firewall rule that blocked SSH?

First distinguish the cloud firewall from the host firewall. Editing UFW cannot repair a cloud security group, network ACL, or missing route. In the dashboard, restore access to the actual SSH TCP port from your current trusted source. Check IPv4 and IPv6 separately and account for return traffic where the network policy is stateless.

From a working server shell, inspect UFW and the listening sockets:

sudo ufw status numbered
sudo ss -lntp

If SSH is listening on port 22 and UFW is the blocker, this example inserts a narrowly scoped allow rule ahead of user rules. Replace the documentation address with your actual source IP and use the real SSH port:

sudo ufw insert 1 allow from 203.0.113.10 to any port 22 proto tcp

Inspect the rules again and test a new connection. An earlier rule in another chain or a cloud firewall can still block it. Do not flush the entire ruleset or disable all filtering as a routine fix. Ubuntu documents UFW inspection and source-specific SSH rules.

If the host uses firewalld, nftables, or another manager, inspect and repair that manager’s policy instead of adding UFW on top. Restore only the rule or known-good configuration responsible for this lockout, including the persistent version used at boot.

For a suspected Fail2ban block, check the active jails, then the relevant SSH jail:

sudo fail2ban-client status
sudo fail2ban-client status sshd

If sshd is the actual jail and its ban list contains your current source address:

sudo fail2ban-client set sshd unbanip 203.0.113.10

Correct the key or username that caused the failures before retrying. The Fail2ban command reference documents jail-specific unbanning. Avoid permanently exempting an entire mobile carrier range.

How do I fix sshd_config without SSH access?

Use the recovery console to inspect the service and recent logs:

sudo systemctl status ssh.service sshd.service ssh.socket --no-pager
sudo journalctl -u ssh.service -u sshd.service -n 60 --no-pager
sudo /usr/sbin/sshd -t

Some listed units will not exist on your distribution. Read the installed unit’s output. sshd -t checks configuration validity and host-key sanity; an error must be fixed before applying changes. It does not prove your account can log in. OpenSSH daemon reference.

Back up the file you will edit. Inspect /etc/ssh/sshd_config and any files it includes, commonly /etc/ssh/sshd_config.d/*.conf. Undo the specific change rather than replacing the entire configuration. Ubuntu explains configuration snippets and validation.

If syntax is valid but your account is rejected, inspect effective settings for the failed connection. Replace all example values with the intended user, client source details, server address, and port:

sudo /usr/sbin/sshd -T -C user=deploy,addr=203.0.113.10,host=client.example,laddr=192.0.2.20,lport=22

Look for AllowUsers, AllowGroups, deny rules, AuthenticationMethods, PubkeyAuthentication, AuthorizedKeysFile, and applicable Match blocks. PasswordAuthentication no is not an error if a working key is authorized. A port change also needs a matching listener and firewall policy. OpenSSH documents the authorization and matching directives.

After validation succeeds, reload the actual running service. For a host using ssh.service:

sudo systemctl reload ssh.service

Use sshd.service instead if that is its name. If the service is stopped, start the correct unit after fixing the cause. A socket-activated installation may need its socket configuration reapplied for port or address changes; follow the distribution’s instructions and inspect ss -lntp again. Keep the console available until a fresh remote login succeeds.

How do I recover SSH access after losing my key or password?

First verify that the phone is using the correct account and private key. If another authorized credential works, use it to repair access. If the private key is lost, create a replacement with a trusted key tool and install its public half through an existing administrator session or the provider recovery path. The private half stays on the client.

Confirm the target account’s home directory:

getent passwd deploy

For a conventional account named deploy with group deploy and home /home/deploy, create its SSH directory if missing and edit the authorization file:

sudo install -d -m 700 -o deploy -g deploy /home/deploy/.ssh
sudoedit /home/deploy/.ssh/authorized_keys

Back up an existing file first. Append the new public key as one unwrapped line; preserve existing authorized keys. Do not edit /root/.ssh/authorized_keys merely because the recovery console logged you in as root.

sudo chown deploy:deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys

These are conventional permissions; also check parent-directory ownership, applicable StrictModes checks, key restrictions, and SELinux labels where enabled. Centralized key management or AuthorizedKeysCommand may require repairing the identity source instead. See the OpenSSH authorization-file reference.

An authorized_keys edit normally needs no daemon reload. Test a new login with the replacement key, then revoke a lost or exposed credential through the system that manages it. DigitalOcean provides a lost-key recovery procedure.

For a forgotten local account password, a privileged recovery shell can run sudo passwd deploy. That does not enable SSH password authentication, bypass MFA, or repair account-expiry and PAM restrictions. If console login also fails, use the provider’s documented offline recovery method.

What if the recovery console also fails?

A full filesystem, exhausted inodes, a broken mount entry, or filesystem damage may prevent normal startup or login. If a console shell works, start with:

df -h
df -i
sudo journalctl -b -p err -n 60 --no-pager

Identify the affected filesystem and cause before deleting anything. For a full disk, recover space from known disposable files under your retention policy; preserve application data and incident evidence. If a recent /etc/fstab edit broke boot, restore the correct entry through the console or rescue environment.

When normal login is unavailable, use rescue boot or attach the stopped server’s disk to a helper instance:

  1. Preserve a snapshot or backup where possible before offline writes. Plan for the service interruption.
  2. Identify the installed system’s volumes with lsblk -f; do not assume the root partition is /dev/sda1.
  3. Unlock encrypted storage and activate any required LVM or RAID layers using the appropriate recovery procedure.
  4. Mount the installed root filesystem. Repair its configuration or authorization file, not the rescue OS’s copy. Account for separately mounted home directories.
  5. If validation requires a chroot, follow the provider’s mount and bind-mount instructions. Do not attempt to restart the installed OS’s services inside an ordinary rescue chroot.
  6. Unmount cleanly, restore normal boot, and verify SSH from a fresh client connection.

DigitalOcean’s recovery ISO guide covers mounting, chroot access, and filesystem recovery. Filesystem checks must follow the filesystem’s procedure; do not run a repair tool against a mounted live filesystem.

Rescue is not guaranteed recovery: missing encryption keys, damaged storage, or insufficient provider permissions can block it. If no usable console, management agent, rescue environment, or disk access exists, contact the provider or someone with physical access. Restore a known-good backup to a replacement instance when needed, accounting for data written since the backup. A backup containing the same bad configuration will reproduce the lockout.

Can I recover SSH access from my phone with TermAI?

Yes, when a working recovery endpoint or SSH route exists. Use the provider’s browser console for out-of-band repair; use TermAI on iOS or Android for a surviving SSH session, an accessible rescue SSH endpoint, and the final fresh-login test. Verify the rescue endpoint’s identity independently before accepting a changed host key.

If the server already belongs to your authorized tailnet, use TermAI’s built-in Tailscale to connect to its tailnet address with the correct port, account, and key. This can avoid a broken public-IP allowlist. It still requires a healthy server, permitted network policy, and an SSH listener reachable on that route.

Ordinary OpenSSH over Tailscale still uses sshd and its authentication configuration. Tailscale SSH is a separate server-side feature with its own SSH policy; built-in Tailscale connectivity does not mean it has been enabled. Neither route is a substitute for an independent console. Tailscale SSH documentation.

Once you have a shell in TermAI, give the AI assistant a bounded request such as:

Ubuntu server using ssh.service and UFW. Suggest read-only commands to check the SSH listener and firewall rules. Explain the output to inspect before proposing a change.

Review each suggestion, confirm the host and parameters, and approve execution yourself. TermAI uses an assistant-style workflow; it does not autonomously repair production. Avoid sharing private keys, credentials, or sensitive logs with the assistant. If you are operating in the provider’s browser console, execute the reviewed recovery steps there; TermAI does not control that browser console.

After authentication works, TermAI’s SFTP can retrieve diagnostic files your account can read. SFTP depends on working SSH and is not a back door around a lockout. The free tier includes five AI calls per day and unlimited SSH/SFTP, so keep a written recovery procedure available alongside the assistant.

How do I change SSH settings without locking myself out again?

Before the next change, test provider-console access from your phone, including MFA and guest login. Store recovery instructions somewhere other than the affected server.

Keep a working privileged session open, back up the configuration you are changing, and arrange a tested rollback to that exact known-good state. A timed rollback should restore specific configuration, rather than disable the whole firewall. Make one change, validate it, then test a new connection before removing the old access path.

Use TermAI for that fresh connection from the phone and network you will actually use during an incident. Confirm the server identity, account, and required administrative access. Retest SFTP if file access matters to your runbook. Remove temporary rules and recovery credentials only after the normal path works.

Maintain a separately revocable phone credential and a console route independent of SSH. For the broader operational workflow, continue with mobile SSH production safety and phone-only incident response.

What else should I know about SSH lockout recovery?

Will rebooting fix an SSH lockout?

A reboot will not undo saved firewall rules, SSH configuration, or missing authorized keys. It also closes surviving sessions. Reboot when a diagnosed problem or a planned rescue boot requires it.

Can I recover SSH access without my original private key?

Yes, if another administrator, an existing privileged session, a provider recovery facility, or offline disk access can install a new public key. A public key cannot reconstruct the lost private key.

Can TermAI unlock a server automatically?

No. TermAI provides SSH/SFTP, built-in Tailscale connectivity, and an AI assistant that suggests commands for you to confirm. It still needs a reachable endpoint and valid authentication.

Does Tailscale fix a broken SSH server?

Ordinary SSH over Tailscale can provide an alternative network route, but still depends on the server’s SSH daemon and credentials. Tailscale SSH is a separate server-side feature with its own policy and prerequisites.

Try TermAI

Free on iOS and Android. 5 AI requests/day on the free tier, plus unlimited SSH/SFTP and built-in Tailscale.

CC
Chen Chen — Founder of TermAI

Writes about mobile DevOps, terminal UX, and the surprising depth of "boring" infrastructure.