Skip to content

Configure an SSH Key After Deploying a VPS

Set up SSH key authentication on an existing Linux VPS that was originally deployed with password authentication.

SSH keys provide a secure way to access your VPS without sending your account password during every login. A key pair consists of:

  • A private key, stored securely on your local computer.
  • A public key, added to the VPS.

Important

The private key must never be uploaded, shared, or sent to another person. Only the public key should be copied to the VPS.

Configure SSH key authentication

1

Check for an existing SSH key

Before generating a new SSH key, check whether one already exists on your computer.

Windows PowerShell

Get-ChildItem $HOME\.ssh

Linux and macOS

ls -la ~/.ssh

Look for a matching pair of files, such as:

id_ed25519
id_ed25519.pub

The file ending in .pub is the public key.

The file without .pub is the private key.

Never share your private key

Never share the private key. Only copy or share the file ending in .pub.

If you already have a key pair, continue to the next step. Otherwise, generate an SSH key.

2

Display the public key

Display the public key so that it can be copied to the VPS.

Windows PowerShell

Get-Content $HOME\.ssh\id_ed25519.pub

Linux and macOS

cat ~/.ssh/id_ed25519.pub

The output should be one long line beginning with:

ssh-ed25519

For example:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... [email protected]

Copy the entire line, including:

  • The key type.
  • The encoded key.
  • The optional comment at the end.

Do not copy the private key

Do not copy the private key. Only copy the file ending in .pub.

3

Connect to the VPS with your password

Connect to the existing VPS using its current password:

ssh username@your-server-ip

For example:

Enter the current VPS password when prompted.

4

Create the SSH directory

After connecting to the VPS, create the .ssh directory for the account:

mkdir -p ~/.ssh

Set the correct permissions:

chmod 700 ~/.ssh

The .ssh directory belongs to the account currently logged in.

For example:

  • When logged in as root, the directory is /root/.ssh.
  • When logged in as another user, it is stored inside that user's home directory.
5

Add the public key

Open the authorized_keys file:

nano ~/.ssh/authorized_keys

Paste the complete public key on a new line.

It should appear as one continuous line:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA... [email protected]

Do not split the key across multiple lines.

To save the file in Nano:

  1. Press Ctrl + O.
  2. Press Enter.
  3. Press Ctrl + X.

The authorized_keys file lists the public keys that are permitted to access that account.

6

Set the correct permissions

Set secure permissions on the authorized_keys file:

chmod 600 ~/.ssh/authorized_keys

Make sure the account owns the SSH directory and its files:

chown -R "$(id -un)":"$(id -gn)" ~/.ssh

Confirm the permissions:

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

The output should be similar to:

drwx------ 2 user user 4096 Jul 24 12:00 .ssh
-rw------- 1 user user  100 Jul 24 12:00 authorized_keys

The exact username, group, size, and date will differ.

7

Restore SELinux security contexts

Skip this step on Ubuntu, Debian, and FreeBSD

Ubuntu, Debian, and FreeBSD users can skip this step.

Complete this step on:

  • AlmaLinux
  • CentOS Stream
  • Oracle Linux
  • Rocky Linux

If SELinux is enabled, restore the correct security contexts:

restorecon -RFv ~/.ssh

Check the SELinux status:

getenforce

Possible results include:

Enforcing
Permissive
Disabled

Running restorecon is particularly useful when the .ssh directory or authorized_keys file was created or moved manually.

8

Verify public key authentication is enabled

Check the effective SSH server configuration:

sudo sshd -T | grep pubkeyauthentication

Expected output:

pubkeyauthentication yes

You can also check the configured authorized-key location:

sudo sshd -T | grep authorizedkeysfile

The output will normally include:

authorizedkeysfile .ssh/authorized_keys

If public key authentication reports no, open the SSH server configuration:

sudo nano /etc/ssh/sshd_config

Add or update:

PubkeyAuthentication yes

Validate the configuration:

sudo sshd -t

If no output is displayed, the configuration syntax is valid.

Restart SSH using the command for your operating system.

Ubuntu and Debian

sudo systemctl restart ssh

AlmaLinux, CentOS Stream, Oracle Linux and Rocky Linux

sudo systemctl restart sshd

FreeBSD

sudo service sshd restart
9

Test SSH key authentication

Keep the existing SSH session open.

Open a second terminal on your local computer and connect again:

ssh username@your-server-ip

For example:

If you saved the key under its default name, the SSH client should detect it automatically.

To specify the private key manually, use:

Windows PowerShell

ssh -i $HOME\.ssh\id_ed25519 username@your-server-ip

Linux and macOS

ssh -i ~/.ssh/id_ed25519 username@your-server-ip

If you configured a custom SSH port:

ssh -i ~/.ssh/id_ed25519 -p 2222 username@your-server-ip

A successful connection without the VPS account password confirms that SSH key authentication is working.

You may still be asked for the SSH key's passphrase if one was configured.

10

Confirm that the correct key was used

For detailed connection information, use verbose mode:

ssh -v username@your-server-ip

To specify the key:

ssh -v -i ~/.ssh/id_ed25519 username@your-server-ip

Look for messages similar to:

Offering public key
Server accepts key
Authenticated using "publickey"

This confirms that the VPS accepted the public key.

11

Close the original session

Only close the original password-authenticated SSH session after confirming that the new SSH key works in a separate terminal.

Exit the original session:

exit

SSH key authentication is now configured.

Use ssh-copy-id

Linux and macOS users may be able to copy the public key automatically with ssh-copy-id:

ssh-copy-id username@your-server-ip

To specify a particular public key:

ssh-copy-id -i ~/.ssh/id_ed25519.pub username@your-server-ip

For a custom SSH port:

ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 username@your-server-ip

Enter the current VPS password when prompted.

The command creates or updates:

~/.ssh/authorized_keys

Test the connection afterward:

ssh username@your-server-ip

Using ssh-copy-id

The manual method remains useful when ssh-copy-id is unavailable or when you are accessing the VPS through a provider console.

Summary

You have:

  • Checked for an existing SSH key.
  • Copied the public key to the deployed VPS.
  • Secured the .ssh directory and authorized_keys file.
  • Verified that public key authentication is enabled.
  • Tested the SSH key in a separate terminal.
  • Confirmed that the server accepted the key.

Your VPS can now be accessed securely using SSH key authentication.

Next step

You can now follow Disable SSH password authentication to prevent password-based SSH login.