DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · SECURITY · 22 OF 187

SSH Explained: Keys, Agents, Config and Tunnels

How you reach every server safely: what SSH protects, public and private keys (an ed25519 key took 7 ms to make and its public half is 89 bytes; RSA 4096 took 1,041 ms and 733 bytes), authorized_keys and the permission checks that refused our login when the folder was too open, host keys and known_hosts, ~/.ssh/config, the agent, jump hosts, port forwarding, and why cloud teams now prefer short-lived access.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Encrypted channel
  • ed25519 keys
  • authorized_keys, strict permissions
  • known_hosts
  • config, agent
  • bastions

Notes

What SSH does

SSH, the secure shell, gives you an encrypted channel to another machine over TCP port twenty two. Three things are protected. Nobody in between can read or change the traffic. You can verify the server's identity with its host key. And the server verifies yours, with a password or, much better, a key. Over that channel you get a shell, remote commands, file copy with scp, sftp or rsync, and tunnels.

SSH gives you
  • encryption: nobody can read the traffic
  • server identity: you know it is the real server (host key)
  • user identity: password or, better, a key
  • a shell, commands, file copy (scp, sftp, rsync), tunnels
  • default port: TCP 22

Key pairs

A key pair is two matching keys. The private key stays on your laptop and never leaves it. The public key can be shared with any server. ssh keygen makes them. An ed twenty five five one nine key took seven milliseconds in our run, and its public half is eighty nine bytes. An RSA key of four thousand and ninety six bits took over a second, and its public half was seven hundred and thirty three bytes. Prefer ed twenty five five one nine, and protect the private key with a passphrase.

Our runs
$ ssh-keygen -t ed25519
ed25519: 7 ms, public key
  89 bytes
$ ssh-keygen -t rsa -b 4096
rsa 4096: 1,041 ms, public
  key 733 bytes
private key mode: -rw-------
prefer ed25519; protect the
  private key with a passphrase

Key login

To log in with a key, the server lists your public key in the user's authorized keys file. When you connect, the server sends a challenge and your client signs it with the private key; the server checks the signature with the public key. The private key never crosses the network. We ran an SSH server on port two two two two and logged in as the user lab: hi from lab, in about two hundred and ten to two hundred and thirty milliseconds per run, including the handshake.

Our login (port 2222)
server: ~lab/.ssh/
  authorized_keys = your .pub
$ ssh -i k_ed25519 -p 2222
    lab@127.0.0.1 'echo hi'
hi from lab
3 runs: 229, 213, 209 ms

ssh-copy-id user@host
  installs your key for you

Permissions matter

Then we broke it on purpose. We made the user's dot ssh folder writable by everyone. The same key, the same command, and now: permission denied, public key. The SSH server refuses keys stored where someone else could have added their own key. Dot ssh must be seven hundred, authorized keys six hundred, and the home folder not writable by others. The real reason is always in the server's auth log, never in the client's message.

Our run
$ chmod 777 ~lab/.ssh
$ chmod 644 authorized_keys
$ ssh ... lab@127.0.0.1
Permission denied (publickey).

StrictModes: .ssh must be 700,
  authorized_keys 600, home not
  writable by others
check /var/log/auth.log

Host keys

The server proves its identity too, with a host key. On first connection you are asked to accept its fingerprint, which should match what the console or your provisioning tool shows. It is saved in known hosts. If it ever changes, SSH shouts that the remote host identification has changed. Usually the server was rebuilt, but it could be an attack, so verify before you clear the old entry. Never turn host checking off for real servers.

known_hosts
  • first connect: "authenticity of host can't be established" → compare the fingerprint
  • saved in ~/.ssh/known_hosts
  • later change: "REMOTE HOST IDENTIFICATION HAS CHANGED" → rebuilt server, or an attack: verify, do not ignore

Config and agent

Two tools make daily SSH pleasant. The config file in dot ssh gives servers short names, with their address, user and key, so ssh shop is all you type. Settings under host star apply everywhere, like a keepalive so idle sessions are not dropped. And ssh agent holds your unlocked private key in memory, so you type the passphrase once per session instead of on every connection.

