Home lab SSH hardening: keys first, passwords off, and a way back in
Ask how to harden SSH and the first answers are usually a new port number and a ban tool. Both miss the point. On a default OpenSSH install, password authentication is allowed, and keyboard-interactive authentication is allowed too. Those two defaults, not the port, are what a password guessing script needs. Hardening a home lab host means turning both off, but only after a key has been proved to work for every account that uses the machine. Done in the wrong order, the same change that locks attackers out locks you out first.
Decide how exposed the host is before touching it
Three situations cover most home labs, and they do not need the same amount of work.
| Exposure | What reaches SSH | What this guide asks of you |
|---|---|---|
| LAN only, no port forwarding | Devices already inside your home network | Keys on, passwords off, root login off. That is the whole job. |
| Reached through a VPN you control | Only devices that joined the VPN | The same three changes, plus an AllowUsers line so only named accounts can log in at all. |
| Port forwarded from the internet | Anyone who finds the address | All of the above, plus the attempt limits in the table below, and a hard look at whether the forward needs to exist. |
Notice what is not in the table: moving SSH to a different port. A port change cuts log noise. It does not change who can authenticate, so it comes after the authentication work, if it comes at all.
The directives, checked against the manual
Every setting below was checked against the sshd_config manual page rather than copied from another guide. The defaults are the interesting part: two of the doors start open.
| Directive | Set it to | What the manual says |
|---|---|---|
| PubkeyAuthentication | yes | Public key login, the method that replaces passwords |
| PasswordAuthentication | no | Specifies whether password authentication is allowed; the default is yes |
| KbdInteractiveAuthentication | no | Specifies whether keyboard-interactive authentication is allowed; the default is yes, and ChallengeResponseAuthentication is its deprecated alias |
| PermitRootLogin | no | Controls direct root login; a regular account plus sudo is the pattern the home lab guides document, with prohibit-password as the key only root option for automation that truly needs it |
| AllowUsers | your account names | If specified, login is allowed only for user names that match; by default, login is allowed for all users |
| MaxAuthTries and LoginGraceTime | 3 and 30 | Attempt and grace limits documented in hardening guides, worth adding on a port forwarded host |
The keyboard-interactive line is the one most checklists skip, and it matters. Leaving it on can quietly re-enable password prompts through PAM, so a host can report passwords off while still offering a password shaped door. Set both lines, every time.
One more manual detail decides where the lines go. For most keywords, sshd uses the first value it obtains, and drop-in files under /etc/ssh/sshd_config.d/ are included at the start of the main file and read in lexical order. A hardening line added to the bottom of the main file loses to a drop-in that was already there. That is why the procedure reads the drop-in folder before writing anything.
Inventory first: who logs in to this machine?
Write down every way the host gets logged into today. The list is usually longer than the owner expects:
- Your own accounts, from each computer and phone you actually use, not just the main one.
- Automation: backup jobs, monitoring checks, or sync scripts that connect over SSH from another machine.
- Anyone else with an account, family members included. Their key needs installing too, or their access ends at the reload.
- Your way back in: a monitor and keyboard, a hypervisor console, or a management interface. Name it before you need it.
Every entry on that list gets a key in the next section. An entry that cannot use a key, an old script with a password baked in, gets fixed or retired before passwords go off, not discovered after.
Harden SSH, step by step
- Generate an ed25519 key on each client device. Run
ssh-keygen -t ed25519. It creates a private key at~/.ssh/id_ed25519and a public key at~/.ssh/id_ed25519.pub. The private key never leaves that device. - Install each public key on the server. From the client:
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The public key lands in the account file sshd checks by default,~/.ssh/authorized_keys. - Prove the key works, in a new terminal. Log in fresh from each device and each account on the inventory. Keep the original session open through the whole procedure. A key that has not opened a session has not been proved.
- Read the drop-in folder before writing to it. Run
ls /etc/ssh/sshd_config.d/and search those files for the directives in the table. Because the first value obtained wins, an existing drop-in can outrank your new file. Name yours so it sorts early, for example00-home-lab-hardening.conf, and deal with any conflicting earlier file directly. - Write the drop-in. Put the table into
/etc/ssh/sshd_config.d/00-home-lab-hardening.conf: PubkeyAuthentication yes, PasswordAuthentication no, KbdInteractiveAuthentication no, PermitRootLogin no, and an AllowUsers line naming the inventoried accounts. A drop-in also survives package upgrades of the main configuration file. - Validate, then reload. Check the configuration with
sudo sshd -t, then reload the service rather than dropping every session with a restart. A failed validation is the procedure working: fix the file while passwords are still on. - Test three logins from a second terminal. A key login should succeed. A password attempt should be refused; one documented check is
ssh -o PubkeyAuthentication=no user@host, run before the original session is closed. A direct root login should fail as well. - Close the original session last. Only after all three tests pass. Then repeat the key login once more, so the final state of the machine is the one you have actually used.
What success looks like
Success is specific. A fresh login from every inventoried device opens with a key and no password prompt. The password test is refused, not merely unanswered. The effective settings match the drop-in you wrote, with no earlier file quietly winning. And the recovery route from the inventory, console or physical access, is written down somewhere you can find it without SSH.
If a key is lost next month
Plan for the lost laptop now, while access is easy. Losing a device means two jobs: remove that device public key line from ~/.ssh/authorized_keys on each host, and generate a fresh key on the replacement device. Keeping a second, already proved key on another device you own turns that into an annoyance instead of a console session. The hosts worth doing first are the LAN only ones: the change is small, the recovery route is a short walk, and the same drop-in file, copied and adjusted, hardens the rest of the lab one host at a time.
, Dex Carter, Tech Writer