1. Inspect before changing SSH

sudo sshd -t
sudo sshd -T | grep -E '^(port|permitrootlogin|passwordauthentication|pubkeyauthentication)' 
sudo systemctl status ssh --no-pager

Keep your current SSH session open while testing changes. Confirm that you have console or provider recovery access before tightening remote authentication.

2. Create a separate administrator

sudo adduser adminuser
sudo usermod -aG sudo adminuser

Replace adminuser with a unique account name. Open a second terminal and verify that the new account can log in and use the required administrative commands before proceeding.

3. Install an SSH public key

On your client, create a key pair if you do not already have one:

ssh-keygen -t ed25519 -a 64 -C "admin-laptop"
ssh-copy-id adminuser@SERVER_IP

Keep the private key on the client and protect it with a passphrase. Never paste a private key into a server ticket or public chat.

4. Configure sshd safely

On Ubuntu, create a drop-in file so the change is easy to identify:

sudoedit /etc/ssh/sshd_config.d/60-hardening.conf
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
X11Forwarding no
AllowUsers adminuser

Only disable password authentication after key-based login has been tested in a separate session. If your environment uses PAM, MFA or centrally managed access, review the interaction before applying these settings.

5. Validate and apply

sudo sshd -t
sudo systemctl reload ssh
sudo sshd -T | grep -E '^(permitrootlogin|passwordauthentication|pubkeyauthentication|allowusers)'
Lockout prevention: do not close the original session until a new login has succeeded. If validation fails, correct the syntax before reloading.

6. Review authentication logs

sudo journalctl -u ssh --since "today" --no-pager
sudo grep -iE 'failed|invalid user|accepted' /var/log/auth.log | tail -50

Look for unexpected successful logins, repeated failures and unfamiliar source addresses. Logs are evidence, not a substitute for MFA and access control.

7. Operational checklist

  • Use named accounts, not shared root login.
  • Use key authentication and protect private keys.
  • Restrict who can connect where feasible.
  • Keep a recovery path before changing SSH.
  • Review logs and remove unused accounts/keys.

References & further reading

Commands are examples for a representative Ubuntu/Linux environment. Review paths, versions, access requirements and maintenance impact before applying them to production systems.