~/.ssh/config
Host shop
  HostName 203.0.113.10
  User deploy
  IdentityFile ~/.ssh/id_ed25519
Host *
  ServerAliveInterval 30
$ ssh shop
$ eval $(ssh-agent)
$ ssh-add   passphrase once

Jump hosts and tunnels

Servers in private networks are often reached through a bastion, a single hardened jump host. ssh minus capital J, or ProxyJump in the config, hops through it. Local port forwarding, minus capital L, makes a remote service appear on your laptop: here a database on the private network appears on local port five four three three, through the server. Use these carefully, and avoid forwarding your agent to hosts you do not trust, since anyone with root there can use it.

Bastion and forwarding
$ ssh -J bastion server
  (ProxyJump in config)
$ ssh -L 5433:db:5432 server
  localhost:5433 → db:5432
  through server
-R: reverse, -D: SOCKS proxy
never: agent forwarding to
  untrusted hosts

Harden sshd

On the server, a few lines harden SSH. Turn off password logins and root logins, allow only a specific group, and limit authentication attempts. Always test the file with sshd minus t before reloading, and keep a second session open while you change it, so a mistake cannot lock you out. In the cloud, many teams go further: no open port twenty two at all, using AWS Systems Manager or short-lived certificates instead, which part eight covers.

/etc/ssh/sshd_config
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
AllowGroups ssh-users
MaxAuthTries 3
$ sudo sshd -t   test config
$ sudo systemctl reload ssh
keep a second session open
  while you change it

Common mistakes

Mistakes. Copying private keys onto servers or, worse, into Git. Personal keys with no passphrase. Turning host key checking off everywhere. Leaving password login enabled. Opening port twenty two to the whole internet on every server instead of one bastion or no port at all. And keys shared between people and never rotated, so nobody knows who has access.

Avoid these
  • private keys copied to servers or committed to Git
  • no passphrase on personal keys
  • StrictHostKeyChecking=no everywhere
  • password login left on
  • port 22 open to the world on every server
  • shared keys, never rotated

Say it like this

In the interview: my public key is in the server user's authorized keys file. During login the server sends a challenge, my client signs it with my private key, and the server verifies the signature with the public key, so the private key never leaves my machine. The server proves its own identity with its host key, which my client checks against known hosts. I disable passwords and root login, and reach private servers through a bastion or Systems Manager.

In the interview
  • "The server lists my public key
  • in authorized_keys. It sends a challenge, my client signs it
  • with the private key, the server
  • verifies with the public key, so the private key never travels.
  • Host keys in known_hosts prove
  • the server is the real one."

Interview questions

Q1How does SSH public key authentication work?

The server has your public key in authorized_keys; it sends a challenge that your client signs with the private key; the server verifies with the public key. The private key never leaves your machine.

Q2Why does SSH say Permission denied (publickey) when the key is correct?

Often StrictModes: ~/.ssh or authorized_keys (or the home folder) is writable by others; .ssh should be 700 and authorized_keys 600. Check /var/log/auth.log on the server.

Q3What is known_hosts for?

It stores server host-key fingerprints so the client can detect a changed (possibly impersonated) server.

Q4What is a bastion or jump host?

A single hardened, exposed server used to reach private servers, for example with ssh -J or ProxyJump.

Q5What does ssh -L do?

Local port forwarding: a port on your machine is forwarded through the SSH server to a host and port it can reach.

Q6Which key type should you use?

ed25519: fast, small and secure; RSA only with 3072+ bits for legacy systems.

Q7How do you harden sshd?

PasswordAuthentication no, PermitRootLogin no, AllowGroups/AllowUsers, MaxAuthTries, test with sshd -t, and limit who can reach port 22 (or avoid it with SSM or certificates).

Q8What is ssh-agent?

A process that holds decrypted private keys in memory so you enter the passphrase once per session.