An SSH key pair replaces your password with a long secret that cannot be guessed. It is the single best upgrade to a new server's security, because bots try passwords on port 22 constantly.
How it works
You make two files: a private key that stays on your computer, and a public key that you put on the server. When you connect, your computer proves it holds the private key without ever sending it.
1. Create a key
ssh-keygen -t ed25519 -C "my-laptop"
Accept the default location and set a passphrase. The passphrase encrypts the private key on disk, so a stolen laptop file is not enough.
2. Copy the public key to the server
ssh-copy-id root@203.0.113.10
If the command is not available, paste the contents of ~/.ssh/id_ed25519.pub into ~/.ssh/authorized_keys on the server.
3. Log in
ssh root@203.0.113.10
It should ask for the key passphrase, not the server password.
4. Turn off password login
Only after confirming the key works, edit /etc/ssh/sshd_config:
PasswordAuthentication no
PermitRootLogin prohibit-password
Then run sudo systemctl restart ssh. Keep your existing session open while you test a new one, so you cannot lock yourself out.
Use an SSH config file
Typing the address every time gets old. Add an entry to ~/.ssh/config on your own computer:
Host myvps
HostName 203.0.113.10
User root
IdentityFile ~/.ssh/id_ed25519
After that, ssh myvps is enough.
Type the passphrase only once
Run ssh-add to load your key into the SSH agent. You enter the passphrase once per session instead of at every connection.
Fix common errors
- Permission denied (publickey). The server does not have your public key, or the permissions are wrong. On the server,
~/.sshshould be mode 700 andauthorized_keysmode 600. - Too many authentication failures. Your agent is offering many keys. Add
IdentitiesOnly yesnext to the rightIdentityFilein your config. - Unprotected private key file. Run
chmod 600 ~/.ssh/id_ed25519so only you can read it. - Host key changed warning. The server was reinstalled, or something is intercepting the connection. If you reinstalled it yourself, remove the old entry with
ssh-keygen -R 203.0.113.10.
Why ed25519
ed25519 keys are short, fast and considered strong. Use RSA with at least 3072 bits only when an older system does not support ed25519.
Keep keys safe
- Never share or upload the private key (the file without
.pub). - Use a separate key for each device, so you can remove one without affecting the rest.
- Back up the private key in an encrypted place.
For the other first steps on a new server, follow secure a new VPS in 15 minutes.
Frequently asked questions
Which SSH key type should I use?
ed25519 is short, fast and secure, and is a good default.
Do I need a passphrase?
Yes. It protects the private key if the file is stolen.
Is it safe to disable password login?
Yes, after you have confirmed key login works. Keep a second session open while testing.
Where is the private key stored?
In ~/.ssh on your computer. Never copy it to the server.
Can I use the same key on many servers?
Yes. The public key can be on as many servers as you like. Keep one private key per device.
What if I lose my private key?
You cannot recover it. Log in another way, such as the console in your VPS panel, and add a new public key. This is why you keep an encrypted backup.
Is a passphrase required?
It is not required, but it is strongly recommended. Without one, anyone who copies the key file can log in.