HostArmada Web Hosting Knowledge Base

Knowledge is power! Use that power and achieve total and unconditional control over the Web Hosting Services!

How to use SSH keys to connect to your hosting account on Linux and macOS.

In our experience, using SSH keys has been a big struggle for many of the users attempting to connect via SSH. What is interesting is that this method appears to be much-preferred as opposed to the password authentication approach. It does not involve typing a password on your keyboard, making it less susceptible to a key-logger type of malware that aims to steal your password. In addition to that, the login process is completely automated. With this guide, we are going to try acquainting all our clients with the know-how on using SSH keys correctly, enabling them to feel more comfortable and secure when connecting to the server.

Brief information about SSH keys

Before we dive into the explanation about connecting using SSH keys, let's go over what they are and how they work. SSH keys consist of two parts - a private key and a public key also referred to as an SSH key-pair. Creating this pair happens simultaneously, and you can imagine them being a puzzle that matches perfectly together. The private key is a randomly generated string which the user must place on his local machine, while the public key remains on the server. You can perceive the public key as being a door lock that is opened by your private key ONLY. There is an additional layer of protection you can add for your private key that serves as two-factor authentication - a passphrase. Note that when you establish a connection between your local computer and the server, and you have created a passphrase, you have to enter it to log in.

The procedure of setting up the SSH keys varies based on your Operating System. This article will cover this process for Linux and macOS Operating Systems. Both of them are Unix bases operating systems and do not need any special programs to generate SSH keys, as they have native SSH support.

IMPORTANT: Please note that port 22 is the default SSH port on most UNIX-like operating systems. For security reasons we are utilizing another port for the SSH service. If you are looking for information on how to connect to the SSH service for your web hosting account with HostArmada, please follow this guide

Here are the steps you need to follow:

  1. Please open the terminal window and type the command:
    ssh-keygen -t rsa
  2. You are going to receive a prompt, asking you the name of the key you are going to create. Please type it in. 
  3. After that, you are going to see a question asking you about a passphrase. This step is optional so you can press enter to skip it. 
  4. When you create the key, please type in the following command in the terminal:
    cat ~/.ssh/id_rsa.pub
  5. The above command is going to show you a huge string representing the public key. Please select it and copy it.
  6.  Please log in to your HostArmada cPanel account using SSH. When the command-line interface presents itself, type in the command shown below, replacing the user with your cPanel username and hostname with the hostname of the server to which you would like to connect:
    ssh -p 22 user@hostname
  7. When you have connected to your account, please type in the command listed below:
    if [[ -d "/home/$USER/.ssh" ]]; then vim /home/$USER/.ssh/authorized_keys; else mkdir /home/$USER/.ssh && vim /home/$USER/.ssh/authorized_keys; fi
  8. The above command is going to check if the .ssh directory exists and if it does, it will open the file "authorized_keys" for editing, using the vim text editor. If the .ssh directory does not exist, it will create it and then compose the file "authorized_keys" and automatically open it for editing.
  9. All you need to do when this file opens is to paste the key you copied earlier inside it and save it. To add the key inside, please press the I button on your keyboard. If you have the key copied already, you can press the Shift + insert button combination on your keyboard to paste the key immediately inside the file. Note that this file may already contain a multitude of SSH keys. This case should not worry you at all, as you can press the "I" button on your keyboard to enable editing. Afterward, please press the ENTER key, then the UP ARROW button. After that, please press the Shift + insert button combination to put the key inside. Finally, press the colon button (:) on your keyboard, followed by the sequence of the "wq!" buttons and hit ENTER
  10. The final step is to set the correct permissions for the file and directory (if you created them via the above command). Please type in the following line in the terminal:
    chmod 700 /home/$USER/.ssh && chmod 600 /home/$USER/.ssh/authorized_keys
  11. When done, please close the connection by typing exit and pressing the ENTER button on your keyboard or by pressing Ctrl + a + d in quick succession.

Here is an all-in-one command you can use to do the entire process for you, from the file creation (if needed) to the permission fix:

if [[ -d "/home/$USER/.ssh" ]]; then echo "INSERT KEY HERE" >> /home/$USER/.ssh/authorized_keys; else mkdir /home/$USER/.ssh && touch /home/$USER/.ssh/authorized_keys && echo "INSERT KEY HERE" >> /home/$USER/.ssh/authorized_keys && chmod /home/$USER/.ssh 700 && chmod /home/$USER/.ssh/authorized_keys 600  ; fi

We recommend placing this command within a text editor (such as notepad) first and then pasting the copied string where the INSERT KEY HERE text is located. Now that you have created the SSH key-pair and deployed the keys to their proper locations, you are ready to connect to your hosting account using the described method. Here is what you need to do:

  1. Inside the command-line interface, type in the line shown underneath, replacing the user with your cPanel username and hostname with your HostArmada hostname:
    ssh -p 22 user@hostname
  2. Upon typing this, you should immediately connect to your account over SSH and see the following prompt:
  3. [user@hostname ~]$
  4. You may start executing commands and managing your hosting account through the terminal!
  5.  To close the connection, please type exit and press the ENTER button on your keyboard or by press Ctrl + a + d in quick succession.

Was this article useful and on point?

Find out more about HostArmada entire range of optimized Web Hosting Services and take action today on improving your website Loading Speed, Security, and overall Stability!