Objective: Replace password logins with SSH key logins. Create a key pair on your own computer, use an agent so you only type your passphrase once, log in to your development and production servers without a password, start an agent on a Linux server, and finally harden both servers by turning password logins off.
Since Lab 1 you have logged in to your servers by typing a password. Passwords are easy to understand, but they can be guessed, reused, phished or watched over your shoulder, and you have to type one every time you connect. Every server on the internet that accepts SSH passwords is attacked by password guessing bots all day long.
SSH keys fix this. You create a pair of keys: a private key that never leaves your computer, and a public key that you copy to every server you want to log in to. When you connect, the server checks that you hold the private key that matches, without the private key ever being sent across the network.
How you do this depends on your computer:
| Your computer | Tools you will use |
|---|---|
| Windows | PuTTYgen to create keys, Pageant (the PuTTY agent) to hold them, and PuTTY to connect. |
| Mac | Terminal, with ssh-keygen and ssh-add. macOS starts an ssh-agent for you. |
| Linux laptop | The same as Mac. Most Linux desktops also start an agent for you. |
Everyone does the Linux server part (Part 6), because sooner or later you will need to connect from one server to another.
Note: This lab is done on your development server, except for Parts 5 to 7, where you also add your key to your production server and turn off its password logins. Connect with SSH from Terminal or PuTTY and log in as your own user. Keep that session open for the whole lab. It is your safety net if a key login doesn't work.
Some steps give you every command. Others give you a Goal and ask you to work out the commands yourself. If you get stuck, open the hint. Once you have an answer, or if you are really stuck, open the solution and compare it with your own.
Windows users do Part 2 and skip Part 3. Mac and Linux users skip Part 2 and do Part 3. Everyone does Parts 1 and 4 to 7.
If you get stuck, check the Troubleshooting section at the end of this lab.
| Piece | Where it lives | What it does |
|---|---|---|
| Private key | Your computer only. | Proves who you are. Anyone who has a copy can log in as you, so you never share it, email it or copy it to a server. |
| Passphrase (optional) | Your head. | Encrypts the private key file, so a stolen copy is useless without it. It is not sent to the server. |
| Public key | Every server you want to log in to. | Lets the server check that you hold the matching private key. It is safe to share. Anyone who has it can only let you in. |
authorized_keys |
~/.ssh/authorized_keys on the server. |
A list of public keys, one per line. Any matching private key may log in as this user. |
| Agent | A program on your computer: Pageant on Windows, ssh-agent on Mac and Linux. | Holds your unlocked private key in memory, so you type the passphrase once instead of on every connection. |
~/.ssh/authorized_keys for the user you are logging in as.Note: You have already met the other key pair SSH uses. In Lab 2, the first time you connected, your client asked you to accept the server's host key. That key proves the server is who it says it is. The key pair in this lab proves you are who you say you are. Each side checks the other.
From now on, pay attention to which question you are being asked. They look alike but mean very different things:
| Prompt | Who is asking | What it means |
|---|---|---|
username@127.0.0.1's password: |
The server | Key login did not work. The server has fallen back to a password. |
Enter passphrase for key '/Users/you/.ssh/id_ed25519': |
Your own computer | Your key is being used, but it is not loaded in an agent, so it needs unlocking. |
Passphrase for key "yourname-laptop": (PuTTY) |
Your own computer | The same thing in PuTTY. |
If you see a password prompt after setting up your key, something is wrong. See Troubleshooting. You may Choose to omit a passphrase from your SSH private key. This is an administrative trade-off between convenience and security. While it simplifies automation, it increases your risk profile and you should consdier your environment's exposure and determine the level of risk you are willing to accept.
You will often hear people call this "certificate" based authentication. In this lab we use plain keys: each server trusts each public key you copy to it, one at a time. That works well for a few servers, but imagine 500 servers and 50 administrators who come and go.
SSH also supports real certificates, where a trusted certificate authority (CA) signs your public key, and servers trust anything that CA has signed. You will learn how certificate authorities work when we set up TLS for web sites later in the course. The key pair you create today is the same kind of key a CA would sign.
Mac and Linux users: Skip to Part 3.
In Lab 1 you may have downloaded only putty.exe. For this lab you also need PuTTYgen and Pageant. The easiest way to get all three is the installer.

Note: If your PuTTY is older than version 0.75, update it now. Older versions sign with an algorithm (SHA-1) that RHEL 9 no longer accepts, and your key will be refused. In PuTTY, right click the title bar and choose About PuTTY to check.
Launch PuTTYgen.
Under Parameters, set Type of key to generate to EdDSA, and check that the curve is Ed25519.
Click Generate, and move your mouse around the blank area until the progress bar fills. PuTTYgen uses your mouse movement as a source of randomness.

