Every new Linux VPS starts life the same way: you get an IP address, a root password, and an open SSH port that bots begin poking at within minutes. Passwords can be guessed, reused, or leaked. SSH keys solve that problem, and once they are set up you will also log in faster, because there is nothing to type.
This guide walks through setting up SSH keys on a Linux VPS from start to finish, on macOS, Linux, or Windows, and then shows how to turn off password authentication safely. If you have just received your server, pair this with our checklist of first steps on a new Linux VPS.
How SSH keys work
An SSH key pair has two halves:
- The private key stays on your computer. Never share it, never upload it, never paste it into a chat.
- The public key goes on the server, in a file called
~/.ssh/authorized_keys.
When you connect, the server uses your public key to issue a challenge that only the matching private key can answer. Your private key never crosses the network. Even if someone captures the traffic, there is no password to steal.
You can protect the private key with a passphrase, so a stolen laptop does not automatically mean a compromised server. An SSH agent remembers the passphrase for your session, so you only type it once.
Step 1: Generate a key pair on your computer
Modern OpenSSH supports Ed25519 keys, which are short, fast, and considered strong. Open a terminal (on Windows 10 and 11, PowerShell includes the OpenSSH client) and run:
ssh-keygen -t ed25519 -C "you@yourlaptop"
The -C value is just a label so you can recognize the key later. You will be asked:
- Where to save the key. Press Enter to accept the default (
~/.ssh/id_ed25519). - A passphrase. Use one. It is the difference between "my laptop was stolen" and "my laptop and all my servers were stolen."
If you need to connect to a very old system that does not understand Ed25519, you can create an RSA key instead with ssh-keygen -t rsa -b 4096. For any current Linux distribution, Ed25519 is the better default.
You now have two files: id_ed25519 (private) and id_ed25519.pub (public).
Step 2: Create a regular user (if you haven't already)
Logging in as root all day is a habit worth breaking. Create a normal user with sudo rights first. On Ubuntu or Debian:
adduser deploy
usermod -aG sudo deploy
On Rocky Linux, AlmaLinux, or other RHEL-style systems, the admin group is wheel instead of sudo:
adduser deploy
passwd deploy
usermod -aG wheel deploy
Step 3: Copy the public key to the server
The easy way: ssh-copy-id
On macOS and Linux, ssh-copy-id handles everything, including file permissions:
ssh-copy-id deploy@your.server.ip
Enter the user's password one last time and the key is installed.
The manual way (works everywhere, including Windows)
Print your public key:
cat ~/.ssh/id_ed25519.pub
Copy the single line it prints. Log in to the server with the password, then run:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys
Paste the key on its own line, save, and lock down the file:
chmod 600 ~/.ssh/authorized_keys
Those permissions matter. OpenSSH refuses to use an authorized_keys file that other users can write to, and a permissions mistake is the most common reason keys "don't work."
Step 4: Test the key login
Open a new terminal window and connect:
ssh deploy@your.server.ip
If you set a passphrase, you will be asked for it (that is your key's passphrase, not the server password). If you get straight in, the key works.
If you are asked for the server password instead, run the connection with verbose output to see what happened:
ssh -v deploy@your.server.ip
Look for lines mentioning which keys were offered and whether the server accepted them. Typical culprits are wrong permissions on ~/.ssh, the key pasted with line breaks, or the key installed under a different user's home directory.
Step 5: Make life easier with an SSH config file
Instead of remembering IPs, create ~/.ssh/config on your computer:
Host myvps
HostName your.server.ip
User deploy
IdentityFile ~/.ssh/id_ed25519
Now ssh myvps is all you need. The same alias works with scp and rsync, and editors like VS Code pick it up for remote development.
Step 6: Disable password authentication
Only do this after you have confirmed the key login works, and keep your existing session open while you make the change. That open session is your safety net.
Edit the SSH daemon config:
sudo nano /etc/ssh/sshd_config
Find and set these lines (remove a leading # if present):
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
On older systems the second option may be called ChallengeResponseAuthentication. Also check the /etc/ssh/sshd_config.d/ directory: some cloud images ship a file there that turns password authentication back on, and files in that directory can override the main config.
Validate the config before reloading:
sudo sshd -t
No output means no errors. Then reload the service:
sudo systemctl reload ssh # Ubuntu/Debian
sudo systemctl reload sshd # RHEL-style systems
Now, from a new terminal, confirm you can still log in with your key. You can also check that passwords are refused:
ssh -o PubkeyAuthentication=no deploy@your.server.ip
You should see "Permission denied (publickey)." That is exactly what you want.
Managing keys over time
- One key per device. Give your laptop, desktop, and CI system their own keys. If one is lost, you remove a single line from
authorized_keysinstead of rotating everything. - Label keys clearly with the
-Ccomment so you know which line belongs to which device. - Remove old keys when a device is retired or a teammate leaves.
- Keep a recovery path. Know how to reach your provider's web console or rescue mode in case you ever lock yourself out.
Wrapping up
Setting up SSH keys on a Linux VPS takes about ten minutes and removes the single most common way servers get broken into: guessed or reused passwords. Generate an Ed25519 key with a passphrase, install the public half for a non-root user, test it in a fresh window, and only then switch off password logins. Add a tool like fail2ban on top and your SSH port becomes a much less interesting target.