Sub topic 7
Installing the Public Key on the Server
Assume you can already connect to the server with SSH and a password, understand basic Unix permissions, and know which local private key belongs to the account you will use. In this chapter, the local computer is the client, and the remote Linux machine is the server.
Public key authentication works only when the server has a copy of your public key. You install that copy in the remote account's ~/.ssh/authorized_keys file. The private key stays on your client and must never be copied to the server.
Understand the destination
An SSH public key is one long line. It usually starts with a key type such as ssh-ed25519, followed by encoded key data and an optional comment.
The server checks each line in authorized_keys. If one public key matches the private key that your SSH client can prove it controls, the server allows the login.
| Item | Location | Purpose |
|---|---|---|
| Private key | Client: `~/.ssh/id_ed25519` | Proves your identity; keep secret |
| Public key | Client: `~/.ssh/id_ed25519.pub` | Safe to copy to the server |
| Authorized keys | Server: `~/.ssh/authorized_keys` | Lists public keys allowed to log in |
The key must be installed for the correct remote user. Installing a key for alice does not allow login as root or another account.
Install with `ssh-copy-id`
The simplest method is ssh-copy-id. It logs in using your current password and appends the public key to the remote account's authorized keys.
ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]Enter Alice's server password when prompted. The command normally creates ~/.ssh and authorized_keys if they do not exist. It does not replace existing keys, so other authorized users can continue to connect.
After the command finishes, test the key explicitly:
ssh -i ~/.ssh/id_ed25519 [email protected]If the test succeeds, the server accepted the installed public key. A passphrase prompt for the private key is expected if you protected that key with a passphrase. It is different from a server account password.
Install the key manually
Some minimal systems do not include ssh-copy-id. You can transfer the public key through an existing password login:
cat ~/.ssh/id_ed25519.pub | ssh [email protected] 'umask 077; mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys'The umask 077 command makes newly created files accessible only to Alice. The >> operator appends the key instead of deleting existing entries.
You can also log in and edit the file directly:
ssh [email protected]
umask 077
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keysPaste the complete public key on one line, save the file, and then set the permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keysDo not paste the private key. Do not wrap the public key across multiple lines.
Verify the account and permissions
A frequent error is installing the key in the wrong home directory. Confirm the remote user and home path:
ssh [email protected] 'printf "user=%s home=%s\n" "$USER" "$HOME"; ls -ld "$HOME" "$HOME/.ssh" "$HOME/.ssh/authorized_keys"'The .ssh directory should normally be mode 700, and authorized_keys should normally be mode 600. The files must also be owned by the login user. If you have administrative access, repair ownership with:
sudo chown -R alice:alice /home/alice/.sshExercise
Which file belongs on the server, and which file must remain only on the client?
Show answer
Copy ~/.ssh/id_ed25519.pub to the server's ~/.ssh/authorized_keys. Keep ~/.ssh/id_ed25519, the private key, only on the client.