Logging in
How do you get a shell on a machine in another building?
ssh user@host opens an encrypted connection to sshd on port 22 and starts a shell there; a key pair replaces the password.
The server you look after is in a data centre you will never visit. How do you type commands on it? s s h connects your terminal to a program on the server, s s h d, over an encrypted link, and that program starts a shell for you. You name the account and the machine. You then have to prove who you are. A password works, but it must be typed every time, it can be guessed, and a script cannot type it. A key is a file on your computer. There is nothing to type and nothing short enough to guess, so it is what people and scripts both use.
Known hosts
Trust the server’s key once, and ssh checks it every time after
At the first connection you accept the server’s host key into known_hosts; if it ever changes, ssh refuses and warns you.
The first time you connect, ssh shows the server’s fingerprint and asks whether to trust it. Check it against the server itself, from its console. Once accepted it is saved in known hosts and checked silently every time. If it ever changes, ssh stops with a loud warning: either the server was rebuilt, or someone is in the middle. Check, then remove the old key with ssh-keygen dash capital R.
Key pairs
The private key stays with you; the public key goes everywhere
A key pair: the private key stays on your machine, the public key goes into authorized_keys on each server.
ssh-keygen makes two files: a private key, readable only by you, and a public key you can hand to anyone. ssh-copy-id puts the public key on the server, as one line in authorized_keys, in your home directory there. The fingerprint is a short summary of the key, used to check you have the right one.
TLS certificates · Going deeper · 1 of 3
A public key with a name, dates and a trusted signature
A certificate binds a public key to a name and is signed by a certificate authority the client trusts.
Key pairs · Going deeper · 2 of 3
Six steps: agree a key, check the server, then prove who you are
SSH runs a key exchange, checks the server’s host key, then you prove your identity by signing with your private key.
Here is what happens in the half second after you press enter. The client connects to port twenty-two and both sides announce their versions. They run a key exchange: each computes the same secret session key without ever sending it. From here everything is encrypted. The server signs with its host key, and your client checks it against the keys it has seen before. You offer your public key; the server finds it in authorized keys. Your client signs data unique to this session with your private key, and the server checks the signature. You are in, and no password was ever sent.
ssh -v · Going deeper · 3 of 3
The same six steps, printed by ssh -v
ssh -v prints each handshake step, so you can see where a failing login stops.
Client config
One config file turns long commands into short names
~/.ssh/config stores the address, user, port, key and jump host per name; ssh-agent holds your unlocked key.
Every option you type on the ssh command line can live in your config file, under a host name. The long command with a port, a key and an address becomes ssh web01, and scp, rsync and git use the same entry. ProxyJump reaches a private machine through a bastion, and Host star sets defaults for everything else.
Copying files
rsync sends only what changed; scp sends everything
scp copies whole files; rsync compares both sides and sends only differences, over the same SSH connection.
The first rsync sends every file. Change one file and run it again: only that file travels, eight hundred bytes instead of eight thousand. scp is simpler and copies everything every time. rsync dash a keeps owners, permissions and times; dash dash delete mirrors deletions; dash n shows what would happen. And watch the trailing slash: with it, the contents are copied; without it, the folder itself.
Copying files · Going deeper
Carry another connection inside SSH, through the firewall
A local forward (-L) opens a port on your machine that leads, through the server, to a host only it can reach.
The database is on a private network; only web01 can reach it. ssh dash L opens port fifty-four thirty-three on your laptop. Whatever connects there travels inside the SSH connection to web01, which passes it on to the database. Dash R does the reverse, opening a port on the server that leads back to you. Dash D turns the connection into a proxy for a browser.
Hardening
No root, no passwords, and only the people you name
Set PermitRootLogin no, PasswordAuthentication no and AllowGroups in a drop-in; test with sshd -t before reloading.
Three lines do most of the work. No direct root logins, keys only, and only members of one group may log in. Test the file with sshd dash t, then reload, and keep your current session open until a new login works. Moving SSH to another port on RHEL also needs SELinux and the firewall told about it.
Lab
Key login, file copies, a tunnel and a hardened server
Generate and install a key, name the server in your config, copy with scp and rsync, tunnel a port, then turn off password and root logins.
In this lab you set up key login between two machines, use it for copies and a tunnel, and then lock the server down so only keys work. The last check is a refusal: password login must fail. Press the down arrow for the steps.
Lab · Going deeper
Key login, file copies, a tunnel and a hardened server
Use two machines: a client (your laptop or a VM) and a server VM you can also reach by console. Keep the console open until the last step works.
Use two machines: a client (your laptop or a VM) and a server VM you can also reach by console. Keep the console open until the last step works. Steps. Make a key. ssh-keygen -t ed25519 -C "<you>@<machine>" and give it a passphrase. ls -l ~/.ssh: the private key must be -rw-------. Install it. ssh-copy-id <user>@<server>. On the server, check ~/.ssh is 700 and authorized_keys 600, owned by you. Check the host key. On the server console: ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub. Compare it with what the client saw at first login. Name it. Write a Host entry in ~/.ssh/config so that ssh <name> works. Add the key to the agent: eval "$(ssh-agent)"; ssh-add. Copy. scp a file each way. rsync -av a folder, change one file, rsync again, and compare the bytes sent. Try --delete with -n first. Tunnel. On the server, run python3 -m http.server 8000 --bind 127.0.0.1. From the client: ssh -N -L 8001:localhost:8000 <name>, then curl localhost:8001. Harden. Create /etc/ssh/sshd_config.d/50-hardening.conf with PermitRootLogin no and PasswordAuthentication no. sshd -t; systemctl reload sshd (ssh on Ubuntu). Prove it. From a second terminal: ssh <name> works; ssh -o PubkeyAuthentication=no <name> is refused; ssh root@<server> is refused. journalctl -u sshd shows both refusals. Make a certificate. openssl req -x509 -newkey rsa:2048 -nodes -days 30 -keyout web.key -out web.crt -subj "/CN=<server>"; openssl x509 -in web.crt -noout -dates. Check: ssh <name> logs in without a password prompt (the agent holds the passphrase), the tunnelled curl returns a directory listing, and password and root logins are both refused.
Steps
- Make a key. ssh-keygen -t ed25519 -C "<you>@<machine>" and give it a passphrase. ls -l ~/.ssh: the private key must be -rw-------.
- Install it. ssh-copy-id <user>@<server>. On the server, check ~/.ssh is 700 and authorized_keys 600, owned by you.
- Check the host key. On the server console: ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub. Compare it with what the client saw at first login.
- Name it. Write a Host entry in ~/.ssh/config so that ssh <name> works. Add the key to the agent: eval "$(ssh-agent)"; ssh-add.
- Copy. scp a file each way. rsync -av a folder, change one file, rsync again, and compare the bytes sent. Try --delete with -n first.
- Tunnel. On the server, run python3 -m http.server 8000 --bind 127.0.0.1. From the client: ssh -N -L 8001:localhost:8000 <name>, then curl localhost:8001.
- Harden. Create /etc/ssh/sshd_config.d/50-hardening.conf with PermitRootLogin no and PasswordAuthentication no. sshd -t; systemctl reload sshd (ssh on Ubuntu).
- Prove it. From a second terminal: ssh <name> works; ssh -o PubkeyAuthentication=no <name> is refused; ssh root@<server> is refused. journalctl -u sshd shows both refusals.
- Make a certificate. openssl req -x509 -newkey rsa:2048 -nodes -days 30 -keyout web.key -out web.crt -subj "/CN=<server>"; openssl x509 -in web.crt -noout -dates.
Lesson complete
You finished “SSH and remote work”
Nicely done.