← Networking and Remote Servers from Zero
SSH keys and config: ssh-keygen, ssh-copy-id, ssh-add and the config file

Lesson 5 of 6

SSH keys and config: ssh-keygen, ssh-copy-id, ssh-add and the config file

Log in without typing a password. Create an SSH key pair, install the public key on a server, save short names in your config file, and turn off password logins.

The idea in plain words

An SSH key pair is a lock and a key. The public key is the lock, and you can put copies of it on any server. The private key is the only key that opens it, and it never leaves your computer. A passphrase protects the private key, like a pin on your key ring.

The config file is your speed dial: short names for long server details. After that, ssh, scp, rsync and git can all use the short name.

Commands in this lesson

ssh-keygen -t ed25519 -C "ava@example.com"
Create a modern key pair: a private key and a public key ending in .pub.
ssh-copy-id ava@203.0.113.10
Install your public key on the server so you can log in without a password.
ssh-add ~/.ssh/id_ed25519
Store your key’s passphrase in memory for the session so you type it once.
Host web
A block in ~/.ssh/config that gives a server a short name, so ssh web is enough.
PasswordAuthentication no
A setting in the server’s sshd_config that turns off password logins once keys work.

Written notes from the video

Why SSH keys

Passwords can be guessed, and typing them gets old fast. This is Networking and Remote Servers from zero, episode five: SSH keys and config. You will log in without a password, and turn long commands into short names.

The lock and key analogy

An SSH key pair is a lock and a key. The public key is the lock. You can put copies of it on any server. The private key is the only key that opens it. It never leaves your computer. A passphrase protects the private key, like a pin on your key ring. And the config file is your speed dial: short names for long server details.

Command 1: ssh-keygen

Command one: ssh-keygen. Choose the ed25519 type. It is modern, small, and secure. Press enter to accept the default location, and set a passphrase. You now have two files: the private key, and the public key ending in .pub.

Command 2: ssh-copy-id

Command two: ssh-copy-id. It installs your public key on the server. You type your password one last time. It adds the key to a file on the server called authorized_keys. Now log in. No password needed.

Command 3: ssh-add

Command three: ssh-add. If your key has a passphrase, ssh-add stores it in memory for the session. You type the passphrase once, instead of on every connection.

Command 4: the config file

Command four: the config file. Create a file at ~/.ssh/config. Give each server a short name, with its address, user, port and key. Now ssh web is all you need to type. Even scp, rsync and git understand these names. Keep one block per server. It is easy to read, and easy to share with your team.

Command 5: turn off password logins

Command five: turn off password logins. Once keys work, passwords are the weak point. Attackers try them all day. Edit the server's sshd config file, and set PasswordAuthentication to no. Then restart the SSH service. Careful: test your key login first, and keep one session open, so a typo cannot lock you out.

Recap

Recap. ssh-keygen makes the key pair. ssh-copy-id installs the public key. ssh-add remembers your passphrase. The config file gives servers short names. And turning off passwords makes the server safer.

Next episode

Next episode, the last one: moving files, and keeping work running on a server. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

Should I put a passphrase on my SSH key?

Yes. A passphrase protects the key if someone gets a copy of the file. ssh-add keeps it in memory for your session so you do not retype it for every connection.

Should I use ed25519 or RSA?

ed25519 is modern, short and secure, and works on current systems. If you must connect to a very old system, RSA with a length of at least 3072 bits is the fallback.

What if I lose my private key?

Create a new key pair and install the new public key on your servers, then remove the old one from authorized_keys. If password logins are already off, you may need your provider’s console to get back in.

Is ssh-copy-id available on Windows?

It is not included with Windows’ OpenSSH. Add the contents of your .pub file to the end of ~/.ssh/authorized_keys on the server instead.

Is it safe to turn off password logins?

It makes the server safer, but test your key login first and keep one session open while you restart the SSH service. Then a typo cannot lock you out.