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
Check for an existing SSH key
Before generating a new SSH key, check whether one already exists on your computer.
Windows PowerShell
Linux and macOS
Look for a matching pair of files, such as:
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.
Display the public key
Display the public key so that it can be copied to the VPS.
Windows PowerShell
Linux and macOS
The output should be one long line beginning with:
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.
Connect to the VPS with your password
Connect to the existing VPS using its current password:
For example:
Enter the current VPS password when prompted.
Create the SSH directory
After connecting to the VPS, create the .ssh directory for the account:
Set the correct permissions:
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.
Add the public key
Open the authorized_keys file:
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:
- Press Ctrl + O.
- Press Enter.
- Press Ctrl + X.
The authorized_keys file lists the public keys that are permitted to access that account.
Set the correct permissions
Set secure permissions on the authorized_keys file:
Make sure the account owns the SSH directory and its files:
Confirm the permissions:
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.
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:
Check the SELinux status:
Possible results include:
Running restorecon is particularly useful when the .ssh directory or authorized_keys file was created or moved manually.
Verify public key authentication is enabled
Check the effective SSH server configuration:
Expected output:
You can also check the configured authorized-key location:
The output will normally include:
If public key authentication reports no, open the SSH server configuration:
Add or update:
Validate the configuration:
If no output is displayed, the configuration syntax is valid.
Restart SSH using the command for your operating system.
Ubuntu and Debian
AlmaLinux, CentOS Stream, Oracle Linux and Rocky Linux
FreeBSD
Test SSH key authentication
Keep the existing SSH session open.
Open a second terminal on your local computer and connect again:
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
Linux and macOS
If you configured a custom SSH port:
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.
Confirm that the correct key was used
For detailed connection information, use verbose mode:
To specify the key:
Look for messages similar to:
This confirms that the VPS accepted the public key.
Use ssh-copy-id
Linux and macOS users may be able to copy the public key automatically with ssh-copy-id:
To specify a particular public key:
For a custom SSH port:
Enter the current VPS password when prompted.
The command creates or updates:
Test the connection afterward:
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
.sshdirectory andauthorized_keysfile. - 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.