How to set up SSH keys for your VPS

Secure Shell (SSH) keys are a vital tool for anyone looking to enhance security while accessing their Virtual Private Server (VPS). Unlike traditional password-based authentication, SSH keys offer a more secure and efficient method by utilizing cryptographic algorithms to create a pair of keys—a public key and a private key. This article will guide you through the process of setting up SSH keys and connecting to your VPS, whether you’re using Linux, macOS, or Windows. By the end of this guide, you’ll have a solid understanding of how to establish a secure connection to your server, minimizing the risks associated with unauthorized access.

This article is part of Rackset’s Server Administration guides — browse the rest of the category for more in-depth tutorials on this topic.

We’ll cover everything from generating SSH keys on different operating systems to adding your public key to your VPS for seamless access. For Linux and macOS users, we’ll delve into using the terminal for key generation and configuration, while Windows users will learn how to utilize tools like PuTTY and Windows Subsystem for Linux (WSL). Whether you’re a beginner or looking to refine your VPS security practices, this comprehensive guide will equip you with the knowledge to securely manage and connect to your virtual server environment. Let’s get started on setting up SSH keys for your VPS, ensuring a safer and more reliable connection.

Generate an ED25519 SSH Key Pair

Generating an ED25519 SSH key pair is a crucial step in setting up secure, passwordless authentication for your VPS. The ED25519 algorithm is recommended due to its robustness and efficiency. Below are the steps to generate an ED25519 key pair on Linux, macOS, and Windows.

On Linux and macOS

  1. Open your terminal.
  2. Run the following command to generate a new ED25519 SSH key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. You will be prompted to enter a file path to save the key pair. Press Enter to accept the default location ~/.ssh/id_ed25519.
  2. Next, you will be asked to enter a passphrase. A passphrase adds an extra layer of security. Enter a strong passphrase or press Enter to skip.
  3. Once completed, your ED25519 SSH key pair will be stored in the specified location.

On Windows

For Windows, you can use a terminal application like Git Bash or Windows Subsystem for Linux (WSL) to generate the SSH key pair.

  1. Open Git Bash or WSL.
  2. Execute the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. When prompted, specify the location to save the key pair or press Enter to use the default path C:UsersYourUsername.sshid_ed25519.
  2. Enter a passphrase for added security or press Enter to continue without one.
  3. Your ED25519 SSH key pair will be generated and stored in the specified location.

After generating your SSH key pair, you are ready to add the public key to your VPS for secure access.

Copy Your Public Key to the VPS Using ssh-copy-id

Generating an SSH key pair

Once you’ve generated your SSH key pair, the next step is to copy your public key to your VPS. This allows you to authenticate without a password by using the ssh-copy-id command. This command simplifies the process of installing your public key on the remote server. Below are the steps for Linux and macOS systems. For Windows users, a different approach is required, which will be covered later.

Steps for Linux and macOS

  1. Open a Terminal: Launch the terminal application on your Linux or macOS system.
  2. Run the ssh-copy-id command: Use the following command to copy your public key to the VPS:
    ssh-copy-id username@your_vps_ip
    • Replace username with your actual username on the VPS.
    • Replace your_vps_ip with the IP address of your VPS.
  3. Enter your password: You will be prompted to enter the password for the specified user on the VPS. This is the only time you’ll need to enter your password for SSH access.
  4. Confirm key installation: Once the password is entered correctly, the ssh-copy-id command will copy your public key to the VPS and automatically configure the server to accept the key for authentication.

Verify the SSH Key Installation

After copying the public key, verify the setup by attempting to log in to your VPS using SSH:

ssh username@your_vps_ip

If the setup was successful, you should be able to log in without needing to enter your password.

Using ssh-copy-id is a straightforward way to enhance the security of your VPS by employing SSH keys, which are more secure than password-based authentication.

Connect to Your VPS from Linux

Connecting to your VPS from a Linux machine using SSH keys is a straightforward process. This method enhances security by eliminating the need for password-based logins. Follow the steps below to establish a secure connection:

