When to restrict root and password-based SSH

Narrow login methods only after verifying alternatives and a return plan.

A closed wooden gate beside an open passage: restricting one route while retaining another.
A closed wooden gate beside an open passage: restricting one route while retaining another.

Disabling password login is easier than establishing that the server remains manageable afterwards. The sequence determines whether narrowing access is a useful change or a lockout.

Before changing it, verify a separate administrator’s fresh key-based login, required privileges, and an independent recovery console. An open root session helps retain control during the work but replaces none of these prerequisites.

E-01 practice: why PasswordAuthentication no was not enough

On the training Debian 13 server, the separate solution200 administrator could already sign in using a dedicated key and elevate privileges through password-authenticated sudo. Before restricting access, I repeated a fresh login without reusing an SSH connection, checked sudo, and kept the root session and Mirohost VNC console open. In the console, id -u returned 0.

The main /etc/ssh/sshd_config already contained PasswordAuthentication no. However, it included the configuration directory near the beginning, and 50-cloud-init.conf contained PasswordAuthentication yes:

grep -nE '^(Include|PermitRootLogin|PasswordAuthentication)' /etc/ssh/sshd_config
cat /etc/ssh/sshd_config.d/50-cloud-init.conf
/usr/sbin/sshd -T | grep -E '^(permitrootlogin|passwordauthentication) '

The effective output showed permitrootlogin yes and passwordauthentication yes. For these settings, the first obtained value takes precedence: the earlier Include had already set yes, so the later no did not change it. This was the server’s effective state before my restriction:

A separate early drop-in instead of editing the cloud-init file

I installed /etc/ssh/sshd_config.d/00-solution200-access.conf containing these five lines:

PermitRootLogin no
PubkeyAuthentication yes
AuthenticationMethods publickey
PasswordAuthentication no
KbdInteractiveAuthentication no

The 00- name made it load before the existing 50-cloud-init.conf. The original main file and cloud-init drop-in remained unchanged. AuthenticationMethods publickey explicitly selects the required method; UsePAM yes remained in effect, and the administrator’s OS password remained necessary for sudo. The root restriction applies to new SSH logins, rather than local console root access.

Before writing, I verified the exact machine, unchanged original files, and absence of the new destination. The candidate passed a stdin configuration check without installation; I used sshd -T -C for the actual root and administrator connection contexts. From the root session, I wrote a temporary hidden file in the same directory. This excerpt shows the write operation: data contains the bytes of the five lines above, with a trailing newline; prerequisite checks and the final directory fsync are omitted here.

import os
import tempfile
from pathlib import Path

folder = Path('/etc/ssh/sshd_config.d')
target = folder / '00-solution200-access.conf'
# data contains the five configuration lines shown above, ending in a newline.
fd, temporary = tempfile.mkstemp(prefix='.solution200-access-', dir=folder)
try:
    with os.fdopen(fd, 'wb') as stream:
        stream.write(data)
        stream.flush()
        os.fchown(stream.fileno(), 0, 0)
        os.fchmod(stream.fileno(), 0o644)
        os.fsync(stream.fileno())
    os.link(temporary, target, follow_symlinks=False)
finally:
    os.unlink(temporary)

The temporary name does not match *.conf. os.link adds the completed file atomically and refuses an existing destination, preserving any foreign file. Ownership is root:root and the mode is 0644.

Checking the installed policy and reloading

After installation, I checked syntax and both contexts again. Only three values changed in the complete effective output: permitrootlogin yes → no, authenticationmethods any → publickey, and passwordauthentication yes → no. Other settings, including port 22, stayed the same. This host runs ssh.service with reload support; socket activation is disabled. I ran:

/usr/sbin/sshd -t
systemctl reload ssh.service
systemctl is-active ssh.service

sshd -t exited with code 0 and no messages, reload succeeded, and the service returned active. New journal messages contained no startup or configuration errors. The screenshot shows a repeated read of the installed file and effective policy after reload:

A fresh login, sudo, and rejected older methods

In a new connection without multiplexing, using the same selected key, id -un returned solution200 and id -u returned 1000. I checked privilege elevation:

id -un
id -u
sudo -k
sudo -n -- id -u
sudo -S -p '' -v
sudo -n -- id -u
sudo -k

The first sudo command after sudo -k returned sudo: a password is required and exit code 1. I then supplied the OS password to sudo -S through protected stdin; sudo -n -- id -u returned 0. The password was neither a command argument nor displayed in the terminal. The final sudo -k cleared the cache. This screenshot shows the verification repeated in another fresh session after reload:

