Lessons 1/4 · 25 min
SSH: connect with a key, close password access
A starting question
You rent a small server from a host. A few minutes after it goes online, the logs show dozens of login attempts from all over the world. Nobody is "targeting" you: programs continuously try passwords on every address that answers on port 22. A password, even a long one, is a permanent target. A key solves the problem, but only if you use it correctly.
What SSH does
SSH (Secure Shell) opens an encrypted terminal on a remote machine. It uses TCP port 22 by default (workshop 101). It does two distinct things:
- Authenticate the server to you (so you are not talking to an impostor).
- Authenticate the user to the server.
Key authentication: the idea
A key pair has a secret private key that produces signatures, and a public key anyone can use to verify those signatures (principle detailed in workshop 105). For SSH:
- On your computer you create a pair:
ssh-keygen -t ed25519 -C "my-laptop". The file~/.ssh/id_ed25519is the private key;~/.ssh/id_ed25519.pubis the public key. - You copy the public key into
~/.ssh/authorized_keyson the server (the commandssh-copy-id user@serverdoes it). - At login, your client signs data tied to this session with the private key (a simplification: there is no separate random challenge); the server checks the signature with the public key. The private key never leaves your computer and is never sent to the server.
Protect the private file with a passphrase: if it is stolen, it remains encrypted.
First connection: the server's fingerprint
The first time, the client displays:
The authenticity of host 'srv.example.test (203.0.113.10)' can't be established.
ED25519 key fingerprint is SHA256:AbCdEfGhIjKlMnOpQrStUvWxYz0123456789FICTIF.
Are you sure you want to continue connecting (yes/no/[fingerprint])?(The address 203.0.113.10 and the fingerprint are fictitious examples; the range 203.0.113.0/24 is reserved for documentation.) Compare the fingerprint with the one your host shows in its console. If you answer "yes" without comparing, you may be accepting an impostor. Once accepted, the fingerprint is stored in ~/.ssh/known_hosts. If it changes later, SSH prints a big warning ("REMOTE HOST IDENTIFICATION HAS CHANGED"): do not ignore it, investigate (legitimate reinstall or interception).
Permissions: without them, SSH refuses
SSH refuses a private key that others can read. Usual rules:
| Item | Octal mode | Why |
|---|---|---|
~/.ssh (directory) | 700 | only the owner enters |
~/.ssh/id_ed25519 (private) | 600 | owner-only read/write |
~/.ssh/authorized_keys | 600 | nobody else may add a key |
The message "Permissions 0644 for 'id_ed25519' are too open. UNPROTECTED PRIVATE KEY FILE!" tells you exactly this. Fix: chmod 600 ~/.ssh/id_ed25519.
Closing password access — without locking yourself out
The file /etc/ssh/sshd_config configures the SSH server. The useful settings:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes(KbdInteractiveAuthentication no closes another route by which a password may still be requested on some server images; depending on the OpenSSH version, this keyword may be called ChallengeResponseAuthentication.)
Safe procedure, in order:
- Keep a second session open (never close your only connection).
- Check that a key works: open a new connection with the key.
- Edit the configuration, then test its syntax:
sudo sshd -t(no output = syntax is correct). - Reload the service (
sudo systemctl reload sshon Ubuntu; the service is namedsshdon other distributions). A port change may need a restart rather than a reload, especially if the service is started by a systemd socket: check withss -tlnp. - From a third terminal, check that key login still works and that a password attempt is refused.
What this does NOT guarantee
- A key protects the connection, not the machine: if your laptop is compromised, so is the key. Hence the passphrase and one key per device.
- Disabling the password does not stop attempts, only their success. Changing the port may reduce log noise, it is not a protection.
- An account with no password can still be attacked through other open services: closing SSH is not closing the server.
Recap
Create a key pair, deposit the public key, compare the fingerprint at first connection, set the right permissions, then disable password and remote root — keeping a session open and checking the effective value.
