January 7, 2019

How to ssh into cPanel

Notes for myself as I need to set up such connection quite often. Would be helpful to remind myself how to create a shortcut to ssh into cPanel (or any remote machine).

Say, if I want to ssh from A to B.

To unlock the door at B, I must create a key from the lock of B, then handover this key to the hand of A.

Step-1 : Create a Key
Open the terminal at B, the destination machine, here srv60x is the machine name of my location B, and playabee is my username.

I want to create a key just under the .ssh directory.

# navigate to the .ssh directory
[playabee@srv60x ~]$ cd /home/playabee/.ssh

Then I can start creating a key with ssh-keygen.

# create a key pair with ssh-keygen
$ ssh-keygen
# when you are prompted with all those questions, you can leave everything as default, and leave the password as blank.

Till here you should obtain the following public and private key pairs in your .ssh/ directory :

$ ls
id_rsa id_rsa.pub

Step-2 : copy the private key to A
When you happy to hand over the keys, open the another terminal at your departure machine (A)

$ cd ~/.ssh/

Create an empty key with an unique name, it is better to associate the name of the key with the destination machine in my case, I would just name it srv60x.key :

$ touch srv60x.key

Copy the text of the private key we created earlier : id_rsa into this empty srv60x.key, you may easily do this manually, or scp if you do not trust copy and paste :

$ scp your_username@<replace with your hostname>:~/.ssh/id_rsa ./srv60x.key

You may need to punch in the password for your_username to scp said contents.

# to obtain the <replace with your hostname>
go back to the terminal you opened at machine B and :

$ hostname

should gives you the hostname.

Step-3 : change the permission of your key
You will only need to do this if you manually copy and paste the key instead of scp it from B to A, otherwise it is fine to skip it.

If you are not sure, check the permission level of the srv60x.key you just modified in last step :

$ ls -alh

should be fine if the permission level is -rw——- Otherwise :

$ chmod 600 srv60x.key

Step-4 : finally, teach ssh when/how to use this key
ssh needs to know where the key is and when to use it
write the following rules in ~/.ssh/config file

Host cpanel
  HostName YOUR_HOST_NAME
  Port YOUR_PORT_NUMBER
  User playabee
  IdentityFile ~/.ssh/cpanel.key

Step-5 : testing!

By now you should be able to get into your cPanel in a terminal without punching any password.

$ ssh cpanel

Leave a Reply

Your email address will not be published.

About Jiayi Liu

Software Engineer, GA analyst and member of the 24 hour Oncall team @ SLI Systems. Fan of Beethoven and Tintin. I collect mountains of video games but can't seem to finish any of them :(

Category

Notes

Tags

,