SSH Public and Private Keys: How Key Login Works and How to Turn Off Passwords
Part of New VPS setup, our guide to this topic
An SSH public private key pair is two files. The public key goes into ~/.ssh/authorized_keys on the server; the private key stays on your computer and proves who you are by signing the login. Once key login works, turn off password login, and keep one session open while you test it.
On this page
This guide has no affiliate links in the steps.
Key login is the step of a new server setup that ends password guessing. Bots try passwords on port 22 all day; they can’t guess a key. If you’re setting up a fresh machine, this guide is step 4 and step 5 of our new VPS setup checklist, explained in full. If you’ve never connected to a server at all, start with how to SSH into a VPS.
How an SSH public private key pair works
The two keys are made together and only work as a pair. The public key can check a signature; only the private key can make one. That’s the whole trick.
Plenty of explainers say the server encrypts a random challenge with your public key and waits for you to decrypt it. That isn’t what SSH version 2 does. The SSH authentication standard, RFC 4252 section 7, says the client sends a signature made with the private key. What gets signed includes the session identifier of that one connection, your user name and the public key. The server looks up the public key in your authorized_keys file and checks the signature. If both hold, you’re in.
Two things follow from this. The private key never travels over the network, so a compromised server can’t steal it from a login. And the signature is tied to one session, so recording it doesn’t let anyone log in later. The flip side: whoever holds your private key file can sign as you, on every server that lists the matching public key.
The standard itself: the client sends a signature, and the server checks the key and the signature. Source: rfc-editor.org, captured Nov 20, 2026.
What is in each file
Every SSH public private key pair lives in two files. The private key is ~/.ssh/id_ed25519; the public key is the same name with .pub added. The public key is one line of text:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... laptop-2026
The fields are the key type, the key itself in base64, and a comment. Per the sshd manual, the comment “is not used for anything”; it only helps you tell keys apart. Name it after the device, not after yourself, so you know which line to delete when a laptop dies.
The private key file is the one to guard. The ssh manual says it should be readable by you and not accessible by others. Microsoft’s OpenSSH key management page is blunter: treat it like a password.
Ed25519 vs RSA: which SSH key type to use
Use Ed25519 for a new SSH public private key pair unless something forces you onto RSA.
| Ed25519 | RSA | |
|---|---|---|
| Default in ssh-keygen | Yes, since OpenSSH 9.5 | No |
| Size | Short key, one line you can read | Default 3072 bits; Ubuntu suggests 4096 |
| Supported since | OpenSSH 6.5 (January 2014) | Much longer |
| Use it when | Any new key | An old system or device rejects Ed25519 |
The OpenSSH 9.5 release notes made Ed25519 the default key type. Ubuntu 24.04 ships OpenSSH 9.6p1, and the Ubuntu Server OpenSSH guide recommends Ed25519 for its shorter key size and lower computing cost. For RSA, the ssh-keygen manual sets the default at 3072 bits and calls that “considered sufficient”. Ubuntu’s guide gives -t rsa -b 4096 as its RSA example.
Since OpenSSH 9.5, plain ssh-keygen makes an Ed25519 key. Source: openssh.com, captured Nov 20, 2026.
There’s also ed25519-sk: the private key lives on a FIDO hardware security key, a small USB device you touch to approve each login. It’s a good upgrade later. Start with a normal file key.
Passphrase and ssh-agent
ssh-keygen asks for a passphrase. You may leave it empty, but then anyone who copies the file can use it straight away. With a passphrase, a stolen file is useless until they crack it.
Typing it on every connection gets old. ssh-agent holds the unlocked key in memory, so you type the passphrase once and connect many times. Load the key with:
ssh-add ~/.ssh/id_ed25519
Set up SSH public private key login on Ubuntu 24.04
You generate the key on your own computer, never on the server. These steps assume you can already log in to the server, with a password or with a key the provider installed.
On Windows 10 or 11 the commands in steps 1 and 4 work the same in PowerShell, but ssh-copy-id doesn’t exist there. Use our Windows SSH key guide for steps 1 and 2, then come back for step 3.
Generate the key pair on your computer
your computerssh-keygen -t ed25519 -C "laptop-2026"
Press Enter to accept the default path. Type a passphrase twice. You now have ~/ (private) and ~/ (public). The -t ed25519 is the default on current OpenSSH; writing it out makes sure an older client doesn’t hand you something else.
If it fails: It asks to overwrite an existing id_ed25519. Say no. You’d lose a key that may already open other servers. Run it again with -f ~/ to make a second key.
Copy the public key to the server
your computerssh-copy-id yourname@YOUR_SERVER_IP
ssh-copy-id logs in (usually with your password) and appends your public key to ~/ on the server. Its manual page says it creates the file and the folder if they’re missing.
ssh-copy-id appends your key and creates ~/ if it isn’t there. Source: manpages.ubuntu.com, captured Nov 20, 2026.
No ssh-copy-id on your system? Pipe the key through a normal SSH session instead:
your computercat ~/.ssh/id_ed25519.pub | ssh yourname@YOUR_SERVER_IP 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'
authorized_keys holds one public key per line. Blank lines and lines starting with # are ignored. Each user has their own file, so a key added for yourname doesn’t let anyone in as root. To give a second device access, append its public key as a new line. To revoke one, delete its line.
Check permissions on the server
Log in to the server and run:
serverchmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
This isn’t tidiness. sshd’s StrictModes setting is on by default. If your home folder, ~/ or authorized_keys can be written by other users, sshd refuses to use the file. It logs a line like Authentication refused: bad ownership or modes for directory /. You’ll only see the cause in the server log (sudo journalctl -u ssh); your computer just shows Permission denied.
Log in with the key only
Open a new terminal on your computer and force a key-only login:
your computerssh -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no yourname@YOUR_SERVER_IP
The two -o options stop your client from falling back to a password. If you land at a shell, the key works. To see it spelled out, connect with ssh -v and look near the end for:
outputAuthenticated to YOUR_SERVER_IP ([YOUR_SERVER_IP]:22) using "publickey".
If it fails: Permission denied (publickey,password). The key wasn’t accepted. Don’t go on to step 6; check the problems section below first.
Keep a safety net open
From here on you’re changing how the server lets people in. Keep one logged-in session open and don’t touch it until step 7 has passed. Restarting SSH doesn’t drop sessions that are already open, so that window is your way back if the new settings are wrong.
Also find your provider’s web console (VNC or serial) in their control panel and check that it opens. If both SSH sessions die, it’s the only way in.
Turn off password login
Create a small drop-in file rather than editing the main config:
serversudo nano /etc/ssh/sshd_config.d/00-key-only.conf
Add the lines below. Leave out the last line if you log in as root and have no other user with a key: PermitRootLogin no would lock root out (more on that below).
00-key-only.conf — nanoPasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
Why a drop-in, and why 00-? The sshd_config manual says that for most settings “the first obtained value will be used”. Ubuntu’s / starts with Include /, and included files are read in alphabetical order. Many cloud images ship 50-cloud-init.conf in that folder, and cloud-init uses it to switch password login on or off. A file named 00-… is read first, so your no wins.
First value wins, which is why the 00- file beats anything read after it. Source: man.openbsd.org, captured Nov 20, 2026.
KbdInteractiveAuthentication no matters too. On systems that use PAM, the cloud-init docs warn that PasswordAuthentication no alone may not stop password prompts.
PermitRootLogin already defaults to prohibit-password: root can’t use a password, but can use a key. Set it to no only if you log in as your own sudo user. If root is the only account with a key, leave that line out.
Restart SSH and test from a new terminal
Check the config first. If sshd -t prints nothing, the files are valid:
serversudo sshd -t
Then ask sshd which values it will actually use:
serversudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|pubkeyauthentication) '
Check: passwordauthentication no, kbdinteractiveauthentication no, pubkeyauthentication yes, and your permitrootlogin value.
Apply it:
serversudo systemctl restart ssh
On Ubuntu 24.04 the service is called ssh (sshd is an alias), and it’s socket-activated: ssh.socket holds port 22. Changing login methods needs only this restart. The unit file in Ubuntu’s package uses KillMode=process, which is why your open session survives. (Changing the port is a different job on 24.04 and isn’t covered here.)
Now open a new terminal and log in normally with your key. Then prove passwords are really off:
your computerssh -o PubkeyAuthentication=no yourname@YOUR_SERVER_IP
You should get Permission denied (publickey). The list in brackets is what the server still accepts, and it now says only publickey. Here that error is the good result. Only now close the safety-net session.
Key takeaways
- The public key goes in
~/.ssh/authorized_keyson the server; the private key never leaves your computer. - SSH logs you in with a signature from the private key, checked against the public key. Nothing secret crosses the network.
- Use Ed25519 with a passphrase. RSA only for old systems.
- Fix permissions (
700on~/.ssh,600onauthorized_keys) or sshd ignores the file. - Turn off passwords in
/etc/ssh/sshd_config.d/00-key-only.conf, runsshd -t, restartssh, and close the old session only after a new key login works. That’s a finished SSH public private key setup.
A VPS for this guide
Everything above runs on any VPS with 1 GB of RAM. The cheapest 1 GB+ plan at each provider whose prices we read every day, IPv4 included:
| Plan · provider | vCPU · RAM · disk | You pay | Visit |
|---|---|---|---|
KVM VPS Special 1 GB RackNerd · Los Angeles +19 | 1 vCPU·1 GB·20 GB | $1.83/mo | |
VPS pico G11.5s netcup · Nuremberg | 1 vCPU·1 GB·30 GB | $2.00/mo | |
SSD VPS 1 CloudCone · USA | 2 vCPU·1 GB·30 GB | $2.33/mo | |
1 slice InterServer · New York +2 | 1 vCPU·2 GB·40 GB | $3.00/mo | |
NVMe 2 GB HostHatch · Amsterdam +15 | 1 vCPU·2 GB·10 GB | $4.00/mo |
Prices read Dec 4, 2026. gets that provider's deals on Telegram. Compare every 1 GB plan →
Common SSH key login problems
Permission denied (publickey).The server didn’t accept any key you offered. Check that the key is in authorized_keys of the user you’re logging in as, and that permissions match step 3. Then run ssh -v to see which key files your client offered, and sudo journalctl -u ssh on the server (via the console if needed) to see why sshd said no.
WARNING: UNPROTECTED PRIVATE KEY FILE!Your computer, not the server, is complaining. The next lines read Permissions 0644 for '…' are too open and “This private key will be ignored.” Fix it with chmod 600 ~/.ssh/id_ed25519 and connect again.
The server still asks for a password.Either the key failed and your client fell back to a password, or password login is still on. Run sudo sshd -T | grep -i passwordauthentication. If it says yes, some file is read before yours; grep -ri passwordauthentication /etc/ssh/sshd_config.d/ will show which.
The wrong key is offered.With several keys in ~/.ssh, the client tries them in turn, and the server may stop you after too many failures. Name the key with ssh -i ~/.ssh/id_ed25519_vps yourname@YOUR_SERVER_IP. To make it permanent, add a Host block to ~/.ssh/config with IdentityFile and IdentitiesOnly yes.
You’re locked out.Log in through the provider’s web console with your user’s password. Fix authorized_keys, the permissions or the drop-in file, run sudo sshd -t, then sudo systemctl restart ssh. This is what step 5 is for.
If a private key is lost or leaked
A lost key is a chore. Log in with another device’s key or the web console, add a new public key, and delete the old line from authorized_keys.
A leaked key is urgent, even more so if it had no passphrase. Delete its line from authorized_keys on every server that trusts it, then make a new pair. This is where one key per device pays off: you remove one line and every other device keeps working.
Questions
Is it safe to share my SSH public key?
Yes. It’s built to be copied onto servers, and on its own it can’t log anyone in. Only the private key can sign a login. Never send the file without .pub.
Can I use the same SSH public private key pair on several servers?
Yes. Add the same public key to authorized_keys on each one. The catch is the blast radius: one leaked private key opens all of them. Many people keep one key per device rather than one per server.
Should I use RSA or Ed25519 for SSH keys?
Ed25519 for anything new. It’s the default in current ssh-keygen and Ubuntu’s docs recommend it. Use RSA (3072 or 4096 bits) only for an old system that won’t take Ed25519.
What happens if I lose my private key?
That key can’t log in any more, and it can’t be recovered from the public key. Get in with another key or the provider’s web console, add a new public key, and delete the old line.
Do I still need my user password after turning off SSH passwords?
Yes. For a user you created with adduser, sudo still asks for it, and the provider’s web console logs you in with it. Turning off SSH password login only closes the network door.
How we checked: We checked every command and default in this guide against the OpenSSH manual pages, the SSH standard (RFC 4252), Ubuntu’s Server documentation and Ubuntu 24.04’s own ssh.service unit. We did not run the steps on every provider’s images; cloud images differ in what they put in sshd_config.d/, which is why step 7 checks the effective settings. How we check.