Sub topic 7
Installing the public key on the server
You have a key pair. This chapter puts the public half on the server so the server can recognize you. It assumes you can already log in to the server with a password, that you have generated an Ed25519 key pair with ssh-keygen, and that you know Unix permission bits such as 700 and 600.
What "installing" a key means
Installing a key means adding one line of text to a file on the server. The file is ~/.ssh/authorized_keys in the home directory of the account you want to log in as. When you connect, sshd (the SSH server daemon) reads this file and checks whether your key is listed.
Only the public key goes on the server. The private key stays on your machine. A public key can't be used to impersonate you, so copying it over an ordinary password login is safe.
The file has these properties:
- It is plain text with one key per line.
- Each line holds an optional list of options, the key type, the base64 key data, and an optional comment.
- It is per account. A key in
/home/alice/.ssh/authorized_keyslets someone log in asaliceonly. - It can hold many keys, for example one for your laptop and one for your workstation.
Method 1: ssh-copy-id
ssh-copy-id is the usual way to do this. It logs in with your password, creates ~/.ssh and authorized_keys if they are missing, and appends your public key.
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]You are prompted once for the account password. Afterwards the tool prints a message like Number of key(s) added: 1 and suggests you try logging in.
Before it installs anything, ssh-copy-id tries to log in with each key it would add. It skips keys that already work, so running it twice does not create duplicate lines.
Some options are useful in practice:
| Option | Purpose |
|---|---|
| `-i file` | Choose which public key to install. Without it, the tool uses the keys that `ssh-add -L` lists, or the default `~/.ssh/id_*.pub` files. |
| `-p port` | Connect to a non-standard SSH port. |
| `-n` | Dry run. Prints the keys that would be installed and changes nothing. |
| `-f` | Force installation without the "already installed" check. |
| `-o option` | Pass an option through to `ssh`, such as `-o StrictHostKeyChecking=ask`. |
Always pass -i when you have more than one key. Otherwise you may install a different key from the one you meant.
ssh-copy-id -n -i ~/.ssh/id_ed25519.pub -p 2222 [email protected]Run the dry run first if the server is important. It shows exactly which key would be sent.
First connection and host keys
The first time you connect to a server, ssh shows its host key fingerprint and asks whether to continue. Compare the fingerprint with one from a trusted source, such as your cloud console or the server's administrator, before you answer yes. The next chapter covers host key verification in detail. ssh-copy-id goes through the same prompt, because it uses ssh underneath.
Method 2: install the key by hand
ssh-copy-id is not available everywhere. Windows clients and minimal environments often lack it. In that case you can pipe the key through ssh.
cat ~/.ssh/id_ed25519.pub | ssh [email protected] 'umask 077; mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys'Here is what each part of the command does:
catprints the public key on your machine.- The pipe sends that text to the remote shell's standard input.
umask 077makes any new file or directory private to the owner. A new~/.sshgets mode700and a newauthorized_keysgets mode600.mkdir -p ~/.sshcreates the directory if it doesn't exist.cat >> ~/.ssh/authorized_keysappends the key. The>>operator is important. A single>would overwrite the file and delete every other key in it.
Method 3: paste the key with an editor
If you have a console session on the server, for example through a cloud provider's web terminal, open the file in an editor and paste the key.
- Print the public key on your machine with
cat ~/.ssh/id_ed25519.pub. - On the server, run
mkdir -p ~/.sshand thennano ~/.ssh/authorized_keys. - Paste the whole key as a single line and save.
The line must not be wrapped. A key split over two lines is read as two broken entries and sshd ignores both. Terminal copy-and-paste sometimes inserts line breaks, so check the result with wc -l ~/.ssh/authorized_keys. The count should equal the number of keys you have installed.
Set the permissions
sshd is strict about who can write to the files it trusts. With the default StrictModes yes, it refuses to use authorized_keys if the file, the ~/.ssh directory, or the home directory can be modified by anyone other than the owner or root. A key that is correct but sits behind loose permissions fails silently from your side.
Set the expected modes on the server:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_ed25519The third line applies to the private key on your client machine. Run it there, not on the server. Mode 600 on the private key is required, because the ssh client refuses to use a private key that other users can read.
You can check everything on the server in one command:
ls -ld ~ ~/.ssh ~/.ssh/authorized_keysYou should see these results:
- The home directory is not writable by group or others. A mode such as
drwxr-xr-xis fine, butdrwxrwxrwxordrwxrwxr-xwith a shared group is not. Fix it withchmod go-w ~. ~/.sshisdrwx------.authorized_keysis-rw-------, or-rw-r--r--at most. The file may be readable, but it must not be writable by others.- All three belong to the account you log in as. If you created them while using
sudo, they may belong to root. Fix that withsudo chown -R alice:alice ~alice/.ssh.
Adding options to a key
Each line can start with options that limit what that key may do. Options go before the key type and are separated by commas, with no spaces outside quotes.
from="203.0.113.0/24",restrict ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyDataOnly alice@laptopThat example is not a working key. It only shows the layout. The options mean:
from="203.0.113.0/24"accepts this key only from clients in that address range.restrictturns off port forwarding, agent forwarding, X11 forwarding, and terminal allocation, unless you re-enable them with options such aspty. Use it for keys that automated jobs rely on.command="/usr/local/bin/backup.sh"forces that command to run whatever the client asks for. This suits single-purpose keys, such as one used by a backup job.
Options are optional. A plain line with only the key type, key data, and comment works for normal interactive logins.
Verify that the key works
Test with a new connection, and keep your current session open until you've seen it succeed. The command below turns off password fallback, so a success proves the key itself works.
ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]You should land in a shell without a password prompt. If the key has a passphrase, you are asked for the passphrase of the private key. That is a local unlock, not a server password. The later chapter on ssh-agent shows how to avoid retyping it.
If the connection fails with Permission denied (publickey), add -v to see which keys the client offered and whether the server accepted any. Troubleshooting has its own chapter. These are the most common causes at the installation step:
- You installed the wrong key. Compare
ssh-keygen -lf ~/.ssh/id_ed25519.pubon the client withssh-keygen -lf ~/.ssh/authorized_keyson the server. The fingerprints should match. Older OpenSSH versions print only the first key in a file, so don't read too much into a mismatch there. - You installed the key for a different account than the one you log in as.
- The key line was wrapped or truncated during a paste.
- The permissions or ownership are wrong, as described above.
- The server uses a custom
AuthorizedKeysFilesetting insshd_config. Check it withsudo sshd -T | grep -i authorizedkeysfile. The default is.ssh/authorized_keys. - The account's home directory is encrypted, as with some older Ubuntu setups using
ecryptfs, and is not mounted until you log in. In that case the server can't read the file.
Managing several keys in one file
Because the file takes one key per line, you can add keys from more machines by running ssh-copy-id again with a different -i. Keep the comment field meaningful, such as alice@laptop or alice@work-desktop. When you later need to find and remove one key, the comment tells you which line it is.
To remove a key, delete its line with an editor. The change takes effect on the next login attempt, with no service reload. Existing sessions are not closed by removing a key, so end them separately if you need to. Rotation and revocation are covered in the last chapter.
Exercise
You run echo "$(cat ~/.ssh/id_ed25519.pub)" | ssh [email protected] 'cat > ~/.ssh/authorized_keys' on a server where two other keys were already listed. What happens to those keys, and what should the command have used instead?
Show answer
The single > overwrites the file, so the two other keys are deleted and only the new key remains. Anyone relying on those keys loses access. The command should have used >> to append, ideally with umask 077; mkdir -p ~/.ssh; before it so the directory and file are created with private permissions.
Exercise
The key is installed in ~/.ssh/authorized_keys and the file has mode 600, but logins still fail with Permission denied (publickey). ls -ld ~ shows drwxrwxr-x. Why might that matter, and how do you fix it?
Show answer
With StrictModes yes, sshd rejects keys when the home directory is writable by group or others, because another user could replace the authorized_keys file or the .ssh directory. Remove the extra write permission with chmod go-w ~. Then retry the login with ssh -v to confirm the key is accepted.
Exercise
Write the authorized_keys line prefix that accepts a key only from the 198.51.100.0/24 network and disables forwarding and terminal allocation.
Show answer
from="198.51.100.0/24",restrict followed by a space and then the key type, key data, and comment, for example from="198.51.100.0/24",restrict ssh-ed25519 AAAA... alice@laptop.