Skip to content

17 min read

What is SSH and how to connect with a key

In one sentence: SSH opens a terminal on another computer over the network. Everything travels encrypted, the server proves who it is with its key and you log in with your private key, no password.

What you will learn

  • Explain what happens when you connect over SSH, from the ssh client to the sshd server on port 22.
  • Check a server's fingerprint the first time, and know how it differs from HTTPS certificates.
  • Create an ed25519 key, install it on a server and on GitHub, and type less with ssh-agent and ~/.ssh/config.
  • Copy files with scp and rsync, and reach a closed port through a tunnel.

Before you start, read: phase-0/08-tls-and-https.md 06-services-and-packages.md

Imagine that in Phase 8 you rent a server. It’s hundreds of miles away, with no screen or keyboard, and you have to install programs, read its logs and start your website from your terminal.

This used to be done with Telnet, which connected your keyboard to the server over the network. But everything traveled like a postcard, password included. In 1995, after someone stole passwords by snooping on his university’s network, the Finn Tatu Ylönen designed an encrypted replacement (the story, on Wikipedia).

That replacement is SSHSSH stands for Secure Shell.The protocol for opening a terminal on another computer over the network, as if you were sitting in front of it. Everything travels encrypted, and you prove who you are with your private key instead of a password.Go to definition (Secure Shell). It gives you a shell on another computer with everything encrypted, like HTTPS, and lets you log in with a key instead of a password that can be guessed. That’s how almost every server in the world is managed, and how your computer talks to GitHub when you run git push.

The analogy

Imagine you often visit an office in another city. The first time you arrive, you take a photo of the building’s front. Every time you come back, you compare it: if one day the front looks different, you suspect you’ve been taken to another building and you don’t go in.

To get in, you don’t say a password out loud to the doorman. Before your first visit you left a photo of the mark your seal makes, and the doorman has it on his list. Every day he hands you a different piece of paper, you stamp it and he compares the mark. The seal never leaves your pocket.

The photo of the front is the server’s Fingerprint (of a key)A short summary calculated from a public key that serves to recognize it. If the key changes by a single character, the fingerprint comes out completely different.Go to definition. The doorman’s list is the authorized_keys file, and your seal is your Private keyThe half of a key pair that only its owner knows. It decrypts what was encrypted with its public key and signs messages anyone can check with the public key. If it leaks, it must be replaced.Go to definition: the same wax seal from the TLS lesson.

Where the analogy breaks down

  • Nobody guarantees that the first photo is the right one: if on the first day you were taken to a fake building, you keep the photo of the fake. That’s why it’s a good idea to compare the fingerprint with one that reaches you some other way.
  • A seal’s mark can be copied just by looking at it. A private key can’t be worked out from the public one.
  • If you lose a seal, you notice. If someone copies your private key file, you don’t: that’s why it’s protected with a phrase.

SSH is client-server, like the web. On your computer you use the client, ssh, which comes with macOS, Linux and WSL. On the server runs sshd, which listens on PortA number, from 0 to 65535, that says which program on a computer a connection is for. The computer has an address; each listening program has its port.Go to definition 22. The d stands for DaemonA program that keeps running on its own, with no window or terminal, waiting to do its job. Web servers are daemons, and many have a d at the end of their name.Go to definition, like the services in lesson 6.

With nc, the one from Phase 0, you can call that port on your practice server. Here the server speaks first:

Ventana de terminal
$ nc 192.168.64.2 22
SSH-2.0-OpenSSH_9.6p1 Ubuntu-3ubuntu13.19

SSH-2.0 is the protocol version, and OpenSSH is the server program, by far the most used. Then it waits for your client to answer in the same language: quit with Ctrl + C.

When you connect with the real ssh, this happens:

  1. ssh client → sshd serverTCP connection to port 22
  2. sshd server → ssh clientSSH-2.0-OpenSSH_9.6p1
  3. ssh client · sshd serverKey exchange, like in TLS
  4. sshd server → ssh clientIts public server key + a signature
  5. ssh clientIs it in known_hosts? If it's the first time, it asks you.
  6. ssh client · sshd serverFrom here on, everything is encrypted.
  7. ssh client → sshd serverUser ubuntu, your public key + a signature with your private key
  8. sshd serverIs it in authorized_keys? Does the signature check out?
  9. sshd server → ssh clientA shell on the server

There are two checks: first the server proves who it is, and then you do.

The first time: the server’s fingerprint

Section titled “The first time: the server’s fingerprint”

The first time you connect to a server, ssh stops and asks you:

