Machine Room

Machine Room · 28 Nov 2026 · 10:25

What Is SSH? What Really Happens When You Log In to a Linux Server

Watch on YouTube

SSH (Secure Shell) is how we log in to a Linux server from another computer and run commands on it. This episode makes one real connection with OpenSSH, from a laptop to a server called devbox, and goes through what happens between pressing Enter and getting in.

The ssh command connects to sshd, the program waiting on port 22 of the server. First the server proves who it is with its host key: ssh shows the key's fingerprint, asks "Are you sure you want to continue connecting", and saves the key in ~/.ssh/known_hosts. Then the two machines run a key exchange and encrypt everything after it. Then we prove who we are. A password works, but the server asks anyone who connects for it. So we make a key pair with ssh-keygen, put the public key into ~/.ssh/authorized_keys on the server with ssh-copy-id, and log in with a signature made by the private key, with no password sent at all. With PasswordAuthentication set to no in the sshd settings, anyone without a listed key gets "Permission denied (publickey)", the same line GitHub gives for a key it doesn't know. And when a server is reinstalled, ssh prints "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!" and refuses to connect until the old key is removed from known_hosts.

This episode with chapters, full transcript and sources: https://themachineroom.dev/v/how-ssh-works/?utm_source=youtube&utm_medium=description&utm_campaign=how-ssh-works

Chapters

  1. 0:00Press Enter: one command, 151 lines
  2. 0:40The Picture: ssh, sshd and port 22
  3. 2:05The Host Key: the fingerprint and known_hosts
  4. 3:13Encryption: the shared secret
  5. 4:08Password Login: why a password is not enough
  6. 5:05The Key Pair: ssh-keygen, private and public key
  7. 6:06Authorized Keys: ssh-copy-id and authorized_keys
  8. 7:07The Signature: logging in with no password
  9. 8:14Permission Denied (publickey): turning password login off
  10. 9:21Host Key Changed: the remote host identification warning

Notes

every line of output on screen comes from a real run of OpenSSH 9.6 on Ubuntu 24.04, on three made-up machines (alex's laptop, the server devbox, sam's laptop). Two steps are ours and not a source's words: that the list in brackets after "Permission denied" is what the server still accepts (read from the run), and the reason our public key doesn't reach the server by itself.

Sources

Get the next one by email

One email per video, with the sources. No newsletter padding, no selling the list. Leave any time.

Transcript

The narration, word for word, from the subtitle file that ships with the video.

0:00Press Enter: one command, 151 lines

We type ssh, alex at devbox, and press Enter. A moment later we're inside another computer, typing commands on it. Ask ssh to show every step, and that moment is a hundred and fifty-one lines long. What happened in it? In this video we make one real connection, from a laptop to a server, and go through what SSH does before it lets us in. By the end we'll know how it checks that the server is the right one, and how it keeps anyone in between from reading what we type. We'll also know how we can log in without typing a password at all.

0:40The Picture: ssh, sshd and port 22

We start with one picture. We have a laptop, and somewhere else there's a server called devbox. We want to run commands on devbox while sitting at the laptop. On devbox, one program is always running and waiting for someone to connect. It's called sshd, and it waits on port 22. On our laptop, we use the ssh command. We type ssh, alex at devbox. Alex is our account on devbox, so this means: log me in to devbox as alex. The ssh command connects to port 22 on devbox, and before we can type anything, three things happen. First, devbox proves that it really is devbox. Second, the laptop and devbox agree on a secret that only the two of them know, and they use it to encrypt everything they send. Third, we prove that we really are alex. After those three, we're logged in. The terminal now says alex at devbox, and whatever we type runs on devbox. The rest of this video is those three steps, one at a time. We start with the first: how the laptop knows it's really talking to devbox.

2:05The Host Key: the fingerprint and known_hosts

In the first step, devbox has to show the laptop who it is. Our laptop has never connected to devbox before, so when we run the ssh command, it stops and tells us it can't be sure who is answering. Every server has a key of its own, made once when the server was set up, and different on every server. It's called the host key, and devbox sends it at the start of every connection. ssh shows us a fingerprint, which is a short form of that key, and asks if we're sure we want to continue. The laptop can't answer for us, because it has nothing to compare the key with. We know which machine we're connecting to, so we type yes. ssh saves devbox's key on our laptop, in a file called known hosts. From then on, every time we connect, devbox sends its key and ssh compares it with the saved one. They match, so the second time we connect, there's no question.

3:13Encryption: the shared secret

The second step happens by itself, with nothing for us to type. Everything we send to devbox crosses a network we can't trust, so before anything private is sent, the laptop and devbox make the connection unreadable to everyone else. To do that, they need a secret that only the two of them know. They agree on one at the start of every connection, by exchanging a few messages. How two machines can agree on a secret while others are listening is a piece of mathematics we won't open here. From then on, everything they send each other is encrypted with that secret: every command we type, and everything devbox sends back. So when ssh asks for our password in the next step, nobody between the laptop and devbox can read it.

4:08Password Login: why a password is not enough

The third step is proving to devbox that we really are alex. The simplest way is a password. We type it, devbox checks it, and we're in. But devbox will ask anyone who connects for that password, not only us. Here's another laptop, used by sam. Sam has no account on devbox, and types the same command: ssh, alex at devbox. devbox asks sam for alex's password. Sam gets three tries, all wrong, and doesn't get in. Nothing stops sam from connecting again and guessing three more. On a server that's open to the internet, attempts like these never stop. So ssh has another way to log in, where no password is sent at all. It uses a pair of keys, and that's next.

5:05The Key Pair: ssh-keygen, private and public key

A key pair is two files that are made together. We make ours on the laptop with a single command, ssh keygen. It asks a few questions, we press Enter, and then both files are created and saved. One file is the private key. It stays on our laptop, and we never give it to anyone. The other file, the one ending in dot pub, is the public key, and that's the one we hand out. The two are linked. Anyone who has the public key can check that we have the private one. But the public key can't be used to make the private key. devbox's host key is a pair like this too. What devbox sent us in the first step was its public key, and its private key never left devbox. So our laptop now has two files: a private key that stays here, and a public key that's ready to go to devbox.

6:06Authorized Keys: ssh-copy-id and authorized_keys

We made the key pair so we can log in without sending a password. But devbox doesn't know our key yet. When we first connected, devbox's key came to our laptop by itself. Ours doesn't go the other way by itself, or anyone could add a key and let themselves in. So we put it there with one command: ssh copy id, with alex at devbox. It logs in with our password one more time, and adds our public key to a file on devbox. That file is called authorized keys, and it sits in alex's home directory on devbox. It's a list of public keys, one per line, and each of them is allowed to log in as alex. The private key hasn't gone anywhere. Now we connect again: ssh, alex at devbox. This time devbox doesn't ask for a password. Our key is on its list, so we're in.

7:07The Signature: logging in with no password

Our public key isn't a secret, so having a copy of it proves nothing. To let us in, devbox needs proof that we hold the private key that goes with it, and the private key must not leave the laptop. The proof is a signature. Our laptop takes a small piece of data from this connection and runs a calculation on it with the private key. The result is the signature. Nobody can produce it without the private key, and anyone with the public key can check or verify it. So ssh sends devbox the signature, and devbox verifies it with our public key from authorized keys. The check passes, so devbox knows the laptop holds the private key, and lets us in as alex. devbox proves itself the same way at the start of every connection. It signs a piece of data with its own private key, and our laptop verifies that signature with devbox's public key, the one it saved in known hosts.

8:14Permission Denied (publickey): turning password login off

Now that the key works, we don't need to log in with a password any more. So on devbox, we turn password login off: from now on devbox accepts a key and nothing else. That stops the guessing sam was doing. It's one line in the settings of sshd, the program on devbox that waits for connections. Sam tries again: ssh, alex at devbox. This time devbox doesn't ask for a password. It answers with one line: permission denied, public key. The word in brackets lists what devbox still accepts. When sam was refused the first time, the brackets said public key and password. Now they say only public key, and sam's laptop has no key that's on devbox's list. Ours does, so we still get in. GitHub gives the same line when it doesn't know the key we offer. So when we see it, the thing to check is whether our public key is on the list at the other end.

9:21Host Key Changed: the remote host identification warning

The last thing is the most common problem people hit with ssh. Suppose devbox gets reinstalled. It keeps its name, but it's a new machine, and a new machine makes a new host key. We connect, and ssh compares the key devbox sends with the one saved in known hosts. They don't match, so ssh prints a large warning and refuses to connect. The warning gives two possible reasons. Either the host key really changed, which is what happened here, or another machine is answering in devbox's place. We know devbox was reinstalled, so we remove its old key from known hosts, with the command the warning shows. We connect again, ssh asks the same question as the first time, we type yes, and the new key is saved. That's what happens in the moment after we press Enter. And if this helped, subscribe.