To set up SSH key authentication on a Linux server, generate a key pair on your computer with ssh-keygen -t ed25519, copy the public key to the server with ssh-copy-id user@host, confirm that key login works, and then disable password logins in the SSH daemon configuration. The private key never leaves your computer.
SSH key authentication replaces a guessable password with a cryptographic proof. This guide explains how key pairs work, how to create and install one, how to fix the permission problems that break it, and how to turn off password authentication safely. The ssh key authentication setup below works on Ubuntu, Debian and Fedora-family servers.
A key pair has two files. The private key stays on your computer and must be kept secret. The public key is safe to share and is stored on the server in the file ~/.ssh/authorized_keys of the user you log in as. When you connect, the server sends a challenge that only the holder of the matching private key can answer, so no secret travels over the network.
Benefits over passwords:
authorized_keys.Run this on your own computer (Linux, macOS, or Windows PowerShell):
Accept the default location, ~/.ssh/id_ed25519, and set a passphrase. Ed25519 keys are short, fast and supported by current OpenSSH versions. If you must connect to a very old system that lacks Ed25519, use ssh-keygen -t rsa -b 4096 instead.
The command creates two files:
The easiest method, while password login still works:
It appends the key to ~/.ssh/authorized_keys on the server and sets safe permissions. Without ssh-copy-id (for example on Windows PowerShell), do it by hand. Print the public key and paste it into the file on the server:
Each key is one line starting with ssh-ed25519. Many cloud providers also let you register a public key in their dashboard so it is installed at creation time.
Open a new terminal, leaving any existing session open:
If it asks for the key passphrase rather than the account password, key authentication works. To avoid typing the passphrase repeatedly, add the key to the agent:
Troubleshoot with ssh -v, and with sudo journalctl -u ssh on the server (the unit is sshd on Fedora-family systems).
By default the SSH daemon ignores keys when the files are writable by others (the StrictModes option). The required state is:
| Path | Mode | Owner |
|---|---|---|
~ (home directory) | not group or world writable | the user |
~/.ssh | 700 | the user |
~/.ssh/authorized_keys | 600 | the user |
Fix ownership with chown -R deploy:deploy ~/.ssh. On systems with SELinux (Fedora, Rocky Linux), restore labels with restorecon -R ~/.ssh if a manually created file is rejected.
Do this only after key login works for a user who has sudo rights. Create a drop-in configuration file:
Check the effective values, validate, then reload, and test from a new terminal before closing the old one:
Cloud images sometimes ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. Because the first value read wins, make sure your file sorts earlier or edit the other file.
authorized_keys.ed25519-sk keys for sensitive servers.Ed25519 for modern systems. RSA with 4096 bits is the fallback when old software needs it.
Yes. Install the same public key on each server. For better separation, use different keys for different roles.
Log in through the provider's console, remove the old public key from authorized_keys and add a new one. Keep a backup to avoid this.
The key was not installed for that user, the permissions are wrong, or you connected as a different user than the one you configured.
Generate an Ed25519 key pair, install the public key into ~/.ssh/authorized_keys with ssh-copy-id, keep ~/.ssh at 700 and the file at 600, and test in a second terminal. Then disable password logins with PasswordAuthentication no and reload SSH.