Why Should You Set Up SSH Keys?

In this article, you will learn how to connect to your application using the SSH Keys on Windows and Linux/macOS.

Cloudways Product avatar
Written by Cloudways Product
Updated over a week ago

Table of Contents

You can connect to your application using the username and password, which is the traditional and commonly used method. Alternatively, you can also connect to your application using the SSH keys, also known as Password-less SSH. SSH key pairs offer a more secure way of logging into your server than a password that can easily be cracked with a dictionary and brute force attacks. SSH keys are very hard to decipher with these attacks.

In this article, you will learn how to connect to your application using the SSH Keys on Windows and Linux/macOS.

How to Set Up SSH Keys

Generating the SSH key provides you with a pair of keys; a public key and the private key. The public key is placed on the server and the server is connected via an SSH client which already has a private key configured. When both keys match up, you can log in to your server without the need for a password. You can also enhance security by adding the passphrase to your SSH key pair.

First of all, you need to generate the SSH key pair and then add those to the server. The process of generating the SSH key pair is different on Windows and Linux/macOS.

Important

Please note that the Master user has SSH access enabled by default; however, you need to manually enable SSH access for the application user(s).

SSH Keys — Generating an SSH Key Pair on Microsoft Windows

For the purpose of this demonstration, we are using a free third-party tool called PuTTYgen (We recommend you download the full installer and install the entire Putty Suite package).

  1. Launch PuTTYgen.

2. Here, the default configurations should work, yet for greater security, we recommend increasing the number of bits from 2048 to 4096. Once ready, click Generate to get a public/private key pair.

3. Puttygen will require you to randomly move your mouse over the blank area to generate your unique key pair.

4. Copy your complete public key and save it on your computer, preferably in notepad as you will need it next step.

5. Next, save your private key by clicking Save Private Key. The private key should be saved .ppk format.

Important

Do not share your private key with anyone and save it somewhere safe.

Leave PuTTYgen open and move to add your public key to the server.

SSH Keys — Generate SSH Key Pair on Linux/macOS

The process to generate your SSH key pair is similar to Linux and macOS, since both operating systems originate from a Unix-based variant. You should not need to install anything, yet if you are running Linux (specifically any Debian based OS such as Ubuntu), and you have problems invoking the underneath commands, we recommend executing the following commands in the terminal:

sudo apt update
sudo apt install openssh-client -y

This will install all the required packages for you. Next, follow the instructions below to generate an SSH key pair.

1. Open your terminal and execute the following command to generate the SSH key pair.

ssh-keygen -t rsa

2. You will be prompted to type in the location where you would like to save the private key. If you leave it blank, then the location of your private key file will be /home/youruser/.ssh/id_rsa. Please note that the public key (.PUB) will also be saved in the same location.

3. Here, you will be prompted to type in the passphrase. If you don’t want to choose a passphrase, then you can leave it blank (it is recommended that you secure your key with a passphrase).

4. Finally, the SSH key pair is generated.

SSH Keys — Add SSH Key Pair to Server

It’s time to add the public key to your server; we are configuring SSH keys with the master user it can also be configured with the application user. Therefore, log in to your Cloudways Platform using your email address and password.

  1. From the top menu bar, open Servers.

  2. Then, choose the server you wish to connect using SSH keys.

    Classic Interface

    New Interface

  3. Click SSH Public Keys.

    Classic Interface

    New Interface

    Alternatively, if you are going to use your application credentials, then click SSH Keys.

    Classic Interface

    New Interface

  4. Choose the label for identification purposes.

  5. Paste your public key here.

  6. Next, click Submit.

Tip

  • Windows users can directly copy and paste the public key from the PuTTYgen or file created in an earlier step for the public key.

  • Linux/macOS users can use a text editor to view and copy the public key to the clipboard.

If you are over Linux or macOS, you can view your public key by opening your terminal window and typing the following:

cat ~/.ssh/id_rsa.pub

Then you’re able to highlight the key and copy it as required.

Classic Interface

New Interface

Once done, you can move to the next step of initiating the SSH Connection on Windows or Linux/macOS.

SSH Keys — Initiating SSH Connection on Windows

  1. Now, launch the PuTTY client and enter your Host Name (Server Public IP address).

  2. Input Port 22, which is also the standard TCP port for SSH.

3. Under Connection, select Data.

4. Enter your username, such as the master username or application username, on which you configured your SSH keys. Learn how to access master credentials or application credentials.

5. Under Connection, expand SSH and select Auth.

6. Browse your private key for authentication purposes.

7. Select Session.

8. Type session name under Saved Sessions.

9. Hit Save.

10. Finally, click Open.

11. You may see an RSA2 key prompt if this is your first time connecting to the server or may receive an alert about the new changes in keys. Click Yes to proceed.

12. You may also be prompted for a passphrase if you have previously chosen it. If not, you will be directly logged in.

Important

  • If you are using the Master Credentials, you will need to browse to your target application folder. If you are using the Application Credentials, you will land directly in the respective application folder.

  • The terminal screen will only work with arrow keys for navigation, not with a mouse.

That’s it! You have learned about connecting to your application using an SSH key pair. If you are having trouble building remote connections, please feel free to contact us via LiveChat or open a support ticket so that our support heroes can assist.

SSH Keys — Initiating SSH Connection on Linux/macOS

We won’t require any external SSH client for Linux/macOS, although you are free to do so if you already have some preferred 3rd party application. In this example, we are using Linux (Ubuntu).

  1. Launch the terminal and type the following.

ssh-add

2. Optional: If you get any type of error related to the missing key file, you can also specify your key as follows.

ssh-add ~/.ssh/id_rsa

Important

By default, ssh-add will look for a file called id_rsa; however, you can use the above command if your file name is different, or you have multiple private keys.

3. Next, execute the following command to SSH. Username refers to a master username or application username, and the hostname is your Server Public IP address. By default, the SSH connection is initiated using port 22.

ssh username@host

Example: ssh [email protected]

  1. You may see an RSA2 key prompt if this is your first time connecting to the server or may receive an alert about the new changes in keys. Type yes to make this a trusted host for future connections.

Important

  • If you are using the Master Credentials, you will need to browse to your target application folder. If you are using the Application Credentials, you will land directly in the respective application folder.

  • The terminal screen will only work with arrow keys for navigation, not with a mouse.

That’s it! We hope this tutorial was helpful. If you need any help, then feel free to search your query on Cloudways Support Center or contact us via chat (Need a Hand > Send us a Message). Alternatively, you can also create a support ticket.

Did this answer your question?