How to setup Github SSH connection
Fortune Ekeruo
To setup a Github SSH connection these two things must be done
- Generate SSH public and private keys
- Add the generated public key to Github
Generate SSH public and private keys
To set up SSH we first need to generate the keys for our local computer and we do that via the command line terminal.
Open up the command line and type the following command into the terminal and press enter .
cd ~/.ssh
This navigates you to a private directory called .ssh on your computer, most of the time these keys are generated for us already and to confirm that we type ls
and press enter to see the content of the directory.
If you already have these keys generated you should see the files similar to id_rsa
and id_rsa.pub
.
However, if you don't already have the keys or the .ssh dirctory then you should create it using the following command.
mkdir ~/.ssh && cd ~/.ssh
The command above will create an .ssh
private directory and navigate into it.
Next, we’ll generate the public and the private keys using the following command
ssh-keygen -C “youremail@whatever.com”
This will prompt you to enter a name and path to save the keys but you can just press enter to use a default name (id_rsa
) and the .ssh
private directory as the path.
The next prompt will ask you to enter a passphrase which is optional and you can leave it empty pressing enter a couple of times.
Add SSH public key to Github
Now that we have the keys generated ie. the id_rsa
which is the private key that should never be shared with anyone and the id_rsa.pub
which is the public that can be shared, and this is the one we’ll be adding to our Github account.
Use the following command to copy the public key.
pbcopy < ~/.ssh/id_rsa.pub
The public key will be copied to the clipboard and we can head over to Github.
Log into your Github account and navigate to the settings page from your profile at the top right corner. See attached screenshot
On the settings click on the SSH and GPG key from the sidebar navigation. See attached screenshot
Click on the on the button new SSH key, fill out the title field eg. Personal Macbook then paste the key you copied earlier from the terminal in the key field and click the button Add SSH key, and voila! You’re done.
Going forward you’ll no longer be required to enter your password when you're pushing or pulling from a repository you have rights to provided the repository is cloned with ssh and not HTTP.
If you're NOT interested in the more geeky stuff about SSH then you should STOP READING HERE.
What SSH (Secure Shell) is
Secure shell (SSH) is a protocol just like HTTP, FTP, etc. It is basically a way for computers to communicate with one another in a more secure way. With SSH you can remotely execute commands, move and copy files around from another computer, mostly by using the terminal (shell).
The significant advantage of SSH over other protocols is it offers the use of encryption to provide a way to securely transfer data from one computer to another.
SSH is designed using these three main techniques
-
Symmetric Encryption:
This is basically a way for data sent between a client (your local computer) and a server (a remote computer) to be encrypted such that it can only be decrypted and read by either of both computers using a secret key that is shared between the client and the server. If the secret key gets into the public then all the encrypted data is compromised because it can be decrypted and read by anybody and that is the challenge with Symmetric encryption. However, the exchange of this secret key is done using a key exchange algorithm to ensure it’s security.
-
Asymmetric Encryption:
This is needed by the key exchange algorithm, and unlike the symmetric encryption, for the client and the server to initiate a connection they both have to generate a private and a public key, the private key is only specific to its computer and is never shared while the public key can be shared. It is based on the Diffie Hellman’s key exchange algorithm which basically uses bits of information from the public and private keys to encrypt and decrypt data between both computers. The private and the public key both work hand in hand, this is to say that a message that was encrypted by a client’s public key can only be decrypted by the same client’s private key which was never ever shared with anyone. More on Diffie Hellman’s key exchange algorithm here and here.
-
Hashing:
This is a form of cryptography that is used in SSH and so far we can deduce that SSH uses both symmetric and asymmetric encryption, asymmetric encryption is mostly used to share the keys and because it’s more time consuming it happens once during initialization but going forward it uses the symmetric encryption for further communication because it’s fast. So to further ensure a secure communication with the symmetric encryption hashing is used to verify the authentication of the messages between both computers. More on hashing here and here