0xy

0x00  /setup

SSH keys done properly

Ed25519 keys, an agent that holds them, a client config that keeps them apart, and a server that refuses everything else.

3 min read

#ssh#hardening#linux

Most SSH setups stop at "it logs me in". This one goes four steps further: a modern key with a passphrase, an agent so the passphrase is typed once, a client config that offers the right key to the right host, and a server that accepts keys and nothing else.

Generate the key#

One key per context (lab, work, personal), never one key for everything. If a key leaks, only that context burns.

console
$ ssh-keygen -t ed25519 -a 100 -C "laptop-lab" -f ~/.ssh/id_ed25519_lab
  • -t ed25519 is short, fast and has no parameters to get wrong.
  • -a 100 raises the KDF rounds, so brute-forcing the passphrase of a stolen key file is slow.
  • -C is only a label. It ends up in authorized_keys on the server, so make it say where the key lives.

Set a passphrase. An unencrypted private key is a password written in a file.

Let the agent hold it#

The agent keeps the decrypted key in memory so the passphrase is asked once per session, not once per connection.

console
$ eval "$(ssh-agent -s)"
$ ssh-add -t 8h ~/.ssh/id_ed25519_lab
$ ssh-add -l
256 SHA256:… laptop-lab (ED25519)

-t 8h makes the agent forget the key after a working day.

Do not forward the agent

ForwardAgent yes lets anyone with root on the remote host use your agent for as long as you are connected. To reach a machine behind another one, use ProxyJump instead. The key never leaves your laptop.

Client config#

~/.ssh/config
Host lab
    HostName 10.10.0.5
    User ops
    IdentityFile ~/.ssh/id_ed25519_lab
    IdentitiesOnly yes

Host inner
    HostName 10.10.1.20
    User ops
    ProxyJump lab

Host *
    AddKeysToAgent yes
    HashKnownHosts yes
    ServerAliveInterval 60

IdentitiesOnly yes matters more than it looks. Without it the client offers every key the agent holds, in order. That shows the server which keys you carry and can run into MaxAuthTries before the right key is tried.

Install the key on the server#

console
$ ssh-copy-id -i ~/.ssh/id_ed25519_lab.pub [email protected]
$ ssh lab

Lock the server down#

Put the overrides in a drop-in file and leave the distribution's sshd_config alone. On Debian and Ubuntu the drop-in directory is included at the top of the main file, and the first value sshd reads wins.

/etc/ssh/sshd_config.d/10-hardening.conf
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey
PermitRootLogin no
AllowUsers ops
MaxAuthTries 3
Setting Effect
PasswordAuthentication no Password guessing stops being possible at all.
KbdInteractiveAuthentication no Closes the PAM back door that still asks for a password.
AuthenticationMethods publickey States the only accepted method outright.
PermitRootLogin no Log in as a user, escalate with sudo.
AllowUsers ops Every other account is refused before authentication.

Validate the config, then reload. The service is called ssh on Debian and Ubuntu, sshd almost everywhere else.

console
# sshd -t && systemctl reload ssh

Keep your session open

Reloading does not drop existing connections. Leave the current one open and test a fresh login from a second terminal. If it fails, you still have a shell to fix it from.

Check the result#

Ask sshd what it actually uses, then prove a password login is refused:

console
# sshd -T | grep -Ei 'passwordauthentication|permitrootlogin'
permitrootlogin no
passwordauthentication no
$ ssh -o PubkeyAuthentication=no [email protected]
[email protected]: Permission denied (publickey).

If the second command asks for a password, a later file is not the problem. An earlier one is. Look for another drop-in that sets the same option first.