How to Set Up an SSH Key for Your Linux Server
Create an Ed25519 SSH key on Linux, macOS or Windows, copy it to your server, test the login and turn off password logins.
To set up an SSH key, run ssh-keygen -t ed25519 on your own computer, copy the public key to your server with ssh-copy-id (or paste it into ~/.ssh/authorized_keys), and then log in without a password. Once key login works, you can turn password logins off so nobody can guess their way in. It takes about five minutes and works the same on Linux, macOS and Windows 10 or 11.
This guide is for Cloud VPS Hosts customers connecting to a Linux VPS or dedicated server. The examples use deploy as the server username and 203.0.113.10 as the server IP. Replace them with your own.
What is an SSH key pair?
An SSH key pair is two matching files. The private key stays on your computer and works like a secret that never leaves it. The public key goes on every server you want to log in to. When you connect, the server checks that you hold the matching private key, without the key ever being sent over the network.
| File | Where it lives | Share it? |
|---|---|---|
id_ed25519 (private key) |
Your computer, in ~/.ssh/ |
Never |
id_ed25519.pub (public key) |
Your computer and each server’s authorized_keys |
Yes, it is safe to share |
Which key type should you use?
Use Ed25519. It is short, fast and supported by every current OpenSSH release, including Ubuntu 20.04 and newer, Debian, AlmaLinux and Windows’ built-in SSH client. Use RSA with 4096 bits only if you must connect to a very old system that does not understand Ed25519.
How do you create an SSH key on Linux or macOS?
Open a terminal and run:
ssh-keygen -t ed25519 -C "your-name@laptop"
Press Enter to accept the default location (~/.ssh/id_ed25519). When asked for a passphrase, enter one. A passphrase encrypts the private key, so a stolen laptop does not mean a stolen server. The -C text is only a label that helps you recognise the key later.
Show the public key:
cat ~/.ssh/id_ed25519.pub
How do you create an SSH key on Windows?
Windows 10 and 11 include OpenSSH, so the same command works in PowerShell or Windows Terminal:
ssh-keygen -t ed25519 -C "your-name@windows-pc"
The keys are saved in C:\Users\YourName\.ssh\. Show the public key with:
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
If you prefer PuTTY, create the key with PuTTYgen instead, choose EdDSA (Ed25519), save the private key as a .ppk file and copy the public key text from the PuTTYgen window.
How do you copy the public key to your server?
On Linux and macOS, ssh-copy-id does everything for you. It asks for the account’s password one last time:
ssh-copy-id deploy@203.0.113.10
Windows has no ssh-copy-id. Use this PowerShell one-liner instead:
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | ssh deploy@203.0.113.10 "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
You can also add the key by hand: log in with the password, open ~/.ssh/authorized_keys in an editor, paste the whole public key on a single line and save. The folder must be 700 and the file 600, or SSH ignores the key.
How do you test the key login?
ssh deploy@203.0.113.10
You should get in without the account password. If you set a passphrase, SSH asks for that instead. To avoid typing it every time, load the key into your SSH agent once per session with ssh-add.
To see exactly which key is offered and why a login fails, connect in verbose mode:
ssh -v deploy@203.0.113.10
How do you turn off password logins?
Only do this after key login works and you have tested it in a second window. Create a small settings file on the server:
sudo nano /etc/ssh/sshd_config.d/99-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
sudo sshd -t
sudo systemctl restart ssh
Keep your current session open and try a fresh login before you disconnect. Our initial server setup guide for Ubuntu covers this together with the firewall and automatic updates.
How do you manage several servers with an SSH config file?
A config file on your computer saves you typing usernames, IPs and key paths. Create or edit ~/.ssh/config:
Host web1
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/id_ed25519
Host db1
HostName 203.0.113.20
User deploy
Port 2222
IdentityFile ~/.ssh/id_ed25519
Now ssh web1 connects with the right user and key.
How do you fix common SSH key problems?
| Problem | Likely cause | Fix |
|---|---|---|
| Still asked for the account password | Wrong permissions on ~/.ssh or authorized_keys |
Run chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys on the server |
Permission denied (publickey) |
Key not on the server, wrong user, or a different key offered | Check with ssh -v which key is tried, and that its public half is in authorized_keys |
UNPROTECTED PRIVATE KEY FILE |
Private key readable by other users | chmod 600 ~/.ssh/id_ed25519 on Linux or macOS |
| Key broken after pasting | Public key split over several lines | Paste it again as one single line |
Ready to put your key to use? Every Cloud VPS Hosts Linux VPS gives you full root access over SSH.
Frequently asked questions
Can I use the same key for several servers?
Yes. Put the same public key in authorized_keys on each server. Many people use one key per computer rather than one per server, so a lost laptop means removing just one key everywhere.
What happens if I lose my private key?
You cannot log in with that key any more. Log in another way (a second key, a password if still enabled, or the server console via support), remove the old public key from authorized_keys and add a new one.
Is a passphrase really necessary?
It is optional, but recommended. Without it, anyone who copies your private key file can log in to your servers. With an SSH agent you only type the passphrase once per session.
Where do I find authorized_keys for the root user?
At /root/.ssh/authorized_keys. For security, log in as a regular user with sudo instead of root wherever you can.