How to Set Up and Connect to a Website/Server with SSH Keys
Like many new technologies, learning to use SSH keys can be confusing. In this article, I’ll break down the parts of setting up an SSH Keys and which keys go where. At the end, you’ll understand and be confident in setting up SSH keys now and on future projects.
The primary focus of this article is to set up SSH keys for use in a Website Development situation, so I’ll mention the “Hosting Server” frequently. For other situations, the Hosting Server is whatever computer you’re connecting to.
Level of Difficulty: Medium
It isn’t particularly hard to set up SSH, but if you aren’t comfortable in the Terminal on your computer, this might be a little more difficult to use.
Finally, the directions below are done on a Mac.
What is SSH used for?
SSH provides you access to whichever computer or server you connect to. It is high level access, so you should not give SSH access to your server to anyone unless you trust them. I primarily use SSH to connect to hosting servers so I can push changes from GIT onto the site easily. If you are looking to use Git, learning how SSH works and how to set up SSH keys is vital.
What is an SSH Key?
An SSH key is made up of two text files. One is on the hosting server, and the other is in a specific folder on your computer. These talk to each other, verify each other, and then allow the SSH connection (open the door to the other computer).
Getting Started – Creating Keys
The first step to using SSH is to create the SSH Keys. There are two ways of doing this: 1) Use the Cpanel on the hosting for your website OR 2) Create them on your computer. In this article, we’re only going to cover creating them in the cpanel.
For every SSH connection, you’ll need two files: the Public Key and the Private Key. The Public Key belongs on the Server and the Private Key belongs on your computer. Let’s start with creating Keys using the Cpanel on your hosting account.
Creating SSH Keys Using the Cpanel
First, log into your cpanel. Find the SSH Access Icon. Click this icon, and then click “Manage SSH Keys”.
You’ll be presented with a screen that says MANAGE KEYS, and an option to “Generate a New Key”. Click the “Generate a New Key“.
Next you’ll need to fill out some information. I recommend changing the key name to something that you can connect to this website. In the future, if you end up with multiple SSH keys on your machine, you’ll know which one goes where.
Next, choose a key password and make sure you save this for later.
Leave the Key type and Key size alone. Click “Generate Key“.
Now you’ll be taken back to the MANAGE KEYS page, where you’ll see PUBLIC KEYS and PRIVATE KEYS.
Make sure you authorize the key by clicking the “Manage” button next to the Public Key. Click Authorize.
Finally, go back to the MANAGE KEYS page and download the private key. You can also just view it and copy all the text inside the box.
Adding the Private Key to Your Computer
When you create the SSH keys on your hosting server, both keys are there, but none are on your computer. We’ll need to move the Private Key to your computer. To do this, first find your SSH keys.
On a Mac, open up the file system (Finder), and navigate to the following: MacintoshHD (or primary Hard Drive) > Users > Your User Name > .ssh
The .ssh folder is hidden. To see hidden files on a mac, you can use the keyboard shortcut: Shift+Command+Period
Create a text file and paste the private key ( the one you copied above) into the file. Name it the same as the private key is named on the server. If you downloaded the private key instead, you can just move the file into this folder. Make sure there is no extension on the end. Your private key should look something like this: “id_rsa_nameyouchose”.
Adding the SSH key
Now that you have the private key on your computer, and the public key on the server, you’ll need to make sure your computer can see the SSH Private key on your computer. To do so, we’ll use the command “ssh-add“. This command adds it into the authentication agent on your computer.
The command below will add the SSH key so that your connection will work. Make sure you change the key_name_here to whatever your key name is.
You can run this command anywhere in the terminal. Simply open the terminal and type the above code.
Connecting using SSH
Finally, we’re ready to connect to your server using the SSH protocol. To do so, you’ll need the cpanel username and password in addition to the SSH setup we did above. Now it’s simple to connect by pasting this line into the terminal:
When asked, use the password for the cpanel.
There are a few variations, though. You can use an IP address instead of the domain name.
You might also need to use a port, by adding -p 1234 to the end of the SSH connection:
SSH cpanelUsername@domainName.com -p 21
Summary: Simple to Connect Using SSH
Overall, the process for SSH access is fairly simple once you have the process down.
- Create an SSH key in the Cpanel
- Download the private key and add it under “MacintoshHD > Users > Your User Name > .ssh”
- Add the SSH key: ssh-add ~/.ssh/key_name_here
- Connect using the cpanel username & password: SSH cpanelUsername@domainName.co