Disabling direct root SSH login means administrators connect with individual accounts and use sudo for permitted administrative work. This guide explains how to change the SSH setting while keeping a working recovery route.
Prepare alternative access
You need server administration access and a provider console or another recovery method. Keep your current administrator session open.
Create or select a non-root account with the required sudo permissions using your distribution’s account management procedure. From a separate terminal, sign in as that user and test:
sudo -v
sudo id -uThe second command should report 0 when authorised. Do not continue if the new login or sudo check fails.
Change the SSH configuration
-
Back up the SSH configuration and identify any included configuration files or
Matchblocks. -
Edit the effective server configuration, commonly
/etc/ssh/sshd_config, with an administrator-capable plain-text editor. -
Set the applicable directive to:
PermitRootLogin no -
Ensure that an earlier setting or connection-specific
Matchblock does not override the intended result. OpenSSH generally uses the first value obtained for a setting. -
Test the syntax before applying it:
sudo /usr/sbin/sshd -t -
Check the effective setting. If connection-specific rules exist, use the real client details in place of these examples:
sudo /usr/sbin/sshd -T -C user=root,host=client.example.com,addr=192.0.2.10Confirm that the output includes
permitrootlogin nofor the intended connection.
Reload and verify
Reload the SSH service used by your distribution. For example, use sudo systemctl reload ssh on a system with ssh.service, or sudo systemctl reload sshd with sshd.service.
Open a new non-root SSH connection and test sudo again. Confirm that a new direct root login is refused. Keep the original session open until those tests pass; use it or the recovery console to correct the configuration if needed.
This disables direct root SSH login, not root administrative capability through an authorised sudo account.