SSH servers control which authentication methods can open new sessions. Setting the server's PubkeyAuthentication to no rejects key-based logins and requires an approved, working alternative.

OpenSSH reads its server configuration from sshd_config and any files loaded by Include. The first global value usually wins, but conditional Match blocks can override it for particular accounts or source addresses.

Public key authentication is often stronger than password-only login. An approved password or keyboard-interactive MFA method must already work before keys are disabled. The setting affects new logins only, so an open session says nothing about whether that method works.

Steps to disable public key authentication on an SSH server:

  1. Keep an authenticated server session or recovery console open throughout the change.

    Disabling the only working authentication method locks out new connections. This sudo-capable session stays needed until the final login succeeds.

  2. Log in from a new connection without using a key.
    $ ssh -o PubkeyAuthentication=no user@host.example.net whoami
    user@host.example.net's password: 
    user

    A keyboard-interactive MFA fallback shows its own prompts instead.

    Fix a failed login before going further, because it is the only way in once keys are disabled.

  3. Check that the main configuration includes the drop-in directory.
    $ sudo less /etc/ssh/sshd_config
    ##### snipped #####
    Include /etc/ssh/sshd_config.d/*.conf
    ##### snipped #####

    Debian and Ubuntu normally include this directory near the beginning. Its files load in name order and the first global value wins, so a PubkeyAuthentication line in an earlier file takes precedence.

  4. Open a new early drop-in file for the override.
    $ sudoedit /etc/ssh/sshd_config.d/01-disable-public-key-authentication.conf

    If the main file has no such Include, add the line to /etc/ssh/sshd_config above any Match block instead.

  5. Disable public key authentication in the drop-in file.
    PubkeyAuthentication no

    Deleting this file and reloading the service later undoes the change.

  6. Test the sshd configuration syntax.
    $ sudo sshd -t

    No output means the configuration passed. An error must be fixed before the reload.
    Related: How to test SSH server configuration

  7. Confirm that the effective configuration disables public key authentication.
    $ sudo sshd -T
    port 22
    addressfamily any
    ##### snipped #####
    pubkeyauthentication no
    ##### snipped #####
    passwordauthentication yes
    kbdinteractiveauthentication no
    ##### snipped #####

    The password and keyboard-interactive lines must still allow the login tested earlier. This output is from a password-only server.

    A Match block overrides these global values. If one sets PubkeyAuthentication yes or an AuthenticationMethods value that needs publickey, change it too, then check that connection with sudo sshd -T -C user=user,addr=192.0.2.10.

  8. Reload the SSH service to apply the change.
    $ sudo systemctl reload ssh

    Use sudo systemctl reload sshd on distributions whose service unit is sshd.
    Related: How to manage the SSH server service with systemctl

  9. Try to log in with a key from a new connection.
    $ ssh -o PreferredAuthentications=publickey user@host.example.net
    user@host.example.net: Permission denied (password).

    This failure is the expected result, even for a previously authorized key. The brackets list the methods the server still offers, and publickey must not be one of them. A host-key or network error proves nothing.

  10. Log in with the fallback method from a new connection.
    $ ssh -o PubkeyAuthentication=no user@host.example.net whoami
    user@host.example.net's password: 
    user

    The account name in the output confirms the login worked, so the recovery session can now be closed.

Share this guide