One of the most basic adages within web development (and computer usage in general) is D.R.Y., which stands for Don’t Repeat Yourself. If there’s a way to make a series of manually taken steps into an automatic process by usage of software, it should be done. One of the first ways I apply this when working on a client site is that I want to speed up the process of logging into a site via SSH so that I can transverse the server files and run commands like keeping the git repositories updated.

Instead of trying to remember (or look up) the specific “ssh [email protected]” and password to ssh into an account, I’d rather just type into the terminal “ssh clientname” and have it connect me. We can accomplish that by setting SSH keys.

Many of my client websites are hosted with web hosting companies (like Siteground or Bluehost) that provide a cPanel.

Find the icon for “SSH/Shell Access”.

You’ll first want to enable shell access for the account, since most of the time it’s turned off by default. Then click “import key.”

Under the public key, enter the content of id_rsa.pub. This is the one ssh checks for most frequently. you don’t need to enter the private key as well. Save it. Then click on the “manage” button next to the list of it in the table. Click “authorize”.

Open up your shell access application. On a Mac, that’s Terminal. Go to terminal and type in ssh username@nameofserver. It’ll likely tell you that the authenticity of the host can’t be established and ask you to continue connecting. You should say yes. This should then get you in.

As an even faster shortcut, create an SSH shortcut through a config file in your ssh directory.

go to the .ssh/config file in your user account on your computer. Edit it to create a shortcut, replacing the content in bold below:

host shortcut
User username
HostName server
Port 18765(or whatever it should be for your sever, 18765 is what siteground uses)

If I’ve been developing the site locally and want to put the site-under-development for the client to look at, I’ll create a git repository locally, set the origin with BitBucket, then want to clone that to the production server. This will likely require another SSH key setting, this time, between the production server and BitBucket.

For setting this up on BitBucket, I’ll actually want to add the SSH key to my user account’s SSH key list, which is at https://bitbucket.org/account/user/USERNAME/ssh-keys/ rather than with the specific repository.

Set up SSH on macOS/Linux

Create your SSH keypair on the hosting server. I prefer to do this via the command line, since trying to do so via the GUI on cPanel often requires you to enter a Key Password, which we’ll want to avoid.

Use the command ssh-keygen. It’ll ask you to set a name for the keypai. Depending on the server, you’ll possibly need to define the specific file path as well. If you haven’t already uploaded a id_rsa for your local machine, then just press enter and leave it. If you’ve already uploaded that, you’ll want to use id_dsa as this is one of the standard names that BitBucket looks for.

It’s possible that you’ll need to save the file specifying a specific path:

$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/emmap1/.ssh/id_rsa): /Users/emmap1/.ssh/my-new-ssh-key

When it asks to set a passphrase, just hit the enter key twice so it doesn’t set one. It should now give you a confirmation message such as 

Your identification has been saved in /home2/mightyq1/.ssh/id_rsa.
Your public key has been saved in /home2/mightyq1/.ssh/id_rsa.pub.

Now use the command cat ~/.ssh/id_rsa.pub and copy the contents that the terminal writes to the window. Paste this into the “add key” section on bluehost. Confirm that the connection works by trying to clone your repo to the server now. It should work.

Step 4. Add the public key to your Bitbucket settings

  1. From Bitbucket, choose Bitbucket settings from your avatar in the lower left.
    The Account settings page opens.
  2. Click SSH keys.
    If you’ve already added keys, you’ll see them on this page.
  3. In your terminal window, copy the contents of your public key file. If you renamed the key, replace id_rsa.pub with the public key file name.

    On Linux, you can cat the contents:

    $ cat ~/.ssh/id_rsa.pub

    On macOS, the following command copies the output to the clipboard:

    $ pbcopy < ~/.ssh/id_rsa.pub
  4. Select and copy the key output in the clipboard.
    If you have problems with copy and paste, you can open the file directly with Notepad. Select the contents of the file (just avoid selecting the end-of-file characters).
  5. From Bitbucket, click Add key.
  6. Enter a Label for your new key, for example, Default public key.
  7. Paste the copied public key into the SSH Key field.
    You may see an email address on the last line when you paste. It doesn’t matter whether or not you include the email address in the Key.
  8. Click Save.
    Bitbucket sends you an email to confirm the addition of the key.

    Edit an SSH key

    After you add a key, you can edit the key’s Label but not the key itself. To change the key’s contents, you need to delete and re-add the key.

  9. Return to the terminal window and verify your configuration and username by entering the following command:

    The command message tells you which of your Bitbucket accounts can log in with that key.

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.