In Key comment, replace the default with something that says which computer this key is on, for example yourname-windows-laptop. The comment is added to the end of your public key, and it helps you find this key on a server later.
In Key passphrase and Confirm passphrase, enter a passphrase you will remember. There is no way to reset it. If you forget it, you have to create a new key pair (this step is optional).
Click Save private key. Save it somewhere you will find it, such as your Documents folder, as itfdn140-key.ppk.
Click Save public key and save it in the same place as itfdn140-key.pub.
Leave PuTTYgen open. You need the text in the box at the top next.

Note: This interface shows creating an Ed25519 key, which is shorter, faster, and the modern standard for OpenSSH. You may however still encounter older, traditional key types like RSA or DSA in legacy environments, but Ed25519 offers superior security and performance.
The box at the top of PuTTYgen, labeled Public key for pasting into OpenSSH authorized_keys file, holds your public key as one line. It starts with ssh-ed25519.
You will build the .ssh directory and the authorized_keys file yourself, and set their permissions with what you learned in Lab 6. Run ls -l after each step and watch the permissions change.
Step 1: Look at your home directory, then create .ssh in it. You may get File exists from mkdir, which is fine:
cd
ls -ld ~
mkdir ~/.ssh
ls -ld ~/.ssh
Question: What permissions did
mkdirgive.ssh? Who else on the server could list or enter it?
Step 2: Make .ssh private, so only you can use it:
chmod 700 ~/.ssh
ls -ld ~/.ssh
It should now show drwx------.
Step 3: Add your public key to the end of authorized_keys with a redirect from Lab 5. Type echo ', right click to paste your key, then type ' >> ~/.ssh/authorized_keys and press Enter. The whole command is one line, and looks like this:
echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...your key... yourname-windows-laptop' >> ~/.ssh/authorized_keys
The file does not exist yet, so >> creates it.
Warning: Use
>>, not>. Remember Lab 5:>>adds a line to the end of the file, while>replaces the whole file. Onceauthorized_keysholds several keys, one>deletes them all, and could lock you out.
Step 4: Look at the file you created, then make sure only you can write to the file:
ls -l ~/.ssh/authorized_keys
chmod 644 ~/.ssh/authorized_keys
ls -l ~/.ssh/authorized_keys
Question: What permissions did the new file start with? Why might you not want other users to be able to write to the list of keys that can log in as you? This file can be more restrictive removing read permissions for group and other.
Step 5: Check your work:
ls -la ~/.ssh
wc -l ~/.ssh/authorized_keys
cat ~/.ssh/authorized_keys
ssh-keygen -l -f ~/.ssh/authorized_keys
wc -l should print 1: one key, on one line. The line should start with ssh-ed25519 and end with your key comment. ssh-keygen -l prints the key's fingerprint, which you will use in Part 4. If it prints is not a public key file instead, the line is damaged. Remove the file with rm ~/.ssh/authorized_keys and repeat Step 3.
Goal: You saved a public key file, itfdn140-key.pub, but you copied the text from the PuTTYgen window instead. Open itfdn140-key.pub in Notepad and compare it with the line in your authorized_keys file. Why didn't we use the file?
Count the lines in each. Look at the first and last lines of the .pub file. Then look at what wc -l ~/.ssh/authorized_keys told you about how a key must be stored.
The .pub file saved by PuTTYgen is in the SSH2 (RFC 4716) format. It looks like this, spread over several lines:
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "yourname-windows-laptop"
AAAAC3NzaC1lZDI1NTE5AAAAI...
---- END SSH2 PUBLIC KEY ----
The OpenSSH server on Linux expects each key in authorized_keys to be on one line, in the form type key comment:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... yourname-windows-laptop
It is the same key in two different formats. The PuTTYgen window shows it in the OpenSSH format, ready to paste.
If you ever need your public key again, you don't need the .pub file at all. Open PuTTYgen, click Load, choose your .ppk private key, and the public key appears in the box again. A public key can always be worked out from its private key, but never the other way around.
Without an agent, PuTTY would ask for your passphrase every time you connect. Pageant unlocks your key once and keeps it ready.
Start Pageant from the PuTTY folder in the Start menu. Nothing opens, but a small computer-with-a-hat icon appears in the system tray, at the bottom right of your screen. You may need to click the ^ arrow to see it.

Right click the Pageant icon and choose Add Key.
Choose your itfdn140-key.ppk file, and enter your passphrase.

