How to Add SSH Keys to New or Existing VPS
How to Add SSH Keys to New or Existing VPS
When managing a Virtual Private Server (VPS), security is a top priority, and one of the most secure ways to authenticate to your server is by using SSH keys. SSH keys provide a passwordless, secure method of logging into your server by utilizing public-key cryptography. This guide will walk you through the process of adding SSH keys to both new and existing VPS environments, ensuring a seamless and secure login experience.
What Are SSH Keys?
SSH keys are a pair of cryptographic keys—a public key and a private key—that are used for authentication. The public key is stored on the server, and the private key remains on your local machine. When you attempt to log in, the server checks the public key against the private key, and if they match, you are granted access without needing to enter a password. SSH keys offer a more secure method than traditional passwords, which are susceptible to brute-force attacks.
Why Use SSH Keys?
- Increased Security: SSH keys use cryptographic authentication, which is much more secure than passwords.
- Passwordless Authentication: Once SSH keys are configured, you can log into your VPS without needing to type a password.
- Automation: SSH keys are ideal for automating tasks such as backups, remote file transfers, and system administration scripts.
Prerequisites
Before we start, ensure you have:
- Access to a VPS (either a new or existing one).
- SSH installed on your local machine (usually installed by default on macOS and Linux).
- An SSH key pair. If you don’t have one, we’ll cover how to generate it.
Adding SSH Keys to a New VPS
If you’re setting up a new VPS, many hosting providers allow you to add an SSH key during the VPS creation process. Here’s how you can do it:
Step 1: Generate an SSH Key Pair (If You Don’t Have One)
If you don’t already have an SSH key pair, you can generate one using the following command in your local terminal (on macOS or Linux):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This command creates a new SSH key using a 4096-bit RSA encryption and adds your email as a comment to the key. Follow the on-screen prompts to save the key to the default location (~/.ssh/id_rsa) and, optionally, add a passphrase for an extra layer of security.
The generated files are:
- id_rsa: The private key (never share this).
- id_rsa.pub: The public key (this is what you upload to your VPS).
To display the public key, run the following command and copy the output
cat ~/.ssh/id_rsa.pub
Step 2: Log Into Your VPS Provider
First of all, you need to log in into your VPS account. Most providers give you the option to add an SSH key during the VPS setup.
Step 3: Create a New VPS and Add the SSH Key
During the VPS creation process:
- Select the operating system (usually Linux-based, such as Ubuntu or CentOS).
- Look for the option to Add SSH Key or Authentication Method.
- Paste the public SSH key (id_rsa.pub) into the provided field.
- Complete the VPS setup by choosing a plan, region, and other options.
Once the VPS is created, it will automatically be configured to allow SSH access using your SSH key.
Step 4: Connect to the VPS
Now that your VPS is configured with your SSH key, you can connect to it using the following command (replace your_vps_ip with your VPS’s IP address):
ssh root@your_vps_ip
If everything is set up correctly, you’ll b
e logged into your VPS without needing to enter a password.
Adding SSH Keys to an Existing VPS
If you already have an existing VPS but are currently using password-based authentication, you can add SSH keys to secure future logins. Here’s how to do it:
Step 1: Generate an SSH Key Pair (If Needed)
If you haven’t already generated an SSH key pair, run the following command on your local machine to create one:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Copy the public key (id_rsa.pub) by running:
cat ~/.ssh/id_rsa.pub
Step 2: Log into Your VPS
To add an SSH key to an existing VPS, you’ll need to log in using the current method (likely with a password). Use the following command to connect to your VPS (replace your_vps_ip with the actual IP address):
ssh root@your_vps_ip
Enter your password when prompted.
Step 3: Create the .ssh Directory (If Not Present)
Once you are logged into your VPS, check if the .ssh directory exists. If not, create it by running the following commands:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
The .ssh directory is where your public key will be stored.
Step 4: Add the Public Key to the authorized_keys File
Next, you need to add your public key to the authorized_keys file, which is used by SSH to authenticate your login attempts.
- Open the authorized_keys file (create it if it doesn’t exist) using a text editor like nano:
nano ~/.ssh/authorized_keys
- Paste your public SSH key (copied from the cat ~/.ssh/id_rsa.pub command) into the file.
- Save and close the file by pressing Ctrl + X, then Y, and hit Enter.
- Set the correct permissions on the authorized_keys file to ensure it is only accessible by the current user:
chmod 600 ~/.ssh/authorized_keys
Step 5: Test SSH Key Authentication
After adding the SSH key, exit the current session by typing:
exit
Now, try logging into your VPS again using SSH:
ssh root@your_vps_ip
If the key was successfully added, you should be logged in without being prompted for a password.
Step 6: Disable Password Authentication (Optional)
For added security, you can disable password authentication to ensure only SSH keys can be used to log in. To do this:
- Open the SSH configuration file on your VPS:
nano /etc/ssh/sshd_config
- Find the following lines and change them to:
PasswordAuthentication no
PubkeyAuthentication yes
- Save the file and restart the SSH service to apply the changes:
systemctl restart sshd
This will disable password-based logins, allowing only users with a valid SSH key to access the server.
Conclusion
Using SSH keys is one of the most effective ways to secure access to your VPS, eliminating the need for traditional password-based authentication. Whether you’re setting up a new VPS or securing an existing one, adding SSH keys is a straightforward process that greatly improves security. By following this guide, you can easily configure SSH keys on your VPS, automate secure logins, and ensure a higher level of protection for your server and data.