If you need this service, Rackset’s team provides professional Linux VPS with full support so you can rely on the quality and get set up quickly.

1. Open a Terminal

Begin by opening a terminal on your Linux machine. You can usually find the terminal application in your system’s applications menu, or you can use a keyboard shortcut like Ctrl + Alt + T.

2. Locate Your SSH Key Pair

Ensure that you have your SSH key pair available. Typically, these are stored in the ~/.ssh/ directory. You should have a private key (e.g., id_rsa) and a public key (e.g., id_rsa.pub).

3. Set Permissions for Your Private Key

To ensure security, verify that your private key has the correct permissions. Use the following command to set the permissions:

chmod 600 ~/.ssh/id_rsa

4. Connect to Your VPS

Use the SSH command to connect to your VPS. Replace username with your VPS username and vps_ip_address with the IP address of your VPS:

ssh username@vps_ip_address

If this is your first connection, you may be prompted to confirm the authenticity of the host. Type yes to proceed.

5. Verify Connection

Once connected, you should see a command prompt for your VPS. You can now execute commands remotely. To disconnect, simply type:

exit

By following these steps, you ensure a secure and efficient connection to your VPS from a Linux environment using SSH keys.

Connect to Your VPS from macOS

Connecting securely to your VPS

Connecting to your VPS from a macOS system using SSH keys is a straightforward process. Follow these steps to set up a secure connection.

Step 1: Open Terminal

On your macOS, open the Terminal application. You can find it by navigating to Applications > Utilities > Terminal.

Step 2: Generate SSH Keys

If you haven’t already generated SSH keys, use the following command to create them:

For projects that need guaranteed resources and full root control, a VPS is the right step up from shared hosting. With VPS hosting you get dedicated CPU/RAM, NVMe storage, a dedicated IP and high uptime, deployed in minutes.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Press Enter to save the key in the default location (~/.ssh/id_rsa). You can choose to enter a passphrase for additional security or leave it empty for no passphrase.

Step 3: Copy the Public Key to Your VPS

Use the ssh-copy-id command to copy your public key to the VPS. Replace user with your VPS username and server_ip with your VPS IP address:

ssh-copy-id user@server_ip

If prompted, enter your VPS password to complete the process.

Step 4: Connect to Your VPS

Once the public key is copied, connect to your VPS using the following command:

ssh user@server_ip

If you set a passphrase during key generation, you will be prompted to enter it. Otherwise, you will directly access your VPS.

Troubleshooting Connection Issues

  • Ensure that SSH access is enabled on your VPS.
  • Confirm that your firewall settings allow SSH connections.
  • Double-check the username and IP address for any typing errors.
  • Verify that the public key is correctly added to the ~/.ssh/authorized_keys file on your VPS.

Following these steps will help you securely connect to your VPS from macOS using SSH keys.

Connect to Your VPS from Windows Using PowerShell and PuTTY

Prerequisites

Before you can connect to your VPS from Windows, ensure you have the following:

  • PuTTY and PuTTYgen installed on your Windows machine.
  • Your VPS’s IP address.
  • SSH private key file generated and saved on your system.

Using PowerShell to Convert SSH Keys

If your SSH keys are in OpenSSH format, you might need to convert them to a format compatible with PuTTY. Follow these steps:

  1. Open PuTTYgen.
  2. Click on Load and select your OpenSSH private key file.
  3. Click Save private key to save the key in PuTTY’s .ppk format.
  4. Name the file and store it in a secure location.

Connecting with PuTTY

With your SSH key converted, you can now connect to your VPS using PuTTY:

For heavy workloads and maximum performance, a dedicated bare-metal server gives you an entire physical machine. Rackset’s dedicated server deliver full hardware, root access and configurable port speeds for the most demanding sites and apps.

  1. Open PuTTY.
  2. In the Session category, enter your VPS’s IP address in the Host Name (or IP address) field.
  3. In the Connection category, expand SSH and select Auth.
  4. Click Browse and select the .ppk file you saved earlier.
  5. Return to the Session category and click Open.
  6. If prompted by a security alert, select Yes to continue connecting.
  7. Log in with your VPS username when prompted.

