SSH keys from scratch: generate, install, shortcut with ~/.ssh/config, turn off passwords
AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (OpenSSH 10.0p2); all outputs below are copied from that run, not written by hand.
Set up SSH key login from zero: generate an ed25519 key, see what ssh-copy-id actually does on the server, find out why a chmod 777 home folder silently breaks key login, create a short alias in ~/.ssh/config, and safely switch off password logins on the server (and check that it worked).
The problem: Typing a password every time you connect to a server is slow, and password logins are what bots on the internet try to guess all day.
1. Generate a key
First, the key pair. -t ed25519 picks the modern key type: short, fast and secure. -C is just a comment to recognize the key later.
$ ssh-keygen -t ed25519 -C "laptop" -f ~/.ssh/id_ed25519 -N "" Generating public/private ed25519 key pair. Your identification has been saved in /root/.ssh/id_ed25519 Your public key has been saved in /root/.ssh/id_ed25519.pub The key fingerprint is: SHA256:yZJMUMO8ZslIf2trDS5G/fYiQl2u8TCJZGaxeYiAG5o laptop The key's randomart image is: +--[ED25519 256]--+ | . .+o | |o . ..+. | |.+ o =.B | |E oo/oo.. | | B+*S= | | +.@ . | | o o @ | | + * * | | . + o o. | +----[SHA256]-----+ I used an empty passphrase with -N to keep the demo simple. On your own machine, set a passphrase and let ssh agent remember it for you.
$ ls -l ~/.ssh total 8 -rw------- 1 root root 399 Nov 21 19:47 id_ed25519 -rw-r--r-- 1 root root 88 Nov 21 19:47 id_ed25519.pub Two files. The one without an extension is the private key: only you may read it, and it never leaves your computer. The dot pub file is the public key. That's the one you give to servers.
2. Install the public key on the server
On a real server, you'd run ssh-copy-id user@server. It asks for your password one last time and installs the key. Here's what it does on the server side. The public key goes into the user's authorized_keys file, inside a dot ssh folder only that user can access: seven hundred for the folder, six hundred for the file.
$ install -d -m 700 -o deploy -g deploy /home/deploy/.ssh && install -m 600 -o deploy -g deploy ~/.ssh/id_ed25519.pub /home/deploy/.ssh/authorized_keys Now let's connect as deploy. The first time, SSH asks to trust the server's host key. accept-new trusts new servers automatically, but would still refuse a server whose key has changed.
$ ssh -o StrictHostKeyChecking=accept-new deploy@localhost whoami Warning: Permanently added 'localhost' (ED25519) to the list of known hosts. deploy We're in, and no password was asked.
3. Why key login suddenly stops working
A classic support story: someone runs chmod 777 on a home folder to fix some other problem. This is dangerous, so we only do it in the demo container.
$ chmod 777 /home/deploy && ssh -o BatchMode=yes deploy@localhost whoami deploy@localhost: Permission denied (publickey,password). Permission denied. The SSH server refuses to use keys if the home folder, the dot ssh folder or the authorized_keys file can be written by other users. Otherwise, anyone on the machine could add their own key. BatchMode only stops SSH from falling back to a password prompt, so we can see the failure. Fix the permissions, and the key works again.
$ chmod 700 /home/deploy && ssh -o BatchMode=yes deploy@localhost whoami deploy When key login fails for no obvious reason, check those three permissions first. The server's log usually says which one is wrong.
4. A shortcut in ~/.ssh/config
Typing the user, the host and the key every time gets old. In the file ~/.ssh/config, you can give a server a short name.
$ printf 'Host web\n HostName localhost\n User deploy\n IdentityFile ~/.ssh/id_ed25519\n' > ~/.ssh/config && chmod 600 ~/.ssh/config $ cat ~/.ssh/config Host web HostName localhost User deploy IdentityFile ~/.ssh/id_ed25519 Host is the alias you type, HostName is the real address, and User and IdentityFile are filled in for you.
$ ssh web 'whoami; hostname' deploy demo Two words instead of a long command line. The same alias also works for s c p and r sync.
5. Turn off password logins
Now that keys work, the server shouldn't accept passwords at all. First, let's check the effective setting.
$ sshd -T | grep -i '^passwordauthentication' passwordauthentication yes Make sure your key login works and keep one session open before you change this on a real server. Otherwise a typo can lock you out. Debian reads extra settings from the sshd_config.d folder, so we add one small file there, then test the config with sshd -t before reloading.
$ echo 'PasswordAuthentication no' > /etc/ssh/sshd_config.d/10-no-passwords.conf && sshd -t && echo config OK config OK $ kill -HUP "$(cat /run/sshd.pid)" && sleep 1 && sshd -T | grep -i '^passwordauthentication' passwordauthentication no In this container I reload by sending a hang-up signal. On a normal server, that's systemctl reload ssh. Let's prove it: try to log in without the key.
$ ssh -o PubkeyAuthentication=no -o BatchMode=yes deploy@localhost true deploy@localhost: Permission denied (publickey). The server only offers public key now. And the key still works.
$ ssh web 'echo key login still works' key login still works The trap, in one paragraph
Key login worked yesterday. After someone ran chmod 777 on the home folder, it says Permission denied. Why? Home, dot s s h and authorized_keys must not be writable by others. Fix the permissions and the key works again.
Quick check
1. Which file do I give to the server: id_ed25519 or id_ed25519.pub?
- id_ed25519
- id_ed25519.pub
id_ed25519.pub: The file without an extension is the private key. It never leaves your computer.
2. Key login worked yesterday. After someone ran chmod 777 on the home folder, it says Permission denied. Why?
- The key expired overnight
- sshd ignores keys that other users could change
- chmod 777 deleted the key
sshd ignores keys that other users could change: Home, dot s s h and
authorized_keys must not be writable by others. Fix the permissions and the key works again.Recap
ssh-keygen -t ed25519 creates the key. Keep the private key private and give servers the dot pub file, usually with ssh-copy-id. If key login fails, check that the home folder, dot ssh and authorized_keys aren't writable by others. Use ~/.ssh/config for short names, and switch passwords off only after the key works.
Source and reproducible setup
- Lesson page with the same quiz (and a video version): https://shellprovenai.github.io/lekcje/ssh-keys/
- Commands, demo setup script and full test log: https://github.com/shellprovenai/terminal-tutorials/tree/main/ssh-keys
This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.