Learn / Linux Fundamentals / Accounts and Access
SSH and Keys
First Bit is still up for grabs: be the first to finish this room.
Almost every Linux machine you'll ever touch, you'll touch over SSH. You can log in with a password, but nearly nobody should: a password can be guessed, reused, phished or tried a million times a night by people who have never heard of you. A key pair can't be any of those things.
In this room you'll learn how the two halves of a key pair fit together, what authorized_keys really controls, and why ~/.ssh is fussy about its permissions. Then you'll use the key Jonah has been given to collect a handover note from an account he can't otherwise reach.
CYB//RANGE Live lab
Log in to launch this room's lab.
Task 1 Two halves of a key
A key pair is two matching files, made together:
$ ssh-keygen -t ed25519 -C "jonah@laptop"
$ ls ~/.ssh
config id_ed25519 id_ed25519.pub
The one ending .pub is the public key. You hand it out, paste it into tickets, copy it to servers. The other one is the private key, and it never leaves the machine it was made on. Nothing you do should ever send it anywhere, and a server never needs it.
Logging in works because the server can set a puzzle only the private key can answer, without the private key ever crossing the network. That's the whole reason keys beat passwords: a password has to be sent to be checked, and anything you send can be captured.
The server side is one file, ~/.ssh/authorized_keys in the account you're logging into. Every public key listed there may log in as that account. Nothing else is needed, and nothing else is checked, which is exactly why an unexpected line in that file matters so much.
A line can also be fenced in, by putting options in front of the key:
restrict,command="/usr/local/bin/handover" ssh-ed25519 AAAAC3Nza... jonah@laptop
Now that key can do one thing. Whatever the person asks to run, sshd ignores it and runs the named command instead, and restrict turns off everything else a session could ask for: no terminal, no port forwarding, no agent forwarding. Backups, deploys and file drops are given keys like this so an automated job can do its one task and nothing more.
Two last details, both of which will bite you:
| File | Mode |
|---|---|
~/.ssh |
700 |
| private key | 600 |
authorized_keys |
600 |
ssh enforces these rather than warning about them: it refuses to use a private key that other people
on the machine can read, and sshd ignores an authorized_keys that anyone but its owner can write.
And ssh checks the server's identity too. The first connection asks you to accept its host key and remembers it in ~/.ssh/known_hosts; if it ever changes, ssh refuses loudly rather than quietly connecting to whatever is now answering.
Which half of a key pair do you copy to a server? (One word.)
Log in or sign up to answer. 5 pts
******
Which file in an account lists the keys allowed to log in as it? (Just the name, no folder.)
Log in or sign up to answer. 10 pts
***************
Task 2 Connecting
The command is ssh user@host, and -i picks which key to offer:
$ ssh courier@localhost
$ ssh -i ~/.ssh/courier_key courier@localhost
$ ssh -i ~/.ssh/courier_key courier@localhost 'uptime'
Put a command on the end and ssh runs just that and comes straight back, which is how scripts use it.
Typing all that every time gets old, so ~/.ssh/config names the details once:
Host courier
HostName localhost
User courier
IdentityFile ~/.ssh/courier_key
Now ssh courier is enough. The Host line is a label you invent, not a hostname, and reading somebody's ~/.ssh/config is one of the faster ways to learn which machines they work on.
When a connection is refused, work down the layers rather than guessing. ssh: connect to host ... port 22: Connection refused means nothing is listening, so the problem is the server, not your key. Permission denied (publickey) means sshd is running and didn't accept what you offered, which is a question about authorized_keys or which key you sent. ssh -v prints the keys it tried, in order, and is usually quicker than thinking about it.
Task 3 Try it on a real machine
The person Jonah is replacing left a handover note in the courier account. He can't log into it and can't read its home folder, but he was given a key.
- Press Launch in CybRange at the top of this page, then Open terminal next to
boxonce it's ready. You're logged in asjonah. - Read
~/todo.txt, then look at what you were given:ls -l ~/.sshandcat ~/.ssh/config. - Try it the direct way first:
cat /home/courier/handover.txt. It fails, andls -ld /home/couriersays why. - Connect with the key. The config file means
ssh courieris enough. - You'll notice you don't get a shell. Try
ssh courier 'id'and see what comes back instead, then work out from the first task where that behaviour is written down.
What are /home/courier's permissions, as the nine characters ls -ld shows after the d?
Log in or sign up to answer. 10 pts
*********