How key-based authentication works (short overview)
- You generate an asymmetric key pair on your client: a private key (keep secret) and a public key (shareable).
- You copy the public key to the remote server and append it to the user’s
~/.ssh/authorized_keys. - The SSH daemon on the server verifies incoming connections by checking the public key and ensuring the connecting client can prove possession of the corresponding private key.
Manual method (what happens under the hood)
- Generate a key pair:
-tspecifies the key type (examples:ed25519,rsa).- This creates
~/.ssh/id_ed25519(private) and~/.ssh/id_ed25519.pub(public) by default.
- Transfer the public key to the server (example methods):
- Copy or append the contents of
~/.ssh/id_ed25519.pubinto the remote user’s~/.ssh/authorized_keys(ensure correct permissions). - Or use
scp/sftpto transfer the public key then append it on the server. - The
ssh-copy-idutility automates this and sets correct permissions — see the practical method below.
Practical method — recommended: ssh-copy-id
Usessh-copy-id to safely install your public key on the remote host:
ssh-copy-id appends the public key to ~/.ssh/authorized_keys on the remote server and sets the proper permissions. It’s faster, less error-prone, and ideal for most workflows.
Prefer
ed25519 keys for modern security and performance, unless you need RSA for compatibility. Protect your private key with a passphrase and use an SSH agent (e.g., ssh-agent + ssh-add) to avoid re-entering the passphrase frequently.Important sshd_config options (server-side)
SSH server configuration typically lives in/etc/ssh/sshd_config (and files under /etc/ssh/sshd_config.d/). The following options control authentication behavior and are commonly adjusted when enabling key-based logins.
Example relevant section from
/etc/ssh/sshd_config:
Demo: Configuring Key-based Authentication

sshd_config.
- Create the
testuser on both systems (control and node1)
test user with the same temporary password (used only for initial setup).
- Generate an SSH key pair for the
testuser on the control node
test user and generate keys:
id_ed25519is the private key — keep it secret.id_ed25519.pubis the public key — safe to copy to servers.
- Install the public key on node1 using ssh-copy-id
test:
- Test SSH login using the key
test:
- Inspect and adjust
/etc/ssh/sshd_configon node1 (as root)
- Verify behaviour for different users
- A user without an installed key (for example
student) should be denied ifPasswordAuthentication nois set:
- A user with a key (for example
test) should succeed:
test can log in via key-based authentication while password logins are disabled for other accounts.
Summary and best practices
- Create a key pair with
ssh-keygen -t ed25519and install the public key usingssh-copy-id. - Ensure
PubkeyAuthentication yesis enabled on the server and only setPasswordAuthentication noafter confirming key logins work. - Disable direct root SSH login with
PermitRootLogin no. - Protect private keys with a passphrase and use an SSH agent to manage passphrases securely.
- Keep at least one administrative account with working key access before disabling passwords to avoid lockouts.
Before disabling password authentication, verify that at least one user account can authenticate using key-based SSH. Otherwise you risk locking yourself out of the server.
Links and references
- OpenSSH Manual: ssh-keygen
- ssh-copy-id — copy public key to remote host
- sshd_config manual (sshd_config(5))