Troubleshooting Tips

  • If you encounter issues, ensure that your VPS firewall allows SSH connections on the default port 22.
  • Verify the IP address and username are correct.
  • Ensure your private key file is in the correct format and not corrupted.

Harden Your VPS by Disabling Password Authentication

After setting up SSH keys, an essential step to enhance the security of your VPS is to disable password authentication. This ensures that only users with the correct SSH key can access your server, significantly reducing the risk of unauthorized access. Follow the steps below to disable password authentication on your VPS.

Step 1: Access Your VPS

First, connect to your VPS using the SSH key you have set up. You can do this through a terminal on Linux and macOS, or using an SSH client like PuTTY on Windows. Rackset provides a seamless connection experience with its robust infrastructure, ensuring you can access your server reliably.

Step 2: Edit the SSH Configuration File

Once connected, you will need to edit the SSH configuration file to disable password authentication. Use the following command to open the file in a text editor such as nano:

sudo nano /etc/ssh/sshd_config

Step 3: Disable Password Authentication

  • In the sshd_config file, locate the line that reads #PasswordAuthentication yes.
  • Uncomment the line by removing the # symbol.
  • Change yes to no so the line reads PasswordAuthentication no.

Step 4: Save and Close the File

Save your changes and close the text editor. If using nano, you can do this by pressing CTRL + X, then Y to confirm, and Enter to exit.

Step 5: Restart the SSH Service

For the changes to take effect, restart the SSH service with the following command:

sudo systemctl restart sshd

On Rackset’s servers, this command ensures that your configuration changes are applied swiftly, enhancing your server’s security immediately.

Conclusion

By disabling password authentication, you fortify your VPS against unauthorized access attempts. Rackset’s hosting solutions support these security enhancements, providing a secure environment for your applications and data.

Need expert help from Rackset?

Want this done fast, professionally and hassle-free? Rackset’s experienced team and full support are here to help you get the best result.

Buy Linux VPS

Conclusion

Setting up SSH keys for your VPS enhances security by replacing traditional password logins with cryptographic keys, providing a safer and more efficient way to access your server. Whether you’re using Linux, macOS, or Windows, the process involves generating a key pair, configuring your SSH client, and ensuring that your server recognizes your public key. This method not only strengthens your server’s security but also streamlines your workflow by allowing password-less logins.

By following the steps outlined in this guide, you can confidently manage your VPS with enhanced security and ease. As you consider your hosting needs, Rackset offers reliable and secure VPS solutions that are perfectly suited for leveraging SSH key authentication. Explore Rackset’s hosting options today to ensure your online projects are both secure and efficient.

Frequently Asked Questions

What are SSH keys?

SSH keys are cryptographic keys used for secure access to a server via the SSH protocol. They consist of a private key, which is kept secret, and a public key, which is shared with the server.

Why use SSH keys instead of passwords?

SSH keys provide a more secure and convenient way to access your server compared to passwords. They eliminate the risk of brute force attacks and offer automated logins without the need to enter a password each time.

How do I generate SSH keys on Linux or macOS?

Use the command `ssh-keygen -t rsa -b 4096` in the terminal to generate a new SSH key pair. Follow the prompts to save the keys in the default location and optionally add a passphrase for extra security.

Can I use SSH keys on Windows?

Yes, you can use SSH keys on Windows by using tools like PuTTYgen or the Windows Subsystem for Linux (WSL). These tools allow you to generate SSH keys and manage connections to your VPS.

How do I add my SSH public key to my VPS?

Copy your SSH public key to the `~/.ssh/authorized_keys` file on your VPS. You can use the `ssh-copy-id` command or manually transfer the key using a secure method like SCP or SFTP.

Leave a Reply

Your email address will not be published. Required fields are marked *

Count: 74 - Average: 4.7

Rating Submitted.