Sub topic 9
Set up SSH key authentication
Assume you can use a terminal, the server has a reachable IP address or DNS name, and you have an existing account that can log in with a password or through a console. You also need sudo access on the server.
SSH (Secure Shell) lets you operate a remote system through an encrypted connection. Password authentication is convenient, but an SSH key pair is harder to guess and can be protected by a passphrase. The private key stays on your workstation. The public key is copied to the server.
Generate and install a key
Run these commands on your own computer, not in the server shell:
ssh-keygen -t ed25519 -C "admin@workstation"
ssh-copy-id [email protected]
ssh [email protected]ssh-keygen creates two files by default:
~/.ssh/id_ed25519: the private key. Never copy or publish it.~/.ssh/id_ed25519.pub: the public key. This can be installed on servers.
When prompted, enter a passphrase for the private key. The passphrase protects the key if somebody obtains the file. ssh-copy-id logs in using the current password and appends the public key to the account's ~/.ssh/authorized_keys file.
The server checks that file during login. It accepts the connection only when the client proves that it owns the matching private key.
Check permissions and troubleshoot
The SSH server usually requires restrictive ownership and permissions. On the server, check them with:
ls -ld ~/.ssh
ls -l ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keysThe files must belong to the login user. If you installed the key as another account, repair ownership with:
sudo chown -R admin:admin /home/admin/.sshReplace admin with the real username. A key can still fail when the server has the wrong address, DNS resolves incorrectly, the username is wrong, or the private key is not being offered. Use verbose client output to inspect the attempt:
ssh -v [email protected]| Symptom | Likely cause |
|---|---|
| `Permission denied (publickey)` | Wrong username, missing public key, or incorrect permissions |
Exercise
Where should the private key and public key be stored?
Show answer
Store the private key only on the workstation, protected by a passphrase. Install the public key in the server account's ~/.ssh/authorized_keys file.