Stop Typing Your Server Password - A Practical Guide to SSH Keys
If you log into a server over SSH with a password, it works, and nothing is technically wrong with it. But open your auth log sometime and look at how many login attempts are coming from addresses you've never heard of. Any server with port 22 on the public internet gets hammered by bots guessing usernames and passwords around the clock. A strong password will hold up. A key makes the whole thing irrelevant.
Why keys are better
A password is a shared secret. You send it to the server (encrypted, but still), and the server checks it. Anyone who gets that password, by guessing, by phishing, or by you reusing it somewhere that got breached, can log in as you.
An SSH key pair works the same way passkeys do on the web. You generate two keys. The private key stays on your computer and never leaves. The public key goes on the server. When you connect, the server sends a challenge, your machine proves it holds the private key by signing it, and the server checks the signature with the public key. Nothing secret crosses the wire, and there's nothing to guess. A modern key is far beyond anything a bot can brute-force.
You also get convenience. No typing a password every time you connect, and scripts and deploy tools can log in without you storing a password anywhere.
The trade-off is that the private key is now the thing to protect. If someone copies that file off your laptop, they have your access. That's what the passphrase is for, which I'll get to.
Step 1: Make a key
On macOS, Linux, or Windows (PowerShell has OpenSSH built in now), run:
ssh-keygen -t ed25519 -C "laptop"
Ed25519 is the type to use for new keys. It's fast, small, and well supported. The -C part is just a label so you can tell your keys apart later; I like to use the device name.
It'll ask where to save it. The default (~/.ssh/id_ed25519) is fine. Then it asks for a passphrase. Set one. It encrypts the private key on disk, so a stolen copy of the file is useless without it. You won't be typing it constantly, because an SSH agent can remember it for your session (macOS can store it in Keychain, and most Linux desktops handle this too).
You now have two files: id_ed25519 (private, never share it, never paste it anywhere) and id_ed25519.pub (public, safe to hand out).
Step 2: Put the public key on the server
The easy way:
ssh-copy-id user@your-server
This logs in with your password one last time and appends your public key to ~/.ssh/authorized_keys on the server.
If ssh-copy-id isn't available (it isn't on Windows by default), you can do it by hand. Copy the contents of id_ed25519.pub, log in, and run:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys # paste the key on its own line
chmod 600 ~/.ssh/authorized_keys
Those permissions matter. SSH is picky on purpose, and if the folder or file is readable by other users, it will ignore your key and fall back to asking for a password with no obvious explanation.
Step 3: Test it
Open a new terminal and connect:
ssh user@your-server
If it lets you in without asking for the server password (it may ask for your key passphrase, which is different), you're set. If it still asks for the account password, check those permissions first. Running ssh -v user@your-server shows what's happening during the connection and usually points right at the problem.
Step 4: Turn off password logins
This is where the real security win is. As long as password login is allowed, the bots still have something to guess at.
Before you touch anything: keep your current SSH session open the whole time. If you get the config wrong, that open session is your way back in. Also make sure your provider has a web console you can use in a pinch.
Edit /etc/ssh/sshd_config and set:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
The last line still lets root log in with a key but never with a password. Many people go further and set it to no and use a normal user with sudo instead, which I'd recommend.
One gotcha: a lot of distros now include extra config files in /etc/ssh/sshd_config.d/, and cloud images sometimes put a file there that turns password auth back on. Check that folder, because settings in it can win over what you put in the main file.
Then check the config for typos and reload:
sudo sshd -t
sudo systemctl reload ssh # on some distros the service is called sshd
Now, from a different terminal, test again with your key. Then try forcing a password login to confirm it's refused:
ssh -o PubkeyAuthentication=no user@your-server
You should get "Permission denied" without being asked for a password. Only then close that original session.
A few habits worth having
Use one key per device rather than copying the same private key around. If a laptop is lost, you remove that one line from authorized_keys and everything else keeps working.
Set up a ~/.ssh/config file so you can stop typing full hostnames:
Host web
HostName 203.0.113.10
User you
IdentityFile ~/.ssh/id_ed25519
Now ssh web is all you need.
And back up your keys the same way you'd back up anything else important, or at least make sure you have another way in (a second key on another device, a provider console) if your laptop dies. Getting locked out of your own server is the one real downside of doing this right, and it's an easy one to plan around.

Comments