Skip to content

Disabling SSH password authentication safely

Turning off password SSH is a one-line config change, but doing it without locking yourself out and without breaking the cloud provider rescue path requires a small, deliberate sequence.

Password SSH is the single largest brute-force surface on every internet-facing Linux host. Disabling it is a one-line config change, but doing it without locking yourself out — and without breaking the cloud provider’s serial-console rescue path — requires a small, deliberate sequence. This is the runbook we follow on every host transition.

How to verify

Confirm the effective sshd config first. The file on disk may not be the active setting if a drop-in overrides it.

sudo sshd -T 2>/dev/null | grep -iE 'passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|permitrootlogin|usepam'
ls /etc/ssh/sshd_config.d/
grep -rE '^[A-Z]' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/ 2>/dev/null

sshd -T prints the resolved configuration after all includes and overrides. That is what the daemon will actually use. If passwordauthentication already shows no, you are done — verify keys work and move on.

What’s happening

OpenSSH reads /etc/ssh/sshd_config and any *.conf files in /etc/ssh/sshd_config.d/. The first match for a setting wins (this is unusual — most config systems have last-wins semantics). So if /etc/ssh/sshd_config.d/50-cloud-init.conf sets PasswordAuthentication yes and you add PasswordAuthentication no to the bottom of /etc/ssh/sshd_config, the cloud-init file wins because it is read first. The only reliable way to verify is sshd -T.

Disabling password authentication should always be paired with: confirming key login works for every user who needs access, leaving the cloud provider’s recovery channel (serial console, web console) intact as a break-glass, and not changing the SSH port at the same time so you can roll back cleanly if something goes wrong.

The procedure

  1. Confirm key-based login already works for the named operator users. This is the prerequisite. If it does not, fix it before disabling passwords. See ssh-key-authentication.

    ssh -o PasswordAuthentication=no ops@web-01 'whoami'

    The -o PasswordAuthentication=no flag on the client forces the client to refuse password fallback — if the key fails, the connection fails. That is exactly the behaviour we want to verify before enforcing it on the server.

  2. Find which sshd_config file currently sets PasswordAuthentication. On a default Ubuntu 24.04 host this is often a cloud-init drop-in.

    sudo grep -rni 'passwordauthentication' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/

    If the answer is /etc/ssh/sshd_config.d/50-cloud-init.conf, that is the file that needs to change (or be overridden by a file that sorts earlier).

  3. Create a hardening drop-in that loads first. Lexical order matters; a file named 01-hardening.conf is read before 50-cloud-init.conf.

    sudo tee /etc/ssh/sshd_config.d/01-hardening.conf > /dev/null <<'EOF'
    # Stack Harbor — disable password and keyboard-interactive SSH auth
    PasswordAuthentication no
    KbdInteractiveAuthentication no
    PermitRootLogin no
    PubkeyAuthentication yes
    UsePAM yes
    EOF
    sudo chmod 0644 /etc/ssh/sshd_config.d/01-hardening.conf

    UsePAM yes should remain — disabling it can break other PAM features (session limits, environment, MOTD) that are unrelated to passwords.

  4. Validate the config before reloading. A syntax error in any sshd config file prevents reload, and the daemon stays on the old config. Worse, on some setups, a restart with bad config can lock you out.

    sudo sshd -t
    echo "exit=$?"
    sudo sshd -T 2>/dev/null | grep -iE 'passwordauthentication|kbdinteract|pubkey'

    Expect passwordauthentication no, kbdinteractiveauthentication no, pubkeyauthentication yes.

  5. Reload sshd (do not restart) with an open session as a safety net. reload re-reads config; existing sessions stay alive even if the new config is broken. restart would drop you.

    sudo systemctl reload ssh
    sudo systemctl status ssh --no-pager
  6. From a fresh SSH session — not the existing one — verify the new behaviour.

    ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no ops@web-01 2>&1 | head

    Expected: Permission denied (publickey). or similar. The connection should be refused immediately, no password prompt. If you still get a password prompt, the drop-in did not take effect — re-check sshd -T.

    ssh ops@web-01 'whoami; date'

    Expected: succeeds with the key.

  7. Confirm the cloud provider’s break-glass still works. Every major cloud has a web-console or serial-console path that bypasses SSH. Test it before you need it, because the time to discover it does not work is not 03:00 on a Saturday.

    • AWS EC2: open the instance, “Connect” → “EC2 Serial Console” (must be enabled on the account/region).
    • Azure: “Serial console” on the VM blade.
    • GCP: gcloud compute connect-to-serial-port.
    • Self-hosted: IPMI/iDRAC/iLO depending on hardware.
  8. Document the change. A drop-in file is invisible until you grep for it; a one-line entry in your ops repo’s host inventory (“01-hardening.conf shipped 2026-06-02”) makes the next operator’s audit ten times shorter.

Common pitfalls

  • Adding PasswordAuthentication no to /etc/ssh/sshd_config and assuming it took effect, while a cloud-init drop-in still sets yes and sorts earlier. Always verify with sshd -T.
  • Using systemctl restart ssh instead of reload; restart drops existing sessions and if the new config is bad you may not get back in.
  • Disabling UsePAM along with passwords. PAM does more than password auth (session setup, environment, account-expired check); leave it on.
  • Forgetting to check IPv6 — if your host accepts IPv6 SSH on a separately-configured ListenAddress, the same rules need to apply.
  • Skipping the test from a fresh session. A currently-open session is irrelevant; the question is whether a new connection works.

In the engagements we run, password SSH is disabled before any host enters production, and the change is gated on automated verification — no host moves into the inventory until sshd -T reports the expected values. That gating layer is part of the Managed Operations baseline.