TT Lab
Blog Get started
Learn Learning paths Courses

SSH and File Transfer

Setting Up Key Authentication

Continue in TT Lab

Goal

You create an SSH key, set its permissions, register it in authorized_keys, and actually connect. You confirm that a key with a command= restriction really runs only that command, and at the end you take three bots that give the same Permission denied (publickey) for different reasons, tell them apart with the server log, and fix them.

Why it matters

Most public key authentication failures are not cryptography but a matter of file permissions, file format, or restrictions placed on the key. Yet what the client sees is always the same one line, so the reason lies only in the log of the side that refused (the server) and that account's files. If others can write to ~/.ssh, sshd does not trust the keys under it, because someone else could put their own key there. That is, permissions are the authentication policy, which is why the prescription "just turn off StrictModes" opens the door as it removes the symptom. command= and from= restrictions are effectively essential in deployment automation. Even if a CI server's key leaks, that key runs only one set command from one set place.

In this lab environment, a real sshd is running on 127.0.0.1:2222. It is in the same Pod, but from SSH's point of view it is a complete second server. The incident step starts its own server (127.0.0.1:2231), and that server writes its log to /var/log/ss-lab/keyhub.log.

Steps

  1. Create an ed25519 key pair at /root/.ssh/id_labhub with no passphrase.
  2. Temporarily loosen /root/.ssh/id_labhub to permissions others can read (644), and try extracting the public key again from that private key. Save the line of the tool's refusal message that contains the permission number and the key path to /root/ssh/unprotected.txt, then set /root/.ssh to 700, /root/.ssh/id_labhub to 600, and /root/.ssh/id_labhub.pub to 644.
  3. Add the public key you just made to /root/.ssh/authorized_keys. The file permissions must be 600.
  4. With ssh-keyscan, fetch the host key of 127.0.0.1 on port 2222 and register it in /root/.ssh/known_hosts.
  5. Connect to root@127.0.0.1:2222 with that key, run hostname remotely, and save the result to /root/ssh/connect.txt.
  6. Create the alias lab2 in /root/.ssh/config. Connecting as lab2 must go to port 2222 of 127.0.0.1 as root, offering only the key /root/.ssh/id_labhub; this must hold for the values that actually apply, not just the values written in the file. Then save the result of running hostname with only the alias to /root/ssh/alias.txt.
  7. Create a second ed25519 key at /root/.ssh/id_restricted, and add its public key to authorized_keys, putting the option restrict,command="/bin/hostname" at the very start of the line. When you connect with that key and request id, the result of hostname must come out, not id. Save that output to /root/ssh/restricted.txt.
  8. Create /root/ssh/keys.txt. It must be the list of fingerprints of all the keys currently registered for the root account that the server accepts (at least 2 lines).
  9. Incident: incident ss-key-denied. Three bots all fail to connect to keyhub with Permission denied (publickey). Leave the server configuration and the restrictions on the keys as they are, get all three connecting again, and for each bot write a cause code and one line of evidence to /root/incident/ss-key-denied/report.txt.

Notes

Creating a key pair

Create an ed25519 key pair at /root/.ssh/id_labhub with no passphrase.

With ssh-keygen, -t specifies the type, -f the output path, and -N the passphrase. Leave the passphrase empty for automatic grading.

Setting permissions: seeing the refusal yourself

Temporarily loosen /root/.ssh/id_labhub to permissions others can read (644), and try extracting the public key again from that private key. The tool checks permissions the moment it opens a private key, and refuses. Save the line of that refusal message that contains the permission number and the key path to /root/ssh/unprotected.txt. Then set /root/.ssh to 700, /root/.ssh/id_labhub to 600, and /root/.ssh/id_labhub.pub to 644.

Tools that open private keys (ssh, and the key tool that extracts a public key again from a private key) check permissions before opening. The refusal message comes out on standard error, so capture it as well and filter it. The recommended permissions for the directory, the private key, and the public key differ, and only the public key may be readable by others.

Registering the public key

Add the public key you just made to /root/.ssh/authorized_keys. The file permissions must be 600.

authorized_keys is a text file that holds one public key per line. Do not delete the existing lines; append.

Registering the host key

With ssh-keyscan, fetch the host key of 127.0.0.1 on port 2222 and register it in /root/.ssh/known_hosts.

ssh-keyscan has an option to specify the port, and a non-standard port goes into known_hosts in square-bracket notation.

Connecting with the key

Connect to root@127.0.0.1:2222 with that key, run hostname remotely, and save the result to /root/ssh/connect.txt.

Specify the key with -i and the port with -p. If you attach the command to run remotely as an argument, it runs right after connecting.

Making an alias with client configuration

Create the alias lab2 in /root/.ssh/config. Connecting as lab2 must go to port 2222 of 127.0.0.1 as root, offering only the key /root/.ssh/id_labhub (without mixing in the agent or default keys). If another block sets the same value first, that one wins, so this must hold for the values that actually apply. Then save the result of running hostname with only the alias to /root/ssh/alias.txt.

One Host block is one alias. Put in the address, port, user, key file, and the directive that makes it use only the specified key, and you connect with just the alias and no options. Do not read the file by eye; check with ssh's option that shows the actually applied values (-G).

Making a command-restricted key

Create a second ed25519 key at /root/.ssh/id_restricted, and add its public key to authorized_keys, putting the option restrict,command="/bin/hostname" at the very start of the line. When you connect to root@127.0.0.1:2222 with that key and request id, the result of hostname must come out, not id. Save that output to /root/ssh/restricted.txt.

List the options, separated by commas, at the very start of the authorized_keys line. Turn everything off with restrict and allow only one thing with command.

Auditing the registered keys

Create /root/ssh/keys.txt. It must be the list of fingerprints of all the keys currently registered for the root account that the server accepts (at least 2 lines).

The same tool that made the keys shows the fingerprints of several keys in one file at once (it strips the options at the start of lines on its own). Write fingerprints, not the public key text.

Incident: three bots get the same Permission denied

Running incident ss-key-denied shows a ticket. Three bots (deploy, archiver, monitor) that used to connect with keys to the keyhub server (127.0.0.1:2231) all fail with Permission denied (publickey). Fix it so all three connect again. You must not change the server configuration (/etc/ss-lab/keyhub/sshd_config), and the restrictions on the key lines (restrict, forced commands, source restrictions) are policy and must remain. In /root/incident/ss-key-denied/report.txt, write one line per bot, such as deploy: <코드> (code), plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Candidate codes: account-locked (the account is locked), from-restrict (caught by the source restriction on the key line), key-format (the key line in authorized_keys is broken), strict-modes (the permissions or owner of the home, .ssh, or authorized_keys are loose, so sshd does not trust them), wrong-key (the client offers a different, unregistered key).

Direction: the client's one line is identical for all three, so start from where the refusing side (the server) leaves its reason. A bot with no reason left behind is one whose key line the server could not read at all. Tools: ss-bots (reproduce the symptom), the server log /var/log/ss-lab/keyhub.log, stat and namei -l, cat -A, ssh-keygen -lf. Concretely: look at the "Authentication refused" and "not from a permitted host" lines in the log, the permissions, owner, and group of each account's home, .ssh, and authorized_keys, how many lines authorized_keys has and where each line ends, and whether the bot key's fingerprint matches the registered fingerprint.