Prerequisites
- Root or sudo access
- A current local backup
- Console access in case an SSH rule is misconfigured
Security note
Do not disable password or root SSH access until key-based login works in a separate terminal. Keep the existing session open while testing.
Step 1
Install available updates
Refresh package information and install current updates. Review the package list and schedule a reboot if the kernel or critical libraries changed.
sudo apt update
sudo apt upgradeStep 2
Create a named administrator
Replace USERNAME with a real account name, then set a unique password when prompted.
sudo adduser USERNAME
sudo usermod -aG sudo USERNAMEStep 3
Add an SSH public key
Sign in as the named user and place your public key in ~/.ssh/authorized_keys with restrictive permissions. Never place the private key on the server.
mkdir -p ~/.ssh
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keysStep 4
Configure the firewall
Allow the active SSH service before enabling UFW. Add only the web or application ports the workload needs.
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verboseStep 5
Harden SSH after testing
After key login succeeds in a second session, review sshd_config settings for root login and password authentication. Validate configuration before reloading the service.
sudo sshd -tStep 6
Record a recovery baseline
Document open ports, enabled services, administrator accounts, and the backup location so future changes can be audited.
Troubleshooting
- The SSH key is ignored
- Check ownership and permissions on the home directory, ~/.ssh, and authorized_keys, then inspect the SSH authentication log.
- UFW blocks the application
- Use sudo ufw status numbered, add the specific required port or application profile, and avoid broad allow rules.
Frequently asked questions
Should I disable root SSH login?
Usually yes after a named sudo account and tested recovery path exist. Apply the change only after a second key-authenticated session succeeds.