Password login is the easiest way into a Linux server, and bots know it. They try common usernames and passwords all day, and they only need one weak password to get in. SSH key authentication replaces that password with a key pair, and the private half never leaves your computer. This guide shows how to set up ssh key authentication linux server login, switch to key-only access, and test it without locking yourself out.
How SSH Keys Work
An SSH key pair has two halves. The public key sits on the server, in the authorized_keys file of the user you log in as. The private key stays on your own computer, and you never send it anywhere. When you connect, the server checks that your private key matches the public key on file, and only then lets you in.
That split is what makes keys safer than passwords. A password travels to the server each time you log in, and it can be guessed. A private key never travels, and guessing it is not practical. Adding a passphrase to the key adds one more layer: even if someone copies the key file, they still need the passphrase to use it.
Keys also make daily work faster. With an SSH agent running, you type the passphrase once per session instead of a password on every login. Tools like rsync, scp, and git over SSH use the same key, so one setup covers all of them.
Generate a Key Pair With ssh-keygen
On your own computer, not on the server, open a terminal and run ssh-keygen ed25519 to create an Ed25519 key. Ed25519 keys are short, fast, and supported by current OpenSSH versions on both sides.
ssh-keygen -t ed25519 -C "you@your-laptop"
Accept the default file location unless you have a reason to change it. The command creates two files: id_ed25519, the private key, and id_ed25519.pub, the public key. Set a passphrase when asked. A key without a passphrase works, but anyone who copies the file can use it, so the passphrase is worth the few seconds it costs.
If you already have a key pair, you can reuse it instead of making a new one. Check the .ssh folder in your home directory first. Making a fresh key per machine is cleaner, though: when you retire a laptop, you delete that one key from the server instead of wondering where the shared key went.
On Windows, the same command works in PowerShell or in Windows Terminal with OpenSSH installed. On macOS and Linux, it works in any terminal. The steps after this point are the same no matter which system you generate the key on.
Copy the Public Key to Your Server
The easiest way to copy the key is ssh-copy-id. It logs in with your current password, creates the .ssh folder on the server if needed, and adds your public key to authorized_keys.
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server-ip
Replace user with your server username and the IP address with your server address. You will type your password one more time, and after that the key is in place. Test it right away in a new window:
ssh -i ~/.ssh/id_ed25519 user@your-server-ip
If the login works without asking for a password, only for the key passphrase if you set one, the key is working. If it still asks for a password, check the permissions on the server. The .ssh folder must be 700 and authorized_keys must be 600, and both must belong to the user you log in as. Wrong permissions are the most common reason a good key gets ignored.
| File or folder | Correct permission | Fix command |
|---|---|---|
~/.ssh |
700 | chmod 700 ~/.ssh |
~/.ssh/authorized_keys |
600 | chmod 600 ~/.ssh/authorized_keys |
~/.ssh/id_ed25519 (your computer) |
600 | chmod 600 ~/.ssh/id_ed25519 |
If ssh-copy-id is not available, copy the key by hand. Print the public key file on your computer, then paste that one line into ~/.ssh/authorized_keys on the server. Make sure it stays on a single line: a wrapped line will not match your private key.
SSH Key Authentication Linux Server: Switch to Key-Only Login
Once the key login works, turn password login off. This is the step that makes the setup a key-only ssh login, and it is also the step where a mistake can lock you out, so do it slowly.
Open /etc/ssh/sshd_config on the server with sudo and find these lines. Change them to match, or add them if they are missing:
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
PermitRootLogin prohibit-password
Those lines disable password authentication ssh wide, keep key login on, turn off challenge-response login, and stop root from logging in with a password. If you already disabled root login fully in your server setup, keep that stricter setting instead. Before you restart SSH, check the config with sudo sshd -t. If the check prints nothing, the config is valid. Restart the service, then test a new login in a second window while your old session is still open.
Try a password login on purpose after the restart, from a fresh connection. It should fail right away instead of asking for a password. That failed attempt is the proof the change worked. If you need the full hardening pass around this change, including custom ports and fail2ban, read our guide to securing SSH with key-only auth, custom ports, and fail2ban.
Fix the Problems That Come Up Most
Most key setups fail for a small reason, and the fix is quick once you know where to look. Run the client with ssh -v to see what the server accepts and rejects, and check the server auth log with sudo tail -f /var/log/auth.log while you try again.
Wrong permissions cause most failures. The server ignores keys when the home folder, the .ssh folder, or authorized_keys is writable by other users. The table above gives the right values, and you should also check that your home folder itself is not group-writable.
A second common problem is the wrong key. If you have more than one key pair, the client may offer a different one first. Use -i with the exact key path to force the right one, or list your keys with ssh-add -l to see which ones the agent holds. If the agent holds an old key, add the new one with ssh-add ~/.ssh/id_ed25519.
The third problem is editing the wrong config. Some systems have extra config files in /etc/ssh/sshd_config.d/ that override the main file. If your change seems to have no effect, look in that folder for a file that sets PasswordAuthentication yes and fix it there.
If you do lock yourself out, use your provider console to log in and fix the config. That is why keeping console access tested matters. Fix the setting, restart SSH, and test again before you close the console session.
Go Further: Second Keys and Hardware Keys
One key on one laptop is a single point of failure. Add a second key from another machine you trust, so losing one laptop does not mean losing server access. Each key is a separate line in authorized_keys, and you can remove one line without touching the others.
When you stop using a machine, delete its key line from every server it could reach. Do this the day you retire the machine, not months later. Old keys from forgotten laptops are easy to overlook, and each one is a way in.
For accounts that matter most, a hardware security key adds a physical check to the login. The key stays on the device, and login needs a touch on it. If that sounds like the right step for your servers, compare the current options in our roundup of the best hardware security keys for 2026 before you buy one.
Keys also pair well with the rest of server hardening. A firewall and fail2ban still help, because they cut the noise of bots knocking on SSH even after password login is off. Our UFW and fail2ban setup guide for Ubuntu covers both, and if you want to hide the server behind a private network as well, our WireGuard VPN setup guide for a VPS shows how to reach it only through the tunnel.
Review your keys once a month, the same way you review your server users. Remove lines you no longer recognise, confirm the keys you still use, and make sure your backup of the private key is somewhere safe and private. Key-only login is a strong setup, and it stays strong only if the keys themselves stay tidy. That routine is what keeps an ssh key authentication linux server setup working as intended over time.