Browse help topics

Security

How to Set Up SSH Key Authentication

Create a key pair on your computer with ssh-keygen, add the public key to ~/.ssh/authorized_keys on your VPS, then test it in a new window while your current session stays open. Keep the private key on your computer and protect it with a passphrase.

  • Updated
  • Last reviewed
  • Intermediate
  • About 15 minutes

Applies to: Linux OpenSSH Blue Arctic VPS Hosting and Virtual Dedicated Servers (VDS); Windows, macOS and Linux computers

On this page
  1. Before you start
  2. Step 1: Create a key on your computer
  3. Step 2: Add the public key to your VPS
  4. On a Mac or Linux computer
  5. On Windows
  6. Step 3: Test in a second window
  7. How to tell it worked
  8. How to undo it
  9. Troubleshooting
  10. Still need help?

An SSH key is a pair of files. The private key stays on your computer and works like a password that can’t be guessed. The public key goes on the server. This guide adds a key alongside your password; it doesn’t switch password login off.

Before you start

  • Warning: Connect to your VPS as the user who will use the key, and keep that window open until you’ve tested the key. It’s your way back in if something goes wrong.
  • Before you change any other SSH or firewall settings, open a support ticket: a wrong setting can lock you out. We’ll tell you whether the work is covered by your plan.
  • Replace username, 203.0.113.10 and PORT with the login details we gave you when your server was set up. If your port is 22, leave out -p PORT.

Step 1: Create a key on your computer

Warning

If a key already exists, ssh-keygen asks Overwrite (y/n)?. Always type n and press Enter: y destroys your existing key. Use that key and skip to Step 2.

  1. Open a new PowerShell window (Windows) or Terminal window (Mac or Linux) on your own computer. Don’t use the window that’s connected to your server.
  2. Run this. It creates the key pair on your computer only; -C adds a label to help you recognize the key.
    ssh-keygen -t ed25519 -C "you@yourdomain.com"
  3. Press Enter to accept the suggested file location.
  4. Type a passphrase and press Enter, then type it again. Nothing appears while you type. Keep the passphrase safe, such as in a password manager: it can’t be recovered.

You now have id_ed25519 (the private key: never share it, even with us) and id_ed25519.pub (the public key, which you copy) in your .ssh folder.

Step 2: Add the public key to your VPS

On a Mac or Linux computer

  1. In the window from Step 1, run this. It adds your public key to ~/.ssh/authorized_keys on the server and keeps keys already there.
    ssh-copy-id -i ~/.ssh/id_ed25519.pub -p PORT username@203.0.113.10
  2. Type your server password when asked. Lines that start with INFO: are normal. Near the end you’ll see Number of key(s) added: followed by 1.

On Windows

  1. In the PowerShell window from Step 1, run this to copy your public key to the clipboard:
    Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard
  2. Switch to the window that’s connected to your server.
  3. Run mkdir -p ~/.ssh. This creates the .ssh folder if it’s missing.
  4. Run chmod 700 ~/.ssh. This lets only you open the folder.
  5. Warning: This command needs two > signs; with one, it would erase the keys already in the file. Run cat >> ~/.ssh/authorized_keys. The cursor waits on an empty line.
  6. Press Enter once, then right-click in the window to paste your key.
  7. Check the paste: one long line (it may wrap onto a second row) that starts with ssh-ed25519 and ends with your label. If it looks right, press Enter. If not, press Ctrl+C and start this list again.
  8. Press Ctrl+D. This saves the key and brings your prompt back.
  9. Run chmod 600 ~/.ssh/authorized_keys. This lets only you change the file.

Step 3: Test in a second window

  1. Leave the window that’s connected to your server open.
  2. Open a new PowerShell or Terminal window and connect as usual, for example ssh -p PORT username@203.0.113.10.
  3. If you see Enter passphrase for key, type your key’s passphrase and press Enter. If you see username@203.0.113.10's password: instead, the key isn’t working: press Ctrl+C and see Troubleshooting.

How to tell it worked

You’re logged in without typing the server password. Now you can close the first window.

How to undo it

Warning

If the server doesn’t accept your password, removing your key can lock you out. First, in a new window, check that password login still works: ssh -o PubkeyAuthentication=no -p PORT username@203.0.113.10. If it doesn’t, keep the key and open a support ticket.

  1. In your open session, make a copy first: cp -p ~/.ssh/authorized_keys ~/.ssh/authorized_keys.backup.
  2. Run awk '{print NR, $NF}' ~/.ssh/authorized_keys. It only reads: it prints each line’s number and last word (the key’s label; a key with no label shows as a long run of letters, and an empty line shows only its number).
  3. Note the number in front of your label (the last word in your .pub file), for example 4 you@yourdomain.com. If your label is missing or appears twice, stop and open a support ticket.
  4. Run nano +4 ~/.ssh/authorized_keys with your number in place of 4. This opens the nano editor with the cursor on that line. If you see nano: command not found, open a support ticket.
  5. Press Ctrl+E. The line’s end should show your label; if not, press Ctrl+X to leave.
  6. Press Ctrl+K once to remove that line. Don’t change any other line.
  7. Press Ctrl+O, then Enter to save, then Ctrl+X to exit.
  8. Run the awk command from step 2 of this list again. Your label should be gone and the other lines unchanged. If the wrong line went, put the copy back: cp -p ~/.ssh/authorized_keys.backup ~/.ssh/authorized_keys.

Troubleshooting

  • You’re still asked for the server password: the key wasn’t added for this user. Check you used the same username in Steps 2 and 3. The server also ignores the key if other users can change your files: in your open session, run chmod go-w ~, chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys.
  • You lost the private key or think someone copied it: remove its line from authorized_keys (see How to undo it) and create a new key.

Still need help?

Open a support ticket from the Client Portal at my.bluearctic.com. Our support page explains how the 24/7 helpdesk works. Include your VPS IP address, the username, the output of your connection attempt with -v added, and what you already tried. Never share your private key (the file without .pub). Never include a password.

Was this article helpful?

Report outdated instructions or suggest an improvement
Type of report

Please do not include passwords or personal information.

Still need help?

Our support team can look into your account and walk you through it.