The authenticity of host '192.168.64.2 (192.168.64.2)' can't be established.
ED25519 key fingerprint is: SHA256:YT3BmapFljkFRZNR89J/feDei3USNV/Yk6w53rp0rpw
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

“I can’t check that this server is who it says it is. Its key has this fingerprint. Shall I go on?” The fingerprint is a short summary of the server’s public key: if the key changed by a single character, the fingerprint would come out completely different.

If you answer yes, ssh saves the server’s key in ~/.ssh/known_hosts, the list of servers you know. The next times it compares it without asking, and if one day it doesn’t match, it refuses to connect.

It’s the same idea as the HTTPS CertificateA document that binds a domain name to a public key, signed by a certificate authority and with an expiration date. The server sends it at the start of a TLS connection.Go to definition, with one important difference: here there’s no Certificate authorityAn organization that checks that whoever requests a certificate controls the domain, and then signs it (CA for short). Browsers and operating systems ship with the list of authorities they trust.Go to definition.

HTTPS (TLS) SSH
The server proves who it is with Its certificate and a signature Its server key and a signature
Who vouches for that key A certificate authority Nobody: you, the first time
Where who you trust is stored Your system’s list of authorities Your ~/.ssh/known_hosts
If something doesn’t match An error screen REMOTE HOST IDENTIFICATION HAS CHANGED! and it doesn’t connect

This is called trust on first use. It works if, the first time, you compare the fingerprint with one that reaches you some other way, as you’ll do in “Try it.”

To prove who you are, you create a key pair with ssh-keygen. Two files appear in ~/.ssh:

  • id_ed25519: your private key. It never leaves your computer: it’s not sent anywhere, and it’s not pasted into any website.
  • id_ed25519.pub (for public): your Public keyThe half of a key pair you can show to anyone. It is used to encrypt messages that only the private key will decrypt, and to check the signatures the private key makes.Go to definition. It’s a single line, like ssh-ed25519 AAAAC3Nz… ana@laptop, and you can give it to anyone you like.

ed25519 is the key type recommended today, short and fast. The classic one, rsa, works, but its keys are much longer.

When you create it, ssh-keygen asks you for a passphrase and encrypts the private key file with it. If someone copies the file, it’s useless to them without the phrase. You can leave it empty, but then the file is the key.

authorized_keys: the list of keys that open the door

Section titled “authorized_keys: the list of keys that open the door”

On the server, each user has a ~/.ssh/authorized_keys file with one public key per line: those of the people who can log in as that user.

When you connect, your client says “I want to log in as ubuntu, with this public key.” If it’s on the list, the server asks it for proof: to sign, with the private key, some data that’s different on every connection. The server checks the signature with the public key. Your private key never travels, and a recorded signature is useless for another connection.

sshd is suspicious about the permissions from lesson 4:

  • If other users can write to your ~/.ssh or to authorized_keys, it ignores them: anyone could add their key and log in as you.
  • The right setting is 700 for the folder (only you get in) and 600 for authorized_keys (only you read and write).
  • Your client checks too: if other users can read your private key, it refuses to use it.

On the Multipass VM, and on almost every cloud server, logging in with a password already comes disabled: you can only log in with a key. In Phase 8 you’ll do the same on your server.

If your key has a phrase, ssh asks for it on every connection. ssh-agent is a program that keeps your keys, already decrypted, in memory for as long as your session lasts, and signs on behalf of ssh.

  • ssh-add adds your key to the agent (it asks for the phrase once).
  • ssh-add -l lists the ones it has.

On macOS, the agent is already running. On Linux and WSL, sometimes it isn’t: you start it with eval "$(ssh-agent -s)", which answers something like Agent pid 74.

Typing ssh ubuntu@192.168.64.2 every time gets tiring, and on Bandit you also have to add the port. The ~/.ssh/config file keeps those details under a short name:

Host course
HostName 192.168.64.2
User ubuntu
Host bandit
HostName bandit.labs.overthewire.org
Port 2220
User bandit0

With it, ssh course is the same as ssh ubuntu@192.168.64.2, and ssh bandit, the same as ssh bandit0@bandit.labs.overthewire.org -p 2220. scp, rsync and git read it too.

scp is like cp, but between computers. The remote side is written server:path, and if the path is empty, it’s the user’s home folder:

Ventana de terminal
$ scp notes.txt course:
notes.txt 100% 22 33.1KB/s 00:00
$ ssh course cat notes.txt
Hello from the laptop

The second command runs cat notes.txt on the VM and brings you the output, without staying inside. The other way around, scp course:notes.txt . brings the file to your current folder.

