How do I set up SSH keys?
In this article
With SSH keys you log in to your VPS with a key pair instead of a password. The private part stays on your own computer; the public part goes on the server. Once you then turn off password login, a bot trying to guess passwords no longer stands a chance. This guide is for a Linux VPS or dedicated server running Ubuntu, Debian or AlmaLinux; on a Windows VPS you log in via Remote Desktop.
Step 1: create a key pair
Create the key on your own computer, not on the VPS. Windows 10 and 11 (in PowerShell), macOS and Linux have ssh-keygen built in:
ssh-keygen -t ed25519
Press Enter to save the key in the default location and choose a passphrase, so that a stolen key file alone is not enough. You get two files in the .ssh folder in your user folder:
id_ed25519: your private key. Never share it and keep a copy; without this file you can no longer get in with this key after step 4.id_ed25519.pub: your public key. This one goes to the VPS.
Ed25519 is the key type Ubuntu recommends: short, fast and secure. Prefer PuTTY? Skip steps 1 and 2 and follow the Using PuTTY on Windows section further down.
Step 2: put the public key on your VPS
You log in one more time with the password from your delivery email. On macOS and Linux it takes one command:
ssh-copy-id root@your-ip
Windows doesn't have ssh-copy-id; in PowerShell you do the same with:
type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh root@your-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
Replace your-ip with your VPS's IP address. If you have created your own user (see Secure your VPS: the first 30 minutes), use that name instead of root: the key only works for the user you install it for.
Step 3: test in a second session
Keep your current session open and open a new terminal window:
ssh root@your-ip
If SSH now asks for your key's passphrase (Enter passphrase for key) instead of the server password, the key works. If it still asks for the server password, don't continue to step 4; check the common problems below first.
Step 4: turn off password login
Don't edit /etc/ssh/sshd_config itself; create your own settings file in the sshd_config.d folder instead:
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
Put this line in it and save with Ctrl + X, Y, Enter:
PasswordAuthentication no
Why this way? OpenSSH uses the first value it finds for each setting, and the files in sshd_config.d are read in alphabetical order, before the rest of sshd_config. On cloud images, cloud-init sometimes adds a file 50-cloud-init.conf with PasswordAuthentication yes; a change in sshd_config itself is then ignored. The 00- in the name puts yours first. Check the settings and restart the SSH server:
sudo sshd -t && sudo systemctl restart ssh
sudo sshd -T | grep -i '^passwordauthentication'
The last line should show passwordauthentication no. On Ubuntu and Debian the service is called ssh; on Ubuntu 24.04, systemctl restart sshd fails with Unit sshd.service not found. Only on AlmaLinux and Rocky Linux is the service called sshd:
sudo sshd -t && sudo systemctl restart sshd
Then open another new window and check that you get in with your key. Only close your first session once that works.
Using PuTTY on Windows
If you use PuTTY, create the key with PuTTYgen, which is installed along with PuTTY. Steps 3 and 4 above stay the same.
- Open PuTTYgen. At the bottom, under Type of key to generate, choose EdDSA (called Ed25519 in older versions) and make sure Ed25519 (255 bits) is selected.
- Click Generate and move your mouse over the blank area until the bar is full.
- Enter a strong passphrase in Key passphrase and Confirm passphrase. If you forget it, you can no longer use the key.
- Click Save private key and store the
.ppkfile somewhere safe. - Copy all the text from the box Public key for pasting into OpenSSH authorized_keys file. It starts with
ssh-ed25519and is one long line. - Log in to your VPS with PuTTY (see connecting to your VPS over SSH) and open the key file:
mkdir -p ~/.ssh && chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
- Paste the public key on a new line with the right mouse button, save with Ctrl + X, Y, Enter and set the permissions:
chmod 600 ~/.ssh/authorized_keys
- In PuTTY, enter your VPS's IP address under Session. Go to Connection > SSH > Auth > Credentials (directly under Auth in older versions) and select your
.ppkfile under Private key file for authentication. Go back to Session, give the session a name under Saved Sessions and click Save so PuTTY remembers the key. - Open the session in a new PuTTY window: PuTTY now asks for your key's passphrase instead of the password. If that works, continue with step 4 above.
Common problems
Unit sshd.service not found: on Ubuntu and Debian the service is calledssh. Usesudo systemctl restart ssh.- It still asks for the server password: the key isn't installed for the user you log in as, or the permissions are wrong. On the VPS, check with
cat ~/.ssh/authorized_keysthat your key is in it as one line, and set the permissions withchmod 700 ~/.sshandchmod 600 ~/.ssh/authorized_keys.ssh -v root@your-ipshows which keys your computer offers. - Password login still works after step 4: another file takes priority, or your file isn't read. Use
sudo grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/to see where else the setting appears, and check that your file ends in.conf: other files in that folder are skipped. Server refused our key(PuTTY): you pasted the wrong text, for example the contents of a file saved with Save public key.authorized_keysneeds the single line starting withssh-ed25519from the box at the top of PuTTYgen.- You're locked out (
Permission denied (publickey)): use the console in your VPS panel (see connecting via VNC), which works independently of SSH. Log in there with your root password, delete/etc/ssh/sshd_config.d/00-hardening.confand runsystemctl restart ssh. You can then log in with your password again and set up the key once more.
Need help?
With an SSH key and password login turned off, your VPS is much better protected. The next steps, such as your own user, a firewall and fail2ban, are covered in Secure your VPS: the first 30 minutes. Still stuck? Open a ticket via our support page. Never send your private key.
Launch your server in 60 seconds on our own hardware.