Log in to your VPS with an SSH key instead of a password, then turn password login off

· 6 min read

Make an SSH key on Windows, macOS or Linux, add it to your server, test it, then switch off password login. Plus how to get back in if you lock yourself out.

Why switch to a key

Your server starts with a generated root password, shown once on your access page. That works, but any server on the internet sees a steady stream of password guesses against SSH. A key pair removes the target: the private key stays on your computer, the server only holds the public half, and once password login is off there is nothing left to guess. The whole switch takes about ten minutes, and the browser console on your access page is the way back in if something goes wrong.

Make a key on your own computer

Windows 10 and 11, macOS and Linux all include the OpenSSH tools, so there is nothing to install. Open PowerShell on Windows, or Terminal on macOS and Linux, and run the same command. Press Enter to accept the file location, then choose a passphrase: it protects the key if your computer is ever lost.

ssh-keygen -t ed25519 -C ""
# Creates two files in your .ssh folder:
#   id_ed25519       the private key: never share it or copy it to a server
#   id_ed25519.pub   the public key: this is what goes on the server

Add the public key to the server

Use the root password from your access page one last time. Replace 203.0.113.10 with your server’s IP address. On macOS and Linux, ssh-copy-id does the whole job:

ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]

Windows has no ssh-copy-id, so pipe the key over in one PowerShell line instead:

type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Test a key login in a second window

Keep your current session open, open a second window, and log in again. It should ask for your key’s passphrase, if you set one, and not for the server’s password. Do not go on until this works: the next step turns passwords off, and the open session is your safety net.

ssh -i ~/.ssh/id_ed25519 [email protected]

Turn off password login

On the server, add a small settings file that allows keys only. First check that your image reads the sshd_config.d folder: the grep should print an Include line. Current Ubuntu, Debian, Fedora, Rocky Linux, AlmaLinux and CentOS Stream images do. If it prints nothing, put the same two lines near the top of /etc/ssh/sshd_config instead.

grep -i '^Include' /etc/ssh/sshd_config

cat > /etc/ssh/sshd_config.d/00-keys-only.conf <<'EOF'
PasswordAuthentication no
PermitRootLogin prohibit-password
EOF

# Check the syntax, then restart SSH. The service is "ssh" on Ubuntu and Debian
# and "sshd" on Fedora, Rocky Linux, AlmaLinux and CentOS Stream.
sshd -t && (systemctl restart ssh 2>/dev/null || systemctl restart sshd)

Now confirm passwords are refused. This command forces a password attempt; the answer should be Permission denied (publickey).

ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password [email protected]

Your first session can now be closed. Keep the root password anyway: it no longer works over SSH, but it is what the browser console asks for.

If you lock yourself out

  • Open your access page and press Console. It shows the server’s own screen in the browser, which is not SSH, so the root password still works there.
  • Lost the root password too? Use Reset password on the access page, then reveal the new one; it is shown once.
  • Logged in at the console, either add your public key to /root/.ssh/authorized_keys again, or delete /etc/ssh/sshd_config.d/00-keys-only.conf and restart SSH to allow passwords while you sort it out.
  • Lost your private key? Make a new pair as above and add the new public key from the console. The old key cannot be recovered, and it does not need to be.

Ready to run this yourself?

Linux KVM servers with NVMe storage in the EU, US and Singapore. Root access arrives by magic link; no account, no KYC. How paying this way works, plans and prices on one page: Crypto VPS hosting.

Configure a server — pay with Bitcoin, no account needed
Log in with an SSH key and turn off password login | BitVPS