rsync is for folders: it compares both sides and only sends what has changed. Copying a web folder to the VM twice in a row proves it:

Ventana de terminal
$ rsync -av web/ course:web/
Transfer starting: 3 files
created directory web
./
index.html
styles.css
sent 272 bytes received 70 bytes 162857 bytes/sec
total size is 35 speedup is 0.10
$ rsync -av web/ course:web/
Transfer starting: 3 files
sent 139 bytes received 20 bytes 75714 bytes/sec
total size is 35 speedup is 0.22

The first time it copies both files; the second, none, because they haven’t changed. -a keeps the permissions and dates, and -v says what it’s doing. The final / in web/ means “what’s inside web”: without it, you’d create web/web on the other side. On Linux, the first line is sending incremental file list.

An SSH tunnelSSH stands for Secure Shell.An SSH connection that carries other connections inside it. With a tunnel, a port on your computer takes you to a port on the server that is closed from the outside, and everything travels encrypted.Go to definition makes a port on your computer lead to a port on the server, through the SSH connection:

Ventana de terminal
ssh -N -L 8080:localhost:80 course

It reads like this: “listen on my port 8080 (-L, for local). Whatever arrives, carry it encrypted to course and, from there, connect to localhost:80.” That localhostA name that always means “this same computer.” When you open localhost:8080, both the client and the server are on your machine.Go to definition is the server’s, not yours. -N says you don’t want a shell, only the tunnel.

  1. Your browser or curllocalhost:8080

    connection inside your laptop

  2. The ssh clienton your laptop

    encrypted, through port 22

  3. sshdon the VM

    connection inside the VM

  4. nginxlocalhost:80

Why bother, if you can reach the VM directly? On a real server, the firewall only leaves port 22 open, and the Phase 5 database only listens on localhost. The tunnel lets you reach them without opening anything else.

The goal: log in to three servers (Bandit with a password, and GitHub and your VM with your key) and see your VM’s nginx page through a tunnel.

The outputs are from a Mac with the user ana, and the VM’s, from Ubuntu 24.04. On Windows, do exercises 1 to 5 in WSL. Exercises 6 to 8 need the Multipass VM, which you don’t use on Windows: just read them.

1. Log in to Bandit, with a password

Ventana de terminal
ssh bandit0@bandit.labs.overthewire.org -p 2220

What you will see:

The authenticity of host '[bandit.labs.overthewire.org]:2220 ([13.51.99.177]:2220)' can't be established.
ED25519 key fingerprint is: SHA256:C2ihUBV7ihnV1wUXRb4RrEcLfXC5CXlhmAAM/urerLY
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[bandit.labs.overthewire.org]:2220' (ED25519) to the list of known hosts.
…
This is an OverTheWire game server.
More information on http://www.overthewire.org/wargames
backend: gibson-1
bandit0@bandit.labs.overthewire.org's password:
…
bandit0@bandit:~$

Bandit is a game for learning Linux, and each level is a user on its server. bandit0 is the level 0 user, and -p 2220 is the port: Bandit doesn’t use 22.

  • Answer yes to the fingerprint question: you have no other one to compare it with.
  • The password is on the level 0 page. Nothing shows up as you type it, not even asterisks: that’s normal. If you get it wrong, it answers Permission denied, please try again.
  • Inside, you’ll see the rules of the game, and the prompt changes to bandit0@bandit:~$: you’re on another computer. Leave with exit.

The rules ask you not to publish passwords or solutions: the next levels are up to you. Your Bandit IP may be different.

2. Create your key pair

Ventana de terminal
ssh-keygen -t ed25519 -C "ana@laptop"

What you will see:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/ana/.ssh/id_ed25519):
Enter passphrase for "/Users/ana/.ssh/id_ed25519" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/ana/.ssh/id_ed25519
Your public key has been saved in /Users/ana/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:ko4J3qj4vKFLDWN/lx2xm217IX7TX9J3lMrkp3MQ4g8 ana@laptop
The key's randomart image is:
+--[ED25519 256]--+
| |
| |
| . |
| . o. . .|
| +. o So. .....|
|..=+ + .o =E+o.o |
| .+o+..o +.o++=.=|
|o+ .. . ...*o++|
|=o+. .o.+ o|
+----[SHA256]-----+

-t chooses the key type, and -C adds a comment so you can recognize it: your user and your computer.

  1. At the first question, press Enter: it accepts the usual place.
  2. Type a passphrase twice. It doesn’t show up as you type it either.

