Sub topic 9
Set up SSH key authentication
This chapter assumes you can log in to the server over SSH with a password, that you have created the non-root administrative user from the previous chapter, and that you are comfortable editing files in a terminal. You will replace password logins with key-based logins. The next chapter turns off passwords for good, so the work here must be correct first.
How key authentication works
An SSH key pair has two files. The private key stays on your computer and must never be copied elsewhere. The public key is safe to share and is placed on every server you want to log in to.
When you connect, the server uses your public key to issue a challenge. Your client answers it by signing with the private key. The private key never crosses the network, so there is no password for an attacker to guess or capture.
Choose a key type
Use Ed25519 unless you must support very old software. Its keys are short, fast to verify, and well regarded. RSA remains a fallback for old systems, and you should use at least 3072 bits.
| Type | Typical use | Notes |
|---|---|---|
| ed25519 | Default choice | Small keys, modern OpenSSH |
| rsa (3072+ bits) | Legacy compatibility | Larger keys, slower |
| ecdsa | Rarely needed | Prefer Ed25519 |
Generate the key pair
Run these commands on your own computer, not on the server.
# On your own computer
ssh-keygen -t ed25519 -C "admin@workstation"
ssh-copy-id [email protected]
ssh [email protected]ssh-keygen asks where to save the key. The default, ~/.ssh/id_ed25519, is fine. It then asks for a passphrase. Set one. A passphrase encrypts the private key file, so a stolen laptop or backup does not give the thief access to your servers.
The -C option adds a comment to the public key. It is only a label, and it helps you recognize the key later.
ssh-copy-id connects with your password one last time. It appends the contents of id_ed25519.pub to ~/.ssh/authorized_keys on the server and sets safe permissions. The last command should now log you in with the key. If you set a passphrase, you are asked for it instead of the server password.
What lives on the server
Each user has an authorized_keys file in ~/.ssh. The file holds one public key per line. If you cannot use ssh-copy-id, for example because the server has no password login, you can install a key by hand through the cloud console or another access path.
# On the server, as the admin user
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAA...your-public-key... admin@workstation" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keysReplace the placeholder with the full line from your id_ed25519.pub file.
Permissions and common failures
By default, sshd refuses to use keys when file permissions are too loose, because other users could change them. The check covers your home directory, ~/.ssh, and authorized_keys.
| Path | Required state |
|---|---|
| Home directory | Not writable by group or others |
| `~/.ssh` | Mode 700, owned by the user |
| `~/.ssh/authorized_keys` | Mode 600, owned by the user |
| Private key on your computer | Mode 600 |
If key login fails and you are asked for a password again, work through these causes:
- The public key was added to the wrong user's
authorized_keys. - The file permissions or ownership are wrong. Check with
ls -ld ~ ~/.ssh ~/.ssh/authorized_keys. - The client offered a different key than the one you installed. Run
ssh -v [email protected]and read which identity files are tried. - The server has
PubkeyAuthentication noset somewhere in its configuration. - Home directory encryption or SELinux labels prevent the daemon from reading the file. On RHEL-family systems,
restorecon -R ~/.sshrepairs the labels.
The server-side log is the fastest way to see the real reason. On Debian and Ubuntu, run sudo journalctl -u ssh -e. On RHEL-family systems, run sudo journalctl -u sshd -e. Messages such as Authentication refused: bad ownership or modes point straight at permissions.
Use an agent and a client configuration
Typing a passphrase on every connection is tedious. ssh-agent holds the decrypted key in memory for your session. Add the key once, and later connections use it.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ssh-add -lMany desktop systems start an agent for you. ssh-add -l lists the keys it holds.
To avoid typing long host names and user names, add an entry to ~/.ssh/config on your computer.
Host web01
HostName server.example.com
User admin
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yesNow ssh web01 is enough. IdentitiesOnly yes makes the client offer only the named key. Without it, an agent holding many keys can offer them all, and the server may disconnect you after too many failed attempts.
Keep keys manageable
Treat keys as credentials with a life cycle.
- Use one key pair per person and per workstation, so you can revoke one without affecting the others.
- Put a distinguishing comment on each key. Then you can tell lines in
authorized_keysapart. - When someone leaves or a device is lost, delete that key's line from every server's
authorized_keys. - Never put a private key on a server just so you can hop to another machine. Use agent forwarding with
ssh -Aonly to hosts you trust, or better, usessh -J jumphost targetto pass through a jump host without exposing your agent.
Verify before you move on
Run this test from your computer. It forces the client to use only keys, so a success proves the key works and not a fallback password.
ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected] 'whoami && hostname'The output should show admin and the server's hostname with no password prompt. If the command asks for a passphrase, that is your key passphrase, not the server password. Also confirm that sudo -v works for this user in the same session.
Exercise
You ran ssh-copy-id and it reported success, but ssh [email protected] still asks for the account password. Name two checks you would make on the server, and one command on the client that shows which keys are being offered.
Show answer
On the server, check that ~/.ssh is mode 700 and authorized_keys is mode 600 and that both are owned by the admin user, and that the home directory is not group or world writable. Also read the daemon log with sudo journalctl -u ssh -e (or -u sshd on RHEL-family systems) for messages such as "bad ownership or modes". On the client, run ssh -v [email protected] to see which identity files are tried and whether the server accepts any of them.
Exercise
Why is it a mistake to disable password authentication right after running ssh-keygen, before testing a login?
Show answer
If the key was not installed correctly, or the permissions or the user are wrong, key login will fail. With passwords turned off, no other SSH method remains, so you would be locked out and would need console access to repair it. Test key login from a second terminal first.