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 the user will be connecting to. 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 the Windows 10 Operating Systems. When Windows 10 first launched, you had to use an SSH client to connect, however, back in 2018, a native SSH client was integrated for windows 10, allowing users to connect using a local program. To generate your SSH keys, please follow the steppes beneath:
- Please open the Run window by pressing the Windows key + r.
- When the Run window appears, please type in "cmd" and then click OK to open the command-line window.
- When the command-line interface appears, please type in the command:
- You are going to receive a prompt, asking you for the name of the key you are creating. Please type it in.
- 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. If you set a passphrase, each time you connect using these keys is going to require you to type it in.
- When you create the key, please type in the following command in the command-line:
- The above command is going to show you a huge string representing the public key. Please select and copy it.
- 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 your HostArmada username:
ssh -p 22 [email protected]
- 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
- This 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" by automatically opening it for editing.
- All you need to do when this file opens is to paste the key you copied earlier inside the file 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 add 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, followed by pressing the ENTER key, then the UP ARROW button. Afterward, please press the Shift + insert button combination to paste the key inside. Finally, press the colon button (:) on your keyboard, followed by the sequence of the "wq!" buttons and hit ENTER.
- 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
- 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:
- Inside the command-line interface, type in the line shown underneath, replacing the user with your cPanel username and hostname with the hostname of the server hosting your account with us:
ssh -p 22 [email protected]
- Upon typing this, you should immediately connect to your account over SSH and see the following prompt:
[[email protected] ~]$
- You may start executing commands and managing your hosting account through the terminal!
- To close the connection, please type "exit" and press the ENTER button on your keyboard or press Ctrl + a + d in quick succession.