If it answers Overwrite (y/n)?, you already had a key: answer n and use the one you have. If you overwrite it, you lose access to wherever you used it.

Your fingerprint and your drawing (randomart, the fingerprint as a picture) will be different. If you didn’t have the ~/.ssh folder yet, you’ll also see Created directory. On Linux and WSL, the third line is Enter passphrase (empty for no passphrase):.

3. Check the permissions

Ventana de terminal
ls -l ~/.ssh

What you will see:

total 24
-rw-------@ 1 ana staff 444 Oct 12 14:49 id_ed25519
-rw-r--r--@ 1 ana staff 92 Oct 12 14:49 id_ed25519.pub
-rw-r--r--@ 1 ana staff 116 Oct 12 14:46 known_hosts

Look at the letters at the start. The private key is -rw------- (600): only you can read it. The public one, -rw-r--r-- (644): anyone can read it, and that’s fine. ssh-keygen already leaves them like that.

known_hosts keeps Bandit’s key from exercise 1. On Linux and WSL you’ll see ana ana instead of ana staff, and no @.

4. Give your key to the agent

Ventana de terminal
ssh-add
ssh-add -l

What you will see:

Enter passphrase for /Users/ana/.ssh/id_ed25519:
Identity added: /Users/ana/.ssh/id_ed25519 (ana@laptop)
256 SHA256:ko4J3qj4vKFLDWN/lx2xm217IX7TX9J3lMrkp3MQ4g8 ana@laptop (ED25519)

ssh-add asks for the phrase once, and ssh-add -l shows the fingerprint of the key the agent holds: the same one as in exercise 2. Until you close your session, ssh won’t ask for it again.

On Linux and WSL, if it answers Could not open a connection to your authentication agent, start the agent first with eval "$(ssh-agent -s)" and try again.

Now, GitHub. Before you give it your key, connect to see what happens and check its fingerprint:

5. Your key on GitHub

Ventana de terminal
ssh -T git@github.com

What you will see:

The authenticity of host 'github.com (140.82.121.3)' can't be established.
ED25519 key fingerprint is: SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
git@github.com: Permission denied (publickey).

Before you answer yes, open the fingerprints GitHub publishes and compare the ED25519 one. -T says you don’t want a shell, because GitHub doesn’t give one. Your GitHub IP may be different.

Permission denied (publickey) is expected: GitHub doesn’t know your key yet. Give it to GitHub:

  1. Show your public key with cat ~/.ssh/id_ed25519.pub and copy the whole line.
  2. On GitHub, go to Settings → SSH and GPG keys → New SSH key, paste the line and save it.
  3. Run ssh -T git@github.com again.

Now it answers Hi ana! You've successfully authenticated, but GitHub does not provide shell access., with your GitHub user instead of ana: it has recognized you by your key. If it still says Permission denied, check that you pasted the .pub file.

Last, your VM. Its IP is shown by multipass list, in the IPv4 column: in the examples it’s 192.168.64.2, and yours may be different. First, ask the VM for its fingerprint some other way, through Multipass, and copy your public key into it:

6. Get the VM ready

Ventana de terminal
multipass exec course -- ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
cat ~/.ssh/id_ed25519.pub | multipass exec course -- tee -a /home/ubuntu/.ssh/authorized_keys

What you will see:

256 SHA256:YT3BmapFljkFRZNR89J/feDei3USNV/Yk6w53rp0rpw root@course (ED25519)
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICrCC5s7Ocaft6KTRg9Ea+DkdipuwEMlkTBxufSg1hev ana@laptop

multipass exec course -- runs whatever comes after it on the VM, through Multipass and not through your ssh.

  • The first command shows the fingerprint of the VM’s server key. Write it down.
  • The second adds your public key to the ubuntu user’s authorized_keys. The | from lesson 3 passes it to tee, which writes it to the file and to the screen (that’s why you see it), and -a (append) puts it at the end. Without -a, you’d erase the key multipass shell uses.

7. Log in without a password

Ventana de terminal
ssh ubuntu@192.168.64.2

What you will see:

The authenticity of host '192.168.64.2 (192.168.64.2)' can't be established.
ED25519 key fingerprint is: SHA256:YT3BmapFljkFRZNR89J/feDei3USNV/Yk6w53rp0rpw
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.64.2' (ED25519) to the list of known hosts.
Welcome to Ubuntu 24.04.5 LTS (GNU/Linux 6.8.0-117-generic aarch64)
…
ubuntu@course:~$

