AI Hints
Linux · Lesson 95 of 120

SSH Keys

Safer than passwords.

⏱ 2 min read▶ Video at 2:06:49🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Passwords can be guessed, stolen, or reused. SSH has a much safer way to prove who you are. A pair of keys. Most professionals use keys every single day.

SSH Keys
From the video: SSH Keys
  • Passwords can be guessed
  • SSH keys are safer
  • Professionals use them daily

A key pair

SSH keys come in pairs. The private key stays on your computer, and you never share it with anyone. The public key can be given out freely. You put it on every server you want to use. When you connect, the server checks that you hold the private key that matches, without the key ever being sent.

Private keystays with you, secret
Public keygoes on servers
They matchlike a lock and its key
Never sharethe private one

Making keys

SSH keygen makes a key pair. Dash T chooses the modern ED two five five one nine type. Normally it asks where to save, and for an optional passphrase. Here we accept the defaults, with no passphrase. The two files appear in the hidden dot SSH folder. The one ending in dot pub is public.

Terminal
ssh-keygen -t ed25519
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/sam/.ssh/id_ed25519):
Created directory '/home/sam/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/sam/.ssh/id_ed25519
Your public key has been saved in /home/sam/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:q8bVlmOkeqb++jH9e7sFsR0M8DJ0wIO5rUVEUcB0n2o sam@laptop
The key's randomart image is:
+--[ED25519 256]--+
|          OX*=   |
|         o.=+ + .|
|          +o...= |
|         ..oo .+.|
|        S+o. Eo .|
|        o+* .  . |
|     . o+o..    .|
|      +.oo .  .. |
|     o=Bo   oooo |
+----[SHA256]-----+
ls ~/.ssh
id_ed25519  id_ed25519.pub

Copying the public key

SSH copy ID installs your public key on the server. It needs the password one last time. After that, SSH logs straight in, with no password at all. Here we even run a single command, hostname, on the server, without staying logged in.

Terminal
ssh-copy-id sam@server
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/sam/.ssh/id_ed25519.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
sam@server's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'sam@server'"
and check to make sure that only the key(s) you wanted were added.
ssh sam@server hostname
server

Summary

SSH keygen makes a key pair, and SSH copy ID puts the public key on a server. Then you log in without a password. Keep the private key secret. Next, copying files between computers.

Key takeaways
  • ssh-keygen -t ed25519
  • ssh-copy-id user@server
  • Private key stays private
Next up: Copying Files Remotely — scp and rsync