Right click the Pageant icon again and choose View Keys. You should see your key listed, with your key comment at the end.
Tip: You can also double click a
.ppkfile. Windows starts Pageant if it isn't running, and asks for your passphrase.
Note: Pageant only keeps keys in memory. When you exit Pageant, log out, or restart your computer, it forgets them, and you have to add your key again. That is a feature: your unlocked key does not sit on your disk.
You should be logged in without a password. PuTTY shows which key it used:
Using username "yourname".
Authenticating with public key "key-name" from agent

If PuTTY asks for a password, something is wrong. Close that window and see Troubleshooting. Your first PuTTY session is still logged in, so you can fix it from there.
Note: You can also point PuTTY straight at your
.ppkfile under Connection -> SSH -> Auth -> Credentials, without using Pageant. It works, but PuTTY asks for your passphrase every time you connect. The agent asks once.
Note: Windows 10 and 11 also include OpenSSH, which you can use from Windows Terminal or PowerShell. It works much like the Mac steps in Part 3, but its agent service is turned off by default. For this course, use PuTTY and Pageant.
Continue to Part 4: Prove you used a key.
Windows users: Skip to Part 4.
All the commands in this part run in a Terminal on your own computer, not on your server. Open a new Terminal window with Command + N so that your SSH session to your development server stays open. Check where you are before each command. Your laptop's prompt looks different from your server's.
You may already have a key pair, for example one you created for GitHub or for work:
ls -l ~/.ssh