Compare the fingerprint with the one you wrote down in exercise 6: if it’s the same, answer yes. No password and no phrase: your key opens the door, and the agent unlocked it in exercise 4. Leave with exit.

If it answers Permission denied (publickey), go back over exercise 6. Your fingerprint and your versions will be different.

Now create ~/.ssh/config with nano ~/.ssh/config (the editor from lesson 2), copy the Host course block from above into it, with your VM’s IP, and save it. Then:

8. A nickname and a tunnel

Ventana de terminal
ssh course whoami
ssh -f -N -L 8080:localhost:80 course
curl -I http://localhost:8080

What you will see:

ubuntu
HTTP/1.1 200 OK
Server: nginx/1.24.0 (Ubuntu)
Date: Mon, 12 Oct 2026 13:31:25 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Mon, 12 Oct 2026 13:29:44 GMT
Connection: keep-alive
ETag: "6acce0c8-267"
Accept-Ranges: bytes
  • ssh course whoami uses the nickname, runs whoami on the VM and answers ubuntu.
  • The second command opens the tunnel, and -f sends it to the background so you can keep typing.
  • curl -I asks your port 8080 for the page, and the VM’s nginx answers, the one from lesson 6: notice Server: nginx/1.24.0 (Ubuntu). In the browser, http://localhost:8080 shows “Welcome to nginx!” Your dates will be different.

If curl answers Failed to connect to localhost port 8080, the tunnel didn’t open: check whether the second command gave an error. If it says Address already in use, another program is using your 8080: try -L 8081:localhost:80.

To close the tunnel, find its PID with pgrep -f 8080:localhost (a pattern that only appears in the tunnel’s command) and stop it with kill and that number, as in lesson 5.

  • GitHub’s “Code” button has an SSH tab with addresses like git@github.com:user/repo.git: the user git, on the server github.com.
  • When you create a cloud server, the provider asks you for your public SSH key and puts it in the new server’s authorized_keys, as you did in exercise 6.
  • multipass shell course also logs in over SSH, with a key Multipass created for you.

And if you code:

  • git push and git pull with a git@github.com:… address use your key and your agent. If they fail, ssh -T git@github.com tells you whether the problem is the key.
  • Deleting the warning without thinking. If you recreate the VM and it gets the same IP, the key no longer matches, and ssh answers WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! and doesn’t connect. Check the new fingerprint as in exercise 6, and only then delete the old one with ssh-keygen -R 192.168.64.2. If you don’t know why it changed, don’t connect.
  • Open permissions on your key. If others can read your private key, ssh ignores it: WARNING: UNPROTECTED PRIVATE KEY FILE! and Permissions 0644 for '/Users/ana/.ssh/id_ed25519' are too open. Fix it with chmod 600 ~/.ssh/id_ed25519.
  • Open permissions on the server. With authorized_keys at 666, the client only sees Permission denied (publickey). The reason is in the VM’s log (sudo journalctl -u ssh): Authentication refused: bad ownership or modes for file /home/ubuntu/.ssh/authorized_keys.
  • Forgetting the user. ssh 192.168.64.2, without ubuntu@, uses your laptop user, which doesn’t exist on the VM: ana@192.168.64.2: Permission denied (publickey). Add the user, or a User line in ~/.ssh/config.
  • SSH opens a terminal on another computer with everything encrypted. The client is ssh, and the server, sshd, on port 22.
  • The server proves who it is with its key. The first time you check its fingerprint yourself, because there’s no certificate authority, and known_hosts remembers it.
  • You log in with your private key, which never leaves your computer. The public one goes in the server’s authorized_keys or on GitHub, and the permissions have to be 600.
  • ssh-agent saves you from repeating the phrase, and ~/.ssh/config saves you from repeating the IP, the user and the port.
  • scp and rsync copy files over SSH, and ssh -L takes a port of yours to a port on the server.
You log in to your company’s server over SSH every day. Today ssh answers WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!, and nobody has announced any change. What do you do?

A tutorial tells you: “paste your SSH key into GitHub.” What do you paste?

A coworker sends you their id_ed25519 file over chat so you can log in to the server with their key, because it’s quicker. What’s the problem?

You run ssh -N -L 5433:localhost:5432 course. Where does a connection to localhost:5433 on your laptop end up?

You add your public key to authorized_keys on the VM, but ssh still answers Permission denied (publickey). ls -ld ~/.ssh shows drwxrwxrwx. What’s going on?

Your browser doesn’t ask you about any fingerprint when you visit your bank for the first time. Why does ssh do it with a new server?

phase-1 · lesson 9/10