For the controlled password-only and keyboard-interactive-only connections, the server offered only publickey; both attempts exited with code 255 without transmitting a password. A new root connection was also rejected. The separately verified PermitRootLogin no in the root context establishes the restriction: rejection of a key absent from root’s authorized keys would not establish that conclusion on its own.

The previously opened root connection remained usable, and a repeated id -u in the VNC console again returned 0. I prepared rollback through those paths: check the new file’s owner and SHA256, remove only that unchanged file, run sshd -t and reload, then verify restored access. Rollback was unnecessary. Further SSH administration uses the separate account with sudo; the root password remains available for local console authentication.

Separate three decisions

Restricting root login, disabling the password method, and controlling keyboard-interactive are different OpenSSH settings. The Debian sshd_config manual documents PermitRootLogin for root access, and PasswordAuthentication and KbdInteractiveAuthentication for distinct authentication methods.

Keyboard-interactive can use PAM and display a password prompt. Disabling PasswordAuthentication alone therefore does not necessarily remove every way of entering a password during login. Keyboard-interactive may also provide a second factor: disabling it mechanically can break an intended MFA scheme.

An illustrative fragment for an environment where ordinary administrators use public keys only:

PermitRootLogin no
PubkeyAuthentication yes
AuthenticationMethods publickey
PasswordAuthentication no
KbdInteractiveAuthentication no

This is neither a complete server configuration nor a universal policy. Consider the requirements of MFA, centralised authentication, and special accounts where applicable. Blocking root over SSH does not remove the system’s root account or prevent local privilege escalation.

Check the configuration the server actually reads

Editing one file does not guarantee the intended result. Debian can include files from sshd_config.d; Match conditions apply to particular users and addresses. For most OpenSSH settings, the first obtained value wins. A line appended at the end may not change effective policy.

In an authorised environment, begin checking the candidate with:

sudo /usr/sbin/sshd -t
sudo /usr/sbin/sshd -T

The sshd manual distinguishes configuration validation from effective-configuration output. Contextual Match rules require the relevant conditions through -C; general output does not describe every possible login. These commands must read the same file and launch options as the intended service.

Successful validation does not prove a new login works. It allows you to proceed to controlled application of the change. Do not apply a candidate that fails validation.

Read policy as a set of conditions

A configuration fragment must literally match its goal. For public-key-only login, check not merely the absence of a password prompt but the full set of permitted methods. Other configured authentication methods may coexist. AuthenticationMethods explicitly defines a required sequence; this example selects publickey. An MFA environment needs a different sequence and a separate decision.

Match conditions matter here. General policy may differ for a particular user, source, or other connection attribute. Candidate checks should therefore include the context of the intended permitted login and the controlled rejected scenario. Inspecting a few arbitrary lines does not describe the server’s whole decision.

Also distinguish local configuration from generated configuration. If a configuration tool maintains a file, its next run may restore an older value. Change the appropriate source and verify that reapplying it does not undo the restriction. That is separate from a successful current reload.

Define timing and acceptance

Before applying, record a short plan: what changes, which checks are required, and when to restore the previous state. If the new administrator can log in but cannot perform the required action, the change has not passed. If that action works but the specified forbidden method is still accepted, the goal has not been achieved either.

This does not require guessing passwords or generating unnecessary login attempts. Use controlled scenarios with known expectations. Client output and logs help identify where rejection occurred. After success, retain the reason for the chosen policy so the next MFA or management change does not begin with guesswork.

Apply and test through another connection

Before applying, preserve previous files, an open administrative session, and a prepared return path. Determine reload or restart behaviour from how SSH actually runs on that host; do not substitute one for the other blindly.

Afterwards, open a genuinely new independent session. Exclude reuse of an existing multiplexed SSH connection, which might avoid new authentication entirely.

Check Expected result for the illustrative policy
Administrator with the intended key A new login succeeds
Required privilege escalation The permitted action works
Direct root SSH login Rejected
Password methods for a control user Rejected according to the chosen policy
Service state and journal No new startup or configuration error

Tie rejection to the policy itself. A wrong password, network timeout, or nonexistent user does not establish that the intended method is disabled. Use a known controlled scenario without risking production-account lockouts.

If permitted login or escalation fails, restore the previous configuration through retained access or the console and verify recovery. Close the old session after confirming the new route. The change then has a concrete outcome: required management remains available, and the specified unnecessary login methods are removed.

  1. Operations & Security A separate administrator: from key login to the right privileges
  2. Operations & Security Your first SSH login: recognising your server
  3. Operations & Security WireGuard on Debian: private SSH access and a post-reboot check
  4. Operations & Security Recovering VPS access when SSH stops working
  5. Observability What the network can see: inventory before firewall rules

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article