| If you see | Then |
|---|---|
No such file or directory, or no id_ files |
You have no keys yet. Continue to the next step. |
id_ed25519 and id_ed25519.pub |
You already have an Ed25519 key pair. You can use it, and skip to Check your agent. |
id_rsa and id_rsa.pub |
You have an older RSA key pair. It still works. You can use it, or create an Ed25519 key as well. Both can live side by side. |
Warning: If
ssh-keygenasks Overwrite (y/n)?, answer n. Overwriting a key you already use for something else, such as GitHub or a work server, locks you out of everything that key was letting you in to. There is no undo.
ssh-keygen -t ed25519 -C "yourname-mac"
| Option | What it does |
|---|---|
-t ed25519 |
The type of key. Ed25519 is short, fast and the current default for OpenSSH. |
-C "..." |
A comment added to the end of your public key. Use one that says which computer the key is on. |
ssh-keygen asks three questions:
~/.ssh/id_ed25519.
Look at what was created:
ls -l ~/.ssh
cat ~/.ssh/id_ed25519.pub
| File | Permissions | What it is |
|---|---|---|
id_ed25519 |
-rw------- (600) |
Your private key. Only you can read it. Never copy it to a server or cat it in front of anyone. |
id_ed25519.pub |
-rw-r--r-- (644) |
Your public key. One line, starting with ssh-ed25519. This is the part you copy to servers. |
These are the same numbers you met in Lab 6. ssh-keygen sets them for you.
macOS starts an ssh-agent for you when you log in, and most Linux desktops do too. Check that your Terminal can find it:
echo $SSH_AUTH_SOCK
ssh-add -l
Output of ssh-add -l |
Meaning |
|---|---|
| The agent has no identities. | The agent is running, but empty. This is what you expect now. |
| A line with a fingerprint and your comment | Your key is already loaded. |
| Could not open a connection to your authentication agent. | No agent is running. echo $SSH_AUTH_SOCK prints a blank line. Start one with eval "$(ssh-agent -s)", which you will learn about in Part 6. |
SSH_AUTH_SOCK is a shell variable holding the location of a socket, a special file that programs use to talk to the agent. ssh and ssh-add find the agent through it.
ssh-add ~/.ssh/id_ed25519
ssh-add -l
ssh-add asks for your passphrase once, and ssh-add -l now lists your key's fingerprint and comment. If you used an RSA key instead, use ~/.ssh/id_rsa.
Tip: The agent forgets your key when you restart. Run
ssh-addagain after each restart, or add the lineAddKeysToAgent yesto a~/.ssh/configfile on your laptop (see the tip at the end of this part). Thensshloads your key into the agent the first time you use it, asking for your passphrase once. This works the same way on Mac and Linux. It is optional. Mac users who want macOS to remember the passphrase across restarts can see Appendix: Mac Keychain options for ssh-add.
On your laptop, print your public key:
cat ~/.ssh/id_ed25519.pub
Select the whole line, from ssh-ed25519 to your comment at the end, and press Command + C to copy it. Make sure you copy the .pub file. Never copy your private key.
Now switch to your SSH session on your development server, and build ~/.ssh/authorized_keys yourself. Run ls -l after each step and watch the permissions change.
Step 1: Look at your home directory, then create .ssh in it. You may get File exists from mkdir, which is fine:
cd
ls -ld ~
mkdir ~/.ssh
ls -ld ~/.ssh
Question: What permissions did
mkdirgive.ssh? Who else on the server could list or enter it?
Step 2: Make .ssh private, so only you can use it:
chmod 700 ~/.ssh
ls -ld ~/.ssh
It should now show drwx------.
Step 3: Add your public key to the end of authorized_keys with a redirect from Lab 5. Type echo ', press Command + V to paste your key, then type ' >> ~/.ssh/authorized_keys and press Enter. The whole command is one line, and looks like this:
echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...your key... yourname-mac' >> ~/.ssh/authorized_keys
The file does not exist yet, so >> creates it.
Warning: Use
>>, not>. Remember Lab 5:>>adds a line to the end of the file, while>replaces the whole file. Onceauthorized_keysholds several keys, one>deletes them all, and could lock you out.
Step 4: Look at the file you created, then make it private:
ls -l ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
ls -l ~/.ssh/authorized_keys
Question: What permissions did the new file start with? Why might you not want other users to be able to read the list of keys that can log in as you?
Step 5: Check your work:
ls -la ~/.ssh
wc -l ~/.ssh/authorized_keys
cat ~/.ssh/authorized_keys
ssh-keygen -l -f ~/.ssh/authorized_keys
wc -l should print 1: one key, on one line. The line should be the same as your id_ed25519.pub file on your laptop. ssh-keygen -l prints the key's fingerprint, which you will use in Part 4. If it prints is not a public key file instead, the line is damaged. Remove the file with rm ~/.ssh/authorized_keys and repeat Step 3.
Run the same ssh command you have used since Lab 2:
# VirtualBox
ssh -p 2222 username@127.0.0.1
# UTM
ssh username@192.168.64.5
You should be logged in without being asked for a password or a passphrase. If you are asked for a password, see Troubleshooting.
Tip: Tired of typing the port and IP address? Create a file named
~/.ssh/configon your laptop with these lines, using your own values. This is the Terminal version of a PuTTY saved session.Host dev01 HostName 127.0.0.1 Port 2222 User usernameNow
ssh dev01is all you need to type. UTM users setHostNameto their VM's IP address and leave out thePortline.
You logged in without a password, but how do you know that a key was used? The server writes every login to /var/log/secure, which you read in Lab 2.
On your development server:
sudo grep Accepted /var/log/secure | tail -n 5
Your older logins say Accepted password. Your newest one says Accepted publickey, followed by the key type and a fingerprint:
sshd[1534]: Accepted password for yourname from 10.0.2.2 port 51034 ssh2
sshd[2210]: Accepted publickey for yourname from 10.0.2.2 port 51211 ssh2: ED25519 SHA256:Qm3k...
A fingerprint is a short summary of a key. Two keys with the same fingerprint are the same key. Fingerprints are much easier to compare than the whole key.
Goal: Show that the key the server accepted is the key in your agent. Compare three fingerprints:
/var/log/secure.authorized_keys file.ssh-keygen -l -f file prints the fingerprint of every key in a file.ssh-add -l from Part 3 lists the fingerprints in your agent.On your development server:
sudo grep "Accepted publickey" /var/log/secure | tail -n 1
ssh-keygen -l -f ~/.ssh/authorized_keys
On your own computer:
ssh-add -lAll three should show the same SHA256:... string. The number before it is the key size. PuTTY shows 255 for an Ed25519 key, and OpenSSH shows 256. It is the same key.
ssh-keygen -l is also a quick way to check your authorized_keys file. If a line is damaged, for example by a bad paste, it prints an error such as is not a public key file instead of a fingerprint.
You set .ssh to 700 and authorized_keys to 644. What happens if you don't? sshd checks these permissions every time you log in. If other users could have changed the file, someone could have added their own key to it, so sshd refuses to trust it.
Goal: On your development server, give the group write permission on authorized_keys. Then open a new connection from your own computer. What happens, and what does /var/log/secure say? Fix the permissions, and confirm that your key works again.
Warning: Keep your current session open while you do this. It is how you fix the file.
chmod g+w adds write permission for the group, from Lab 6./var/log/secure with sudo tail.In your open session on the development server:
chmod g+w ~/.ssh/authorized_keys
ls -l ~/.ssh/authorized_keys
The file now shows -rw-rw----. Open a new connection from your own computer. The server refuses your key and falls back to asking for a password. PuTTY first shows Server refused our key. Cancel the login, then look at the log:
sudo tail -n 5 /var/log/secure
sshd[2481]: Authentication refused: bad ownership or modes for file /home/yourname/.ssh/authorized_keys
Put the permissions back, and connect again. Your key works:
chmod 600 ~/.ssh/authorized_keys
ls -l ~/.ssh/authorized_keys
Going further: Try chmod 644 ~/.ssh/authorized_keys and connect again. Your key still works. sshd only refuses when the group or other users can write to the file, or to the .ssh directory or your home directory above it. Reading a public key is harmless. Even so, 600 is potentially more secure and may be a step you want to take in a production environment. Set it back to 644 or 600 when you are done. Then try the same experiment with chmod 775 ~/.ssh, and set it back with chmod 700 ~/.ssh.
This is the most common reason a key "doesn't work". When a key is refused, check the permissions and /var/log/secure first.
You need your production server name, something like ulc-###.ulcert.uw.edu, and your production username and password. They are in the email you received for your production server.
Your production server is a second server you want to log in to with the same key. You do not create a new key pair. Copy the same public key to it. One private key can open as many servers as you like.
Goal: Add your public key to ~/.ssh/authorized_keys for your user on your production server, and log in to it without a password.
.ppk file if you closed it), Mac and Linux with cat ~/.ssh/id_ed25519.pub on your laptop.-p.~/.ssh and authorized_keys may already exist on production. That is why you use >>.Log in to your production server with your password, then run these commands. Paste your own public key between the quotes:
cd
mkdir ~/.ssh
chmod 700 ~/.ssh
ls -ld ~/.ssh
echo 'ssh-ed25519 AAAA...your key... yourname-laptop' >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
ls -l ~/.ssh/authorized_keys
ssh-keygen -l -f ~/.ssh/authorized_keys
If authorized_keys already had keys in it, >> kept them, and ssh-keygen -l lists them all. Don't remove keys that you didn't add.
Now open a new connection to production, leaving the first one open:
ssh produsername@ulc-###.ulcert.uw.edu logs you in without a password.Your laptop starts an agent for you. A server usually doesn't. In this part you log in to your development server and use it to connect to your production server, the way you would jump from one server to another at work.
Do everything in this part in your SSH session on your development server, as your own user.
This is a new key pair, separate from the one on your laptop. Each computer you connect from gets its own key pair.
ssh-keygen -t ed25519 -C "dev01"
ls -l ~/.ssh
Accept the default file name, and set a passphrase.
echo $SSH_AUTH_SOCK
ssh-add -l
SSH_AUTH_SOCK is empty, and ssh-add says Could not open a connection to your authentication agent. Nothing started an agent for this session.
Try the obvious thing first:
ssh-agent
ssh-add -l
The agent started and printed its process ID (Agent pid), but ssh-add still can't find it. Read what ssh-agent printed:
SSH_AUTH_SOCK=/tmp/ssh-XXXXXXu8pqa1/agent.2345; export SSH_AUTH_SOCK;
SSH_AGENT_PID=2346; export SSH_AGENT_PID;
echo Agent pid 2346;
ssh-agent doesn't set the variables itself. It can't: a program is not allowed to change the variables of the shell that started it. Instead, it prints the shell commands that would set them, and leaves it to you to run them. That agent is now running with no way to reach it, so stop it. Use the PID from your output, not the one shown here:
kill 2346
Now start an agent the right way:
eval "$(ssh-agent -s)"
echo $SSH_AUTH_SOCK
echo $SSH_AGENT_PID
ssh-add -l
| Part | What it does |
|---|---|
ssh-agent -s |
Starts the agent and prints the commands to set its variables. -s asks for bash (sh) syntax. |
$( ... ) |
Command substitution. The shell runs the command inside and replaces the whole thing with its output. |
eval |
Runs that output as shell commands in your shell, so SSH_AUTH_SOCK and SSH_AGENT_PID are set. |
This is the same idea as Lab 5, Part 1: the shell works on your command line before anything runs. ssh-add -l now says The agent has no identities.
ssh-add
ssh-add -l
With no file name, ssh-add loads the default keys from ~/.ssh, such as id_ed25519 and id_rsa.
Now add your development server's public key to production, the same way you added your laptop's key. Print it, and copy the whole line:
cat ~/.ssh/id_ed25519.pub
From your development server, log in to production. Production doesn't have this key yet, so enter your production password. Then add the key with >>, and check the file:
ssh username@ulc-###.ulcert.uw.edu
echo 'ssh-ed25519 AAAA...your key... dev01' >> ~/.ssh/authorized_keys
ls -l ~/.ssh/authorized_keys
ssh-keygen -l -f ~/.ssh/authorized_keys
exit
authorized_keys now holds two keys: one from your laptop, and one ending in dev01. The comments tell you which is which. ls -l should still show -rw-------. Adding a line with >> doesn't change a file's permissions.
Back on your development server, connect again:
ssh produsername@ulc-###.ulcert.uw.edu
exit
This time you are logged in to production from development with your key, and no password.
ssh can run a single command on another server and bring the output back, without opening an interactive session.
Goal: From your development server:
uptime on your production server.uptime from your production server to a file named prod_uptime.txt on your development server.hostname, who or date.Put the command at the end of the ssh line: ssh username@host command. For the file, use a redirect from Lab 5. Which computer's shell sees the >?
ssh username@ulc-###.ulcert.uw.edu uptime
ssh username@ulc-###.ulcert.uw.edu uptime > prod_uptime.txt
cat prod_uptime.txt
ssh username@ulc-###.ulcert.uw.edu hostname
ssh username@ulc-###.ulcert.uw.edu who

The > is seen by the shell on your development server, before ssh even runs. So ssh brings back the output, and your development shell writes it to prod_uptime.txt on development. To write the file on production instead, put the whole command in quotes so it is sent to the remote shell: ssh username@ulc-###.ulcert.uw.edu "uptime > prod_uptime.txt".
Open a second SSH session to your development server and run:
echo $SSH_AUTH_SOCK
ssh-add -l
The new session can't see the agent. The variables were set only in the shell where you ran eval. In the new session, run exit to close it.
The agent does not stop when you log out. Before you log out of the first session, stop it:
eval "$(ssh-agent -k)"
ssh-add -l
-k kills the agent named in SSH_AGENT_PID, and prints commands to unset its variables. If you have lost track of an agent, pgrep -u $USER -l ssh-agent lists every agent you are running, and pkill -u $USER ssh-agent stops them all.
Tip:
ssh-agent bashis another way to start an agent. It starts a new shell with the variables already set, and the agent stops when youexitthat shell. You may see it in older guides.
Note: Instead of a second key pair on your development server, you can forward your laptop's agent through your SSH session with
ssh -A, or in PuTTY under Connection -> SSH -> Auth -> Allow agent forwarding. Only forward your agent to servers you trust, because anyone with root on that server can use your agent while you are connected.
Your keys work. As long as a server still accepts passwords, though, so can a password guessing bot. Your production server is on the internet, so it is being tried by those bots right now. In this last part you finish the switch, and harden both servers by telling their SSH servers to stop accepting passwords. Turning off password logins is one of the first things an administrator does on any new server.
You will do your development server first. If you make a mistake there, you can always get back in at the UTM or VirtualBox console. Your production server has no console you can reach, so you only change it once you have practiced on development and checked every step.
Warning: Keep your current SSH session open until you have tested a new login. Restarting
sshddoes not close existing sessions. If something goes wrong, you fix it from the session you still have open.
Do this in your SSH session on your development server, as your own user.
The SSH server, sshd, reads /etc/ssh/sshd_config. Near the top it includes every .conf file in /etc/ssh/sshd_config.d/. Adding your own small file there is safer than editing the main file, and easy to undo.
grep -n Include /etc/ssh/sshd_config
ls -l /etc/ssh/sshd_config.d/
For most settings, sshd uses the first value it finds. The files are read in name order, so starting your file name with 01- makes sure it is read before the others. This matters on servers that come with a file that turns passwords on, such as 50-cloud-init.conf on many cloud servers.
sudo vim /etc/ssh/sshd_config.d/01-no-passwords.conf
Add these lines, then save and quit:
# Lab 7: SSH keys only, no passwords
PasswordAuthentication no
KbdInteractiveAuthentication no
| Setting | What it turns off |
|---|---|
PasswordAuthentication no |
Logging in with a password. |
KbdInteractiveAuthentication no |
Keyboard interactive logins. This is another way of asking for a password, so it must be turned off too. |
A mistake in an sshd file can stop the SSH server from starting. Always test first. sshd -t prints nothing if the configuration is good:
sudo sshd -t
sudo sshd -T | grep "passwordauthentication\|kbdinteractiveauthentication"
sshd -T prints every setting sshd will use, in lowercase. Both should say no. If sshd -t prints an error, fix the file and test again. Don't restart until it is clean. Now restart the SSH server:
sudo systemctl restart sshd
sudo systemctl status sshd
Goal: Without closing your current session:
Use the same PuTTY session or ssh command as always. For dritchie, change only the username. PuTTY users can type dritchie at login as after clearing the Auto-login username, or start a new session with the address and port only.
Your own user logs in with your key, as before.
dritchie is refused before being asked for a password:
# Mac and Linux
dritchie@127.0.0.1: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
# PuTTY
No supported authentication methods available (server sent: publickey,gssapi-keyex,gssapi-with-mic)
The list in brackets is what the server now accepts. password is no longer in it. gssapi is Kerberos, used in some company networks, which we don't use in this course.
On your development server, the log shows the failed attempt:
sudo tail /var/log/secure
Note: dritchie can still be reached with
sudo su - dritchiefrom your own account, which is how later labs use the lab accounts. Only SSH logins with a password are turned off.
Now make the same change on your production server. The commands are the same. What is different is that you can't use a console to get back in if you lock yourself out. If this happens reach out for help.
Warning: Do not continue until you can tick off every item below. Each one is a way back in if another one fails.
Before you start, check that:
Make the change: In your safety net session on production, repeat the steps from Add a configuration file to your development server and Test, then restart:
ls -l /etc/ssh/sshd_config.d/
sudo vim /etc/ssh/sshd_config.d/01-no-passwords.conf
sudo sshd -t
sudo sshd -T | grep -E "^(passwordauthentication|kbdinteractiveauthentication)"
sudo systemctl restart sshd
sudo systemctl status sshd
Goal: Without closing your safety net session:
For step 3, you need SSH to skip your key and go straight to a password. ssh has an option for that. Look for PubkeyAuthentication in man ssh_config, and set it with -o. You can run this from your development server, so it works for everyone, whether your laptop runs Windows or not.
Steps 1 and 2 are the same logins you have used since Part 5.
For step 3, on your development server:
ssh -o PubkeyAuthentication=no produsername@ulc-###.ulcert.uw.edu
produsername@ulc-###.ulcert.uw.edu: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
-o PubkeyAuthentication=no tells ssh not to offer any keys. Before this change you would have been asked for your password. Now password is not on the list, and the server refuses you straight away. Anyone trying to guess their way in gets the same answer.
When all three steps work, you can close your safety net session.
Note: If a new login fails, don't close your safety net session. Remove the file and restart
sshdfrom that session, as in Undoing this change. Then work through Troubleshooting and try again. If you have already lost every session to production, post your problem in this week's Teams channel, or reach out by email for help.
Leave this setting in place on both servers for the rest of the course. If you ever need to turn passwords back on, for example on your development server while you troubleshoot, run this on that server:
sudo rm /etc/ssh/sshd_config.d/01-no-passwords.conf
sudo sshd -t
sudo systemctl restart sshd
With passwords turned off, you can't log in from a new laptop or computer to add its key, because it has no key the server trusts yet. Instead, copy the new computer's public key, log in from a computer whose key already works, and add it to ~/.ssh/authorized_keys with >>, as in Steps 3 to 5 of Part 2 or Part 3. Remember Lab 5: never with >.
Discuss these with others in class:
username@127.0.0.1's password:. Is this coming from your key, your agent or the server? What does it tell you?eval "$(ssh-agent -s)" work when ssh-agent on its own does not?Most key problems come down to a short list. Check the server's log first. It usually tells you exactly what is wrong. Run this in your open session on the server:
sudo tail -n 20 /var/log/secure
Q. I am still asked for a password.
A. The server didn't accept your key and fell back to a password. Check, in this order:
authorized_keys file you edited? Keys belong to one user on one server.ssh-add -l.~/.ssh/authorized_keys? Not authorized_key, authorised_keys, or authorized_keys.txt. Run ls -la ~/.ssh.ssh-keygen -l -f ~/.ssh/authorized_keys print a fingerprint that matches your agent? See Task 2.Q. /var/log/secure says "Authentication refused: bad ownership or modes".
A. sshd refuses to use an authorized_keys file that other users could have changed. Check the permissions and owner of your home directory, .ssh and authorized_keys, using what you learned in Lab 6:
ls -ld ~ ~/.ssh ~/.ssh/authorized_keys
| Path | Should be | Fix |
|---|---|---|
| Your home directory | drwx------ (700), owned by you. Never writable by group or other. |
chmod 700 ~ |
~/.ssh |
drwx------ (700), owned by you. |
chmod 700 ~/.ssh |
~/.ssh/authorized_keys |
-rw------- (600), owned by you. |
chmod 600 ~/.ssh/authorized_keys |
If the owner is root, you probably created the file with sudo. Fix it with sudo chown -R $USER:$USER ~/.ssh.
Q. My prompt changed to > after I ran the echo command.
A. The closing quote ' is missing, and the shell is waiting for you to finish the command, as in Lab 3. Press Ctrl + C, and run the command again with a ' after your key, before >>.
Q. I used > instead of >>, and my other keys are gone.
A. > replaced the whole file. Add each missing public key again with >>. If you can't log in with any key, log in with your password, or at the console on your development server. On production after Part 7, log in from a computer whose key is still in the file. If you can't, post your problem in this week's Teams channel, or reach out by email for help.
Q. ssh-keygen -l -f ~/.ssh/authorized_keys says "is not a public key file".
A. A line in the file is damaged. The most common causes are a key that was only partly copied, a key split over two lines, or the SSH2 format from the PuTTYgen .pub file (see Task 1). Each key must be one line starting with ssh-ed25519 or ssh-rsa. wc -l should print one line per key.
Q. PuTTY says "Server refused our key".
A. The key in Pageant is not in authorized_keys for this user on this server. Compare fingerprints as in Task 2. If you are using an RSA key, check that your PuTTY is version 0.75 or newer. RHEL 9 refuses RSA signatures from older versions.
Q. PuTTY asks "login as:" every time.
A. Set your username under Connection -> Data -> Auto-login username, then go back to Session and click Save.
Q. Pageant says "Couldn't load private key (unrecognised key type)" or my key isn't listed.
A. Make sure you chose the .ppk private key, not the .pub file. Pageant forgets keys when it exits or your computer restarts, so you may need to add your key again.
Q. ssh-add says "Could not open a connection to your authentication agent".
A. No agent is running in this shell. Start one with eval "$(ssh-agent -s)" and try again. See Part 6.
Q. I am asked "Enter passphrase for key" every time I connect from my Mac.
A. Your key works, but it isn't loaded in your agent. Run ssh-add ~/.ssh/id_ed25519. The agent forgets your key every time you restart, so run ssh-add again after each restart, or add AddKeysToAgent yes to ~/.ssh/config.
Q. I forgot my passphrase.
A. There is no reset. Create a new key pair, copy the new public key to your servers, and remove the line for the old key from each authorized_keys file. Key comments make the old line easy to find.
Q. /var/log/secure mentions SELinux, or the permissions are right but the key is still refused.
A. The SELinux label on ~/.ssh may be wrong, for example if you created the files somewhere else and moved them. Reset the labels with restorecon -Rv ~/.ssh. We will cover SELinux in a later lab.
Q. After Part 7, sshd won't start, or I can't log in at all.
A. Use your safety net session. On your development server you can also log in at the UTM or VirtualBox console. Run sudo sshd -t to find the mistake, or remove the file as in Undoing this change, and restart sshd. If you have lost every session to your production server, you can't fix it yourself. Post your problem in this week's Teams channel, or reach out by email for help.
Q. After Part 7, I need to log in from a new computer, and I get "Permission denied (publickey)".
A. Passwords are turned off, and the new computer's key isn't in authorized_keys yet. Add the new public key from a computer that already works. See Adding a key from a new computer.
Note: This appendix is optional and only applies to macOS. It is not part of the lab. The rest of this course uses the standard
ssh-addand~/.ssh/configoptions, which work the same on macOS, RHEL and Debian or Ubuntu.
Apple's version of OpenSSH can save your key's passphrase in your macOS Keychain, the same place Safari and other apps store passwords. Your key can then be loaded into the agent after a restart without you typing the passphrase.
| Command | What it does |
|---|---|
ssh-add --apple-use-keychain ~/.ssh/id_ed25519 |
Adds your key to the agent, and saves its passphrase in your Keychain. You type the passphrase this one time. |
ssh-add --apple-load-keychain |
Loads every key whose passphrase is saved in your Keychain, with no prompt. Use it after a restart. |
To have ssh do this for you automatically, add these lines to ~/.ssh/config on your Mac:
Host *
AddKeysToAgent yes
UseKeychain yes
UseKeychain yes tells ssh to look up your passphrase in the Keychain when it needs one, and AddKeysToAgent yes loads the key into the agent the first time it is used.
Keep in mind:
ssh-add --apple-use-keychain fails with an illegal option or unknown option error, and UseKeychain in ~/.ssh/config stops ssh with Bad configuration option: usekeychain. If you copy your ~/.ssh/config to a Linux system, add the line IgnoreUnknown UseKeychain above the Host * block.ssh-add -K and ssh-add -A. These are the names used before macOS 12 (Monterey), and they are deprecated.Lab complete