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.
- 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.
$ 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.
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.
$ 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.
- 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.
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.
$ 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.
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.
- 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.
- "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."