Skip to main content

How to set up SSH keys to access remote servers

In an earlier post, I mentioned how to switch your GitHub remote URL from HTTPS to SSH, which can improve productivity. Now before you can push and pull your repositories automatically, you'll also need to set up your SSH key.

Setting SSH keys is extremely useful and can be applied to other uses outside GitHub; essentially whenever you need access to a remote server you will be required to provide a username and password, and setting an SSH key allows secure access to the desired server without having the user to provide that information every single time.

SSH stands for secure shell or secure socket shell, and is used for secure access to a remote server. When you use SSH to connect to a remote server (i.e., another Linux computer), you create a shell session on that server through the terminal on your system. This text-based interface allows you to send any command remotely from your system to that server.

To access via SSH, you'll be required to identify yourself either through a username and password, or SSH keys associated with your computer. The password method easy to understand: you provide a username and password pair for the server to authenticate. On the other hand, SSH keys are a bit more complex. In a nutshell, you create a pair of keys, private and public, of which you share (add) the public one to remote servers for them to identify you.

Compared to the password method, using SSH keys is more secure. Firstly, user generated password can be very vulnerable. Secondly, the password method uses symmetric encryption, which means the same (secret) key is used to encrypt and decrypt information, while on the other hand, the private SSH key is used for decrypting information sent (and encrypted) by the remote server using the public SSH key you provided. Furthermore, accessing multiple remote servers is much more convenient using SSH keys then using passwords: you only have to provide the public key to the remote servers instead of creating multiple usernames and passwords. Here's a great video explaining how all this works:

I referred to the BitBucket documentation for the following tutorial.


This tutorial is specifically aimed to create SSH keys for automatic access to a Git account like GitHub or BitBucket, but as explained above, you can use it to directly access the remote server. As an example, I use SSH to access robots frequently from my computer at work, without having to operate on the physical robot. Also, this tutorial is for Linux systems, but there are online resources that can do it for Mac OS too (and maybe for Windows).

  1. On your terminal, create your SSH key pair by entering
    $ ssh-keygen
    
    You will be asked for a location to store your private and public key. Press 'Enter' to accept the default location (if you don't have a reason to don't change it from the default location).

  2. You will be prompted to enter a passphrase. Leave it blank and press 'Enter' if you don't want one. Press 'Enter' again to confirm the same passphrase (or if you provided one earlier, type it and then press 'Enter').

  3. You should have your public (~/.ssh/id_rsa.pub) and private key (~/.ssh/id_rsa) pair now.

  4. Now add the private key to your SSH agent by first starting the agent in your terminal:
    $ eval `ssh-agent`
    
    And then adding the SSH key:
    $ ssh-add ~/.ssh/id_rsa
    
    Note that the path to your key will be different from the one above if you stored it in a different location or with a different name. Also, you only need to add the private key to your SSH agent.

  5. Next, add your public key to your Git account. Go to your Git account website and access its settings. In Github, when you click 'Settings' > 'SSH and GPG Keys', you will see a list of SSH and GPG keys that you have added in the past. Click 'New SSH key' and you will see the following:
    • In 'Title', put a description to the key you are adding. I usually put something to identify the which machine the key is from.

    • Then, go to your terminal and copy your public key. First output the key by doing
      $ cat ~/.ssh/id_rsa.pub
      
      This will output your key, which starts with ssh-rsa, a huge bunch of mumbo jumbo, and then ending with your machine name. Copy that and paste in the 'Key' box on the Github 'New SSH key' page.

    • Don't forget to save it!

Comments

Popular posts from this blog

Standard Deviation vs. RMSE

I came across the concept of Root Mean Square Error (RMSE) two days ago at work, while investigating 3-D localization using Google Cartographer . I was familiar with the difference between RMSE and standard deviation previously, but at work I seemed to have gotten them mixed up (again). Like various other concepts that I frequently learned to forget, I figured I should write it down somewhere in case I need a quick refresher, hence this post. To get started, let's begin with their similarities. The standard deviation, generally represented by the Greek lowercase sigma letter $\sigma$, and the RMSE are similar mathematically: they are both metrics to measure the variance of a variable from a reference . Put simply, they tell you how much a variable you are measuring/estimating 1 vary from a reference. For example, if you get a high value in $\sigma$ or RMSE, you know that the variable that you are measuring/esti

Introduction

This blog is a result of my many instances thinking "I should write down this really cool thing I'm learning/implementing somewhere", and then proceeding to write it down with pen and paper before losing that documentation for good. After struggling time and time again trying to find notes of something I did or learned, I figured writing a blog that appends to my website / online portfolio would be a good way of storing them. As the famous quote (often mistakenly attributed to A. Einstein) goes, "if you cannot explain it simply you don't understand it well enough" 1 , this blog also serves as a great way to reinforce my understanding of the subjects I'm learning by forcing me to explain the concepts as intuitively as possible. I have enjoyed explaining concepts to people, and have considered that perhaps I'd like to teach someday, which is another benefit to setting up this blog. I should be setting up posts soon (I hope!), so stay tuned. Than

How to switch a remote repo URL from HTTPS to SSH (and vice versa)

For my first post, I figured I could start with something really simple. I encountered this at work some time ago, but I forgot how to solve it after encountering it once again recently. Basically, I realized that (after not updating my website for awhile) I git cloned my repository using HTTPS instead of SSH onto my laptop. For the sake of convenience, I wanted to be able to do git pull, push and so on without having to validate my identification every time. This is awfully simple, but I figured I might as well document this in case I forget it in the future, and hopefully this helps someone in need too. I referred to the GitHub documentation for the following tutorial. When cloning repositories, you can choose whether to do it via HTTPS or SSH. A HTTPS type clone is creating a copy of a remote repo which you do not have automatic write access to , while an SSH type clone gives you the access to push your changes (as long as you have an SSH key set up) automatically . Hence,