task 6.1 · chapter 6 of 8: ssh and keys · 25 min
Your first key
objective
Log in to ws1 over SSH with a password, and check its host key before you trust it. Then make a key pair of your own, put the public half on ws1 with ssh-copy-id, and log in with no password at all.
answers/6.1.yml~/.ssh/open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | SSH and keys |
|---|---|
| ws1 | user netops, password netops-lab |
| ws1's host key, to compare | click ws1 on the Topology tab, then ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub |
| Your answers | answers/6.1.yml |
new to this? step by stepbeginner
ssh-keygen -R ws1, thenssh netops@ws1. Compare the fingerprint it shows with ws1’s own (see given), then typeyes, and the passwordnetops-lab.exit.ssh-keygen -t ed25519: press Enter three times.ssh-copy-id netops@ws1: the password one last time.ssh netops@ws1: no password.exit.- Fill in answers/6.1.yml, then
make check T=6.1.
more in meet the lab
Two kinds of key
SSH checks both ends
SSH (Secure Shell) gives you a command line on another machine, encrypted all the way. Every login proves two things:
- The server is who it says it is. Every SSH server has a host key. The first time you connect, ssh
shows you its fingerprint, a short summary of the key, and asks whether to trust it. If you say yes, it
remembers the key in
~/.ssh/known_hosts, and checks it on every login after that. - You are who you say you are. With a password, or with a key pair of your own.
in one sentence The host key proves the server to you; a password or your own key proves you to the server.
Your own key pair
A key pair is two files that belong together:
| File | Who gets it |
|---|---|
~/.ssh/id_ed25519 | the private key: it never leaves your machine, and only you can read it |
~/.ssh/id_ed25519.pub | the public key: hand it to any server you want to log in to |
A server that has your public key in its ~/.ssh/authorized_keys lets in whoever proves they hold the private
key, without the private key ever crossing the network. ssh-copy-id does the handing over: it logs in once
with your password and appends your public key to the server’s authorized_keys.
This is how Ansible, Git and scripts log in to things: there’s no one there to type a password.
in one sentence The private key stays with you; the public key goes into the server’s authorized_keys.
The first time you meet ws1
$ ssh netops@ws1
The authenticity of host 'ws1 (172.80.102.11)' can't be established.
ED25519 key fingerprint is SHA256:kjuhGNNG83Kh3Enr7x2BUZ+WvNjsbq0zOHZ8ZC5f9Xo.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Your fingerprint is different: every lab builds its own ws1. ssh can’t know whether this is really ws1 or something pretending to be it; only you can, by checking the fingerprint against one you got another way. On a real network that’s the server’s admin, or its console. Here, ask ws1 itself, without SSH: on the lab app’s Topology tab, click ws1. That opens a root shell on it, in a tab of its own, like sitting at its console:
root@ws1:~# ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
256 SHA256:kjuhGNNG83Kh3Enr7x2BUZ+WvNjsbq0zOHZ8ZC5f9Xo root@buildkitsandbox (ED25519)
The same fingerprint: it’s safe to say yes. (buildkitsandbox is just a label from when the image was
built.)
Password, then key
Check ws1’s fingerprint from the inside first: on the Topology tab, click ws1, and in the ws1 tab:
run in the ws1 tabssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pubKeep that tab open, and go back to the VS Code tab’s terminal for the rest.
You said
yesto ws1’s key in task 4.2, so ssh remembers it and won’t ask again. Make it forget, so you meet ws1 the way you’d meet a new server, then connect. Compare the fingerprint ssh shows with the one above. Same? Typeyes, then the passwordnetops-lab(nothing appears while you type it; that’s normal):run in the terminalssh-keygen -R ws1 ssh netops@ws1ssh-keygen -R ws1prints # Host ws1 found and keeps the old file asknown_hosts.old. If it finds nothing, that’s fine too: you skipped 4.2’s login.The prompt becomes
netops@ws1:~$. Copy the fingerprint, startingSHA256:, intows1_fingerprint. Thenexit.Make your key pair.
-t ed25519picks the key type; the codespace’s ssh is older and would make an RSA key without it. Press Enter at each question to take the default file and no passphrase:run in the terminalssh-keygen -t ed25519 ls -l ~/.sshid_ed25519is-rw-------, private;id_ed25519.pubis-rw-r--r--.Hand the public key to ws1. It asks for the password one last time:
run in the terminalssh-copy-id netops@ws1Log in again:
run in the terminalssh netops@ws1 cat ~/.ssh/authorized_keys exitNo password. And there, on ws1, is your public key, the same line as
cat ~/.ssh/id_ed25519.pubin the codespace.
in one sentence ssh-keygen makes the pair, ssh-copy-id installs the public half, and the password is never needed again.
Run something without logging in
Nothing more for the checker in this one: the follow-along is the task. But try what a key makes possible. ssh runs one command and comes straight back when you put it after the host:
ssh netops@ws1 hostname
ssh netops@ws1 'uptime; df -h /'
This is exactly what Ansible does for every task on every host. Then run make check T=6.1.
hint
Asked for a password again? ssh-copy-id didn’t finish. Run it again, then check ~/.ssh/authorized_keys on ws1.
hint
Don’t make a passphrase-less key for a real account without thinking about it: anyone who copies the file is you. The lesson covers passphrases and ssh-agent.
show the solution
$ ssh netops@ws1 hostname
ws1
The quotes in the second command matter: without them, your own shell would see the ; and run df -h / in
the codespace instead.
5 check: run it, then prove it
$ make check T=6.1when it's right, you'll see
╭─ task 6.1 · your first key
│ ✓ ws1's host key fingerprint (follow along): SHA256:...
│ ✓ ws1 is in your known_hosts (follow along)
│ ✓ you have an ed25519 key pair in ~/.ssh (follow along)
│ ✓ your public key is in netops's authorized_keys on ws1 (follow along)
│ ✓ ssh netops@ws1 logs in without a password (follow along)
╰─ 5/5 checks passed verified