The video above shows you how to SSH into Host Gator. The next video shows you how to SSH into Host Gator without a password.
This is a very descriptive tutorial on SSHing into a server. By the end, you will be able to SSH into a server without using a password. The hosting provider will be Host Gator, and my operating system will be Macintosh, but the steps should be similar for any other hosting provider or OS.
For this tutorial, you will need
- the password for cPanel/your server
- the username for cPanel/your server
- the ability to use a terminal
- a server on Host Gator or any other hosting provider
SSH into Host Gator/Any Server with Password
Okay, go ahead an whip open your terminal. The first thing you’ll be doing is connecting to your server. To connect to a server on Host Gator, run
ssh -l username domain-name.com -p 2222. Username is the name that you use to sign into cPanel. Domain-name.com is the name of your website. Go ahead and visit your-domain.com/cPanel and login to make sure you have the right credentials. You can also use the IP address of your website instead of the domain name itself. 2222 is Host Gator’s default port for SSH. A shortened version of this command is
ssh firstname.lastname@example.org -p 2222.
After running that command, you will have to enter the password that you use to log in to cPanel.
If all is successful, you should see email@example.com in your terminal. If it not successful, continue on.
Quick note: if you are not using Host Gator, you may have to enable SSH on your server. Host Gator enables it automatically.
Create Key Pair and Copy Public Key to Host Gator
Now, you will SSH without having to use a password by creating a pair of SSH keys. No worries! It’s a straight forward process.
mkdir ~/.ssh in your terminal. This will create a folder called .ssh in your home folder. This is where you will keep your SSH key pairs that you are about to create.
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa -C "Comment about your key goes here." to create the key pair. When it prompts you to enter a password, leave it blank and leave all of the default settings. After that, you should have a file called id_rsa and id_rsa.pub in the .ssh folder that you just created in your home folder.
You have two options to copy id_rsa.pub to your server. You can run the command
cat ~/.ssh/id_rsa.pub | ssh firstname.lastname@example.org 'cat - >> ~/.ssh/authorized_keys' and hope it succeeds, or you can login to cPanel, use the File Manager to open a file called authorized_keys and paste in the contents of id_rsa.pub on its own line. I prefer the latter because you get to see what is happening.
First, copy the contents of id_rsa.pub from the .ssh folder that you just created. Open the .ssh folder by running
open -a Finder ~/.ssh. You can open the id_rsa.pub file with any text editor you like. Just make sure to copy its contents.
Next, login to cPanel. Open up File Manager and navigate to your home folder.
The .ssh folder will be invisible, and you will need to type .ssh and click Go.
Open up the file called authorized_keys. If there is some text in there already, that means that you already have a key in there. When pasting the contents of id_rsa.pub that you just copied, make sure it goes on its own line. Don’t put it on the same line as the other key. Go ahead and paste the contents of id_rsa.pub that you copied into the file. Save it and exit.
Open up your terminal and try running
ssh email@example.com -p 2222, again. If all was successful, you should log in to your server without having to enter a password.
Copy Public Key to DigitalOcean (Using ServerPilot)
On DigitalOcean, there is no cPanel installed by default. First of all, copy the contents of id_rsa.pub which you just created in the previous steps. Open up your terminal and run
ssh -l root yourdomain.com -p 22 and enter your password to enter your DigitalOcean droplet as the root user. Be careful. Root has the power to do anything to your server. Follow the instructions, and you should be good. Next, run
cd ~/ to enter the home folder of your root installation. Run
cd .ssh to enter the folder where you will keep your keys. If one is not created, run
mkdir .ssh and then
cd .ssh. If you’re using Serverpilot, run
cd /srv/users/serverpilot to get to the location of your .ssh folder. Inside of the .ssh folder, run vi
authorized_keys. Running this command will create the file if it doesn’t already exist. Type
i to begin inserting content into the file and copy and paste (control+v or command+v) the contents of id_rsa.pub into the file. Press esc to exit insert mode and type
:wq and hit enter to save and exit vi. Type
exit to exit your server. Now, you should be able run
ssh -l root yourdomain.com -p 22 without having to enter a password. Congratulations!
You may be wondering what all that did. First, you create an SSH key pair with the terminal on your computer, and two files called id_rsa and id_rsa.pub were created. You can think of the id_rsa file as a physical key and the id_rsa.pub file as a physical lock. By copying the contents of id_rsa.pub (the lock) to the file called authorized_keys on your server, you told the server that if a computer tries to connect to the server and it has the key (the id_rsa file in the folder called .ssh in the home folder), give the computer access to the server. If you had two servers, you could make as many copies of the locks (by copying its contents to authorized_keys) as you want, and the computer with the key would have access to all server with the locks.
SSH Shortcut Bonus
As a bonus, you can make the process of getting into your server even shorter.
Open up your terminal, exit your server:
exit, and navigate to the .ssh folder:
cd ~/.ssh. Create a new file in that folder called config:
touch config. Open up the file in your text editor and add the following contents.
Replace shortname with any name you’d like and replace username with the username that you use to login to cPanel. Of course, also replace your-domain.com with your website.
Open up your terminal and run
ssh shortname and replace shortname with the name that you entered in the config file. If all goes well, you should be able to connect to your server with that simple command.
You, now, know how to use SSH keys to SSH into Host Gator or any server without a password. Congratulations!