How SSH Works

Chapter 1 — How SSH Works

SSH — Secure Shell — is the standard way to connect to a remote machine over a network. It replaced older, insecure protocols like Telnet and rsh by encrypting everything: your commands, the server's responses, and any files transferred. Understanding how SSH works under the hood makes every subsequent chapter easier — and stops you making the mistakes that get people locked out of their own servers.

What SSH gives you: an encrypted terminal session on a remote machine, running as if you were sitting in front of it. Everything after that — SFTP, rsync, port forwarding, tunnelling — is built on top of this same connection.

The Problem SSH Solves

Before SSH, protocols like Telnet sent everything in plain text. Anyone on the same network — a colleague on the same office switch, anyone between you and the server on the internet, or someone with access to router logs — could read your username, password, and every command you typed. SSH eliminates this by encrypting the entire session.

It solves three distinct problems:

  • Confidentiality — everything is encrypted; an eavesdropper sees only scrambled bytes.
  • Integrity — each message is signed; tampering with it in transit causes the connection to fail.
  • Authentication — both sides verify who they're talking to; the server proves it's the right machine, and you prove you're an authorised user.

The SSH Handshake — What Happens When You Connect

When you run ssh user@hostname, a precise sequence of events takes place before you see a prompt. The whole thing happens in under a second.

Your Machine (Client)
1. TCP connectionOpens a TCP connection to port 22 on the server.
3. Check host keyCompares server's public key against ~/.ssh/known_hosts.
5. Agree on session keyUses Diffie-Hellman to derive a shared secret — without ever sending it over the wire.
7. AuthenticateProves identity with a password or private key.
9. Encrypted sessionAll traffic from here is encrypted with the shared session key.
Remote Machine (Server)
2. Send host public keySends its public key so the client can verify it is the right server.
4. Negotiate algorithmsBoth sides agree on the encryption, MAC, and key-exchange algorithms to use.
6. Session key establishedBoth sides now have the same shared secret without it having crossed the network.
8. Grant or deny accessChecks credentials against ~/.ssh/authorized_keys or the system password database.
10. Shell / commandStarts the shell or runs the requested command inside the encrypted channel.
Diffie-Hellman key exchange is the mathematical trick that lets two parties agree on a shared secret over a public channel without ever sending that secret. Even if someone records every byte of your SSH handshake, they cannot derive the session key — it's never transmitted, only calculated independently on each side.

Public Key Cryptography — the Heart of SSH

SSH uses asymmetric cryptography in two places: verifying the server's identity, and (optionally, but preferably) authenticating the user. The concept is always the same: a mathematically linked key pair where anything encrypted with one key can only be decrypted with the other.

Public Key
~/.ssh/id_ed25519.pub
Safe to share with anyone. Goes on the server in ~/.ssh/authorized_keys. Like a padlock — anyone can lock with it, only you can unlock.
Private Key
~/.ssh/id_ed25519
Never leaves your machine. Never share it. If someone has your private key, they are you as far as any server is concerned. Protect it with a passphrase.

When you connect with a key pair, the server challenges your client to prove it holds the private key matching the public key in authorized_keys. The private key signs a piece of data; the server verifies the signature with your public key. Your private key is never sent — the server just confirms the maths works out.

Key Algorithms — Which to Use

Algorithm Key file names Status Notes
Ed25519 id_ed25519 / id_ed25519.pub Use this Modern elliptic curve. Fast, small keys, very strong. Default recommendation.
ECDSA id_ecdsa / id_ecdsa.pub Acceptable Older elliptic curve. Fine but Ed25519 is preferable on all modern systems.
RSA (4096-bit) id_rsa / id_rsa.pub Still common Widely supported including older servers. Use 4096-bit if RSA is required. 2048-bit is marginal.
DSA id_dsa / id_dsa.pub Avoid Fixed 1024-bit key size. Disabled in modern OpenSSH. Do not generate or use.

Host Verification — the known_hosts File

Server authentication is the part most people ignore — but it's what protects you from connecting to the wrong machine. The first time you connect to a host, SSH presents the server's public key fingerprint and asks you to verify it:

philip@debian — first connection to a new host
philip@debian:~$ ssh philip@192.168.1.100 The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established. ED25519 key fingerprint is SHA256:abc123XYZdef456GHI789jklMNO012pqrSTU345vwx. 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.1.100' to the list of known hosts. philip@server:~$

After typing yes, SSH saves the server's public key to ~/.ssh/known_hosts on your machine. Every subsequent connection checks this file — if the server presents a different key, SSH refuses the connection and raises an alarm.

What known_hosts looks like

# ~/.ssh/known_hosts — one line per known host # Format: hostname/IP key-type public-key-data 192.168.1.100 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... 192.168.1.1 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... osztromok.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...

When the host key changes — a WARNING to take seriously

philip@debian — host key mismatch warning
philip@debian:~$ ssh philip@192.168.1.100 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ED25519 key sent by the remote host is SHA256:XYZ... Please contact your system administrator. Add correct host key in /home/philip/.ssh/known_hosts to get rid of this message. Offending ED25519 key in /home/philip/.ssh/known_hosts:3 Host key verification failed.
Do not blindly dismiss this warning. It appears for legitimate reasons — you reinstalled the OS on the server, the server's IP was reused for a different machine, or you updated SSH keys intentionally. But it also appears if someone is intercepting your connection. Verify out-of-band (phone/Slack the server owner, check the console) before overriding it.

Removing a stale known_hosts entry

If you know the key changed for a legitimate reason (you reinstalled the server, for example), remove the old entry with ssh-keygen -R:

philip@debian — remove stale known_hosts entry
philip@debian:~$ ssh-keygen -R 192.168.1.100 # Host 192.168.1.100 found: line 3 /home/philip/.ssh/known_hosts updated. Original contents retained as /home/philip/.ssh/known_hosts.old # Then reconnect — you'll get the first-time fingerprint prompt again philip@debian:~$ ssh philip@192.168.1.100

Verifying a Fingerprint Before Connecting

The right way to confirm a server's fingerprint on first connection is to check it on the server itself, through a separate channel (physical access, the hosting provider's console, or a colleague who has access). Run this on the server:

philip@server — get the server's own fingerprint
philip@server:~$ ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub 256 SHA256:abc123XYZdef456GHI789jklMNO012pqrSTU345vwx root@server (ED25519)

Compare this fingerprint character-by-character with what SSH showed you on the client side. If they match, type yes. If they don't, stop and investigate.

The SSH Directory — File Roles at a Glance

# On your client machine (e.g. philip@debian or philip@windows-pc) ~/.ssh/ ├── known_hosts Keys of every server you've connected to (do not edit by hand) ├── id_ed25519 Your private key — NEVER share or copy to a server ├── id_ed25519.pub Your public key — copy this to servers ├── config SSH client config — shortcuts, per-host settings (Chapter 4) └── authorized_keys On SERVERS: list of public keys allowed to log in # On the server (e.g. philip@server:/etc/ssh/) /etc/ssh/ ├── sshd_config SSH server configuration ├── ssh_host_ed25519_key Server's private host key (never leave this machine) └── ssh_host_ed25519_key.pub Server's public host key (sent to clients on connect)

SSH vs Older Protocols

ProtocolEncryptedStill usedVerdict
SSH Yes — everything Universally The correct choice for all remote access
Telnet No — plain text Legacy embedded systems only Never use on any network you don't fully control
rsh / rlogin No — plain text Essentially extinct Replaced by SSH in the 1990s
Mosh Yes (UDP-based) Niche SSH wrapper that handles roaming and intermittent connections — useful on mobile networks

Key Takeaways

  • SSH encrypts everything — credentials, commands, output, and file transfers all travel as ciphertext.
  • The handshake happens in two stages — server authentication first (is this the right machine?), then user authentication (are you allowed in?).
  • Ed25519 is the key algorithm to use in 2026 — faster and stronger than RSA, supported by all modern SSH implementations.
  • known_hosts is your protection against man-in-the-middle attacks — SSH checks it automatically on every connection.
  • A changed host key warning demands investigation — verify out-of-band before removing the old entry and reconnecting.
  • Your private key never leaves your machine — if asked to copy it to a server, something has gone wrong.
Next — Chapter 2: Connecting to a Remote Host. With the theory in place, Chapter 2 covers the practical side: the ssh command in full, useful flags, connecting with a specific user or port, running a single command without opening a shell, and what to do when the connection is refused.