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).
-
On your terminal, create your SSH key pair by entering
$ ssh-keygen
- 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').
- You should have your public (~/.ssh/id_rsa.pub) and private key (~/.ssh/id_rsa) pair now.
-
Now add the private key to your SSH agent by first starting the agent in
your terminal:
$ eval `ssh-agent`
$ ssh-add ~/.ssh/id_rsa
-
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
- Don't forget to save it!
Comments
Post a Comment