SSH setup and hardening
Goal
Move a server to key only login with no root password access, and leave a tested way back in.
A good procedure changes one thing at a time and verifies from a second terminal.
When to use
- A new server needs secure access.
- Password login or root login is still on.
- The user wants a client config for several hosts.
- For firewall and web exposure, use
use_skillwithnginx-configfor proxy rules.
Steps
- Ask with
ask_userfor the host, the OS, the current login method and whether there is console access from the provider. Console access is the safety net. - On the user's machine, check for an existing key with
lson~/.ssh. Prefer ed25519; create one withssh-keygenthroughrun_commandonly if none exists and the user agrees. Use a passphrase. - Install the public key on the server with
ssh-copy-idor append it toauthorized_keys, with correct permissions: directory 700, file 600, owned by the user. - Test key login in a new terminal. Do not close the original session.
- Create a non-root user with sudo for admin work if one does not exist, and test sudo.
- Edit
/etc/ssh/sshd_configor a drop-in undersshd_config.d. SetPasswordAuthentication no,PermitRootLogin no(orprohibit-password), and limit users withAllowUsers. - Check for included files that override your setting, since the first value wins in many versions. Show effective values with
sshd -Tthroughrun_command. - Validate with
sshd -tbefore reload, then reload the service and test a fresh login in a third terminal while the old one stays open. - Optionally change limits:
MaxAuthTries, idle timeouts, and disabling unused features such as X11 forwarding. Note that moving the port is minor obscurity, not protection. - Add fail2ban or equivalent and a host firewall that allows the SSH port, after checking the rules will not cut the current session.
- Write a client
~/.ssh/configentry with host alias, user, key andIdentitiesOnly yes. - Record the recovery path: provider console, and where the backup key is kept.
Gotchas
- Reloading sshd with a typo or with password login off before the key works locks the user out. Always test in a second session.
- Wrong permissions on
~/.sshor the home directory make sshd silently ignore the key. - Cloud images often ship a drop-in file that re-enables password login after your edit. Check
sshd -T. - Disabling root login before a sudo user is tested leaves no way to administer the host.
- A key without a passphrase on a laptop is a login for anyone who gets the laptop.
- Never paste a private key into chat. Only the
.pubfile is shared.
Output
- The settings changed and effective values from
sshd -T. - Test evidence for each stage (key login, sudo, fresh session after reload).
- The recovery path, and anything left for the user to do themselves.
