📢 LordCloud support has moved to Ballastra — Join now →
VPS & Linux

SSH Key Login on a Linux VPS: Set It Up and Lock Out Passwords

A step-by-step guide to SSH key login on a Linux VPS, from generating the key to turning off password authentication, with the recovery plan nobody tells you to prepare.

LordCloud Engineering

Infrastructure & Support Team

Published · 6 min read
On this page

SSH key login on a Linux VPS means you prove who you are with a cryptographic key pair instead of typing a password. The short version: create an Ed25519 key on your own computer, install the public half on the server, confirm it works from a second terminal, and only then switch password logins off. That order matters, because the usual way people lock themselves out of a fresh VPS is by disabling passwords before the key actually works.

This guide walks through the whole process on Ubuntu or Debian, explains what each command does, and finishes with the checks and recovery habits that keep you out of trouble. If you have not yet got a server, you can pick one from the VPS hosting plans; everything below applies to any Linux VPS with root or sudo access.

Why SSH key login on a Linux VPS beats passwords#

A password is something a bot can guess. A key pair is two files: a private key that never leaves your computer, and a public key that you place on the server. When you connect, the server challenges your client, and only the holder of the private key can answer. Nothing secret crosses the network, and there is no password for a bot to brute-force.

Any internet-facing server on port 22 gets automated login attempts within hours of going online. You can see them yourself in /var/log/auth.log on Ubuntu or Debian. With password authentication off, those attempts fail immediately and cost you nothing. Combine that with a tool such as the one in our Fail2ban SSH setup guide to keep logs quiet, and a firewall as described in the firewall configuration docs.

Step 1: Generate an Ed25519 key pair#

Run this on your own laptop or desktop, not on the server. Windows 10 and 11, macOS and Linux all ship with OpenSSH tools, so the command is the same everywhere:

bash
ssh-keygen -t ed25519 -C "my-laptop-2026"

Ed25519 is the default key type in current OpenSSH, per the ssh-keygen manual. The -C flag only sets a label so you can recognise the key later; use something that names the device. When prompted for a file, accept the default (~/.ssh/id_ed25519). When prompted for a passphrase, set one. A passphrase encrypts the private key on disk, so a stolen laptop or a leaked backup does not hand over your servers. Your agent can cache it so you are not retyping it all day.

You now have two files:

  • ~/.ssh/id_ed25519 is the private key. Never share it, never upload it, never paste it into a chat.
  • ~/.ssh/id_ed25519.pub is the public key. This one is safe to copy anywhere.

Ed25519 or RSA?#

Choose Ed25519 for anything new. The keys are short, quick to verify and supported by every maintained OpenSSH version. RSA still works, but if you use it, pick 3072 bits or more (ssh-keygen -t rsa -b 3072). The only real reason to use RSA is connecting to very old network gear or software that predates Ed25519.

Step 2: Install the public key on the VPS#

The simplest route is ssh-copy-id, which appends your public key to the right file on the server. You log in once with your password to do it:

bash
ssh-copy-id -i ~/.ssh/id_ed25519.pub youruser@YOUR_SERVER_IP

Replace youruser with the account you log in as and the address with your VPS IP. If you are new to connecting at all, the connecting through SSH page covers the basics, including clients on Windows.

If ssh-copy-id is not available (some Windows setups), do it by hand. Log in with your password, then run:

bash
mkdir -p ~/.ssh
chmod 700 ~/.ssh
nano ~/.ssh/authorized_keys     # paste the single line from id_ed25519.pub
chmod 600 ~/.ssh/authorized_keys

Each public key is one line in authorized_keys. Do not wrap it across lines.

Permissions are the number one gotcha#

OpenSSH refuses to use authorized_keys if the file, the ~/.ssh directory or your home directory is writable by other users. The sshd manual describes this strict-modes behaviour. The safe settings are 700 on ~/.ssh and 600 on authorized_keys, both owned by your user. If key login silently falls back to asking for a password, check ownership and permissions first:

bash
ls -ld ~ ~/.ssh ~/.ssh/authorized_keys

Step 3: Test key login before changing anything#

Open a new terminal window and connect:

bash
ssh -i ~/.ssh/id_ed25519 youruser@YOUR_SERVER_IP

If it logs you in with the key (asking only for the key passphrase, not the server password), it works. Keep your original session open while you do this. That first session is your safety net: as long as it stays connected, you can undo any change you make next.

If it still asks for the server password, add -v to see which keys the client offered and why the server rejected them:

bash
ssh -v -i ~/.ssh/id_ed25519 youruser@YOUR_SERVER_IP

Step 4: Disable password login on the VPS#

Only do this once step 3 succeeded. On Ubuntu and Debian, the main file /etc/ssh/sshd_config includes everything in /etc/ssh/sshd_config.d/*.conf, as described in the Ubuntu OpenSSH documentation. A small drop-in file is cleaner than editing the main file, because package upgrades will not overwrite it.

bash
sudo nano /etc/ssh/sshd_config.d/10-keys-only.conf

Put this inside:

text
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no

PasswordAuthentication controls plain password logins, and PubkeyAuthentication controls key logins; both are documented in the sshd_config manual. Turning off KbdInteractiveAuthentication closes the keyboard-interactive route, which on some setups can still prompt for a password.

Heads-up: sshd uses the first value it finds for a setting, and files in sshd_config.d are read in alphabetical order. Some cloud images ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. A lower-numbered name like 10-keys-only.conf is read earlier and wins. Check for conflicts with:

bash
sudo grep -ri "PasswordAuthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/

Now validate the configuration and reload:

bash
sudo sshd -t
sudo systemctl restart ssh

sshd -t runs a test mode that checks the configuration for errors without starting anything. If it prints nothing, the syntax is fine. On Ubuntu the service is called ssh; on some other distributions it is sshd.

Finally, from a third terminal, confirm that a password attempt is now rejected and that your key still works:

bash
ssh -o PubkeyAuthentication=no youruser@YOUR_SERVER_IP

That should end in Permission denied (publickey). If it does, password login is off.

What about root login?#

The default for PermitRootLogin in current OpenSSH is prohibit-password, which means root can only log in with a key, never a password (see the sshd_config manual). Many admins go further, create a normal user with sudo, and set PermitRootLogin no. That is a reasonable hardening step, but make sure your sudo user can log in by key first. The root access docs and security hardening guide cover how this fits into a wider checklist.

Recovery: what to do when you lock yourself out#

Plan for this before you need it:

  • Keep a second key. Add a backup key (for example from a second device or a hardware token) to authorized_keys. Store the backup private key offline.
  • Back up the private key in an encrypted password manager or encrypted drive. A lost key plus disabled passwords means you cannot log in over SSH.
  • Know your out-of-band access. Check your provider's control panel for a console or rescue option before you need it. If you are a LordCloud customer, support is reachable through the contacting support page.
  • Change one thing at a time. If you change the SSH port, the firewall and authentication in the same sitting, you will not know which one broke access.

A short habit list#

  1. One key per device, with a passphrase.
  2. Remove old keys from authorized_keys when you retire a laptop.
  3. Keep the server updated and the firewall enabled.
  4. Check sudo journalctl -u ssh --since today now and then for strange failures.

Frequently asked questions#

Do I need to restart the whole VPS after changing SSH settings?#

No. Restarting the ssh service is enough, and existing sessions stay connected, which is exactly why keeping your first window open is such a useful safety net.

Does SSH key login work with Windows?#

Yes. Modern Windows includes an OpenSSH client, so ssh-keygen and ssh work in PowerShell. Graphical clients such as PuTTY can also use keys, though they use their own key format and converter tools.

SSH keys are one of the cheapest security upgrades you can make: a few minutes of work and an entire attack category disappears. Once it is in place, move on to a firewall, updates and fail2ban, and your VPS will be in far better shape than the default install.

Frequently asked questions

How do I set up SSH key login on a Linux VPS?

Generate a key pair on your own computer with ssh-keygen -t ed25519, copy the public key to the VPS with ssh-copy-id, confirm you can log in with the key in a second terminal, and only then set PasswordAuthentication no in an sshd_config.d drop-in file.

Is Ed25519 better than RSA for SSH keys?

For new keys, Ed25519 is the sensible default: keys are short, fast and supported by every current OpenSSH release. Use RSA with 3072 bits or more only if you must connect to very old software that does not understand Ed25519.

What if I lose my private key?

You cannot recover it. If password login is already disabled, you need another way in, such as the provider's console or a rescue option. Keep an encrypted backup of the key and consider registering a second key on the server.

Should I disable password login on SSH?

Yes, once key login is confirmed working. Public servers receive constant automated password guesses, and removing password authentication ends that whole class of attack. Always test the key from a second session before closing your first one.

Can I use the same SSH key on several servers?

Yes. The public key can be installed on any number of servers. For stricter separation, use one key per device or per role, and remove a key from authorized_keys when a device is retired.

References

Drafted with AI assistance and checked against LordCloud's published product information. Spotted a mistake?Suggest an edit.

All articles →
Have a question? Chat with us👋