In our last article, we demonstrated how we could mirror our repository from our local computer to GitHub and use https to push our repository data from our local computer to GitHub. In this article, we will demonstrate how we may use the ssh protocol to authenticate with GitHub and push changes to it.
First, we will need to generate our ssh keys on our local computer. We will be using these keys to authenticate with GitHub.
[sahil@git-ansible ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/sahil/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/sahil/.ssh/id_rsa. Your public key has been saved in /home/sahil/.ssh/id_rsa.pub. The key fingerprint is: b3:b4:0c:ad:44:a2:c4:7b:46:52:1e:5d:c9:14:7c:d0 sahil@git-ansible The key's randomart image is: +--[ RSA 2048]----+ | o. === | | . o .. + E | | + + . . | | . = o . | | o o o S | | o . = + | | . + | | | | | +-----------------+ [sahil@git-ansible ~]$ ls -l /home/sahil/.ssh total 24 -rw-r--r--. 1 sahil sahil 658 Dec 28 04:03 authorized_keys -rw-------. 1 sahil sahil 668 Dec 17 20:26 id_dsa -rw-r--r--. 1 sahil sahil 601 Dec 17 20:26 id_dsa.pub -rw------- 1 sahil sahil 1679 Jun 12 09:44 id_rsa -rw-r--r-- 1 sahil sahil 399 Jun 12 09:44 id_rsa.pub -rw-r--r--. 1 sahil sahil 741 Feb 19 17:28 known_hosts
Now that our ssh keys have been generated we will need to copy our public key id_rsa.pub to GitHub. Log in to your account on GitHub and go to settings. From there select ‘SSH and GPG keys’. Now click on New ssh key. Here paste the content of your public key. Once that is done You should see the below page. Now we’ll set GitHub as our remote, named as the origin and will push code from our local repository to GitHub. Given below is the command we need to type to accomplish this task.
[sahil@git-ansible perl_scripts_for_training]$ git remote add origin firstname.lastname@example.org:sahilsuri007/perl_scripts_for_training.git [sahil@git-ansible perl_scripts_for_training]$
Notice that this time the URL I provided for the origin is different. This is because now we will be using ssh to authenticate with GitHub instead of https. The ssh based URL will be email@example.com:sahilsuri007/perl_scripts_for_training.git for our repository named perl_scripts_for_training. By running ‘git remote -v’ we may view the push and fetch URLs for our remote origin.
[sahil@git-ansible perl_scripts_for_training]$ git remote -v origin firstname.lastname@example.org:sahilsuri007/perl_scripts_for_training.git (fetch) origin email@example.com:sahilsuri007/perl_scripts_for_training.git (push)
Since we have copied our public key over to GitHub and have added the required URLs for our remote, let’s push our repository over to GitHub.
[sahil@git-ansible perl_scripts_for_training]$ git push origin master The authenticity of host 'github.com (188.8.131.52)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,184.108.40.206' (RSA) to the list of known hosts. Counting objects: 40, done. Delta compression using up to 2 threads. Compressing objects: 100% (40/40), done. Writing objects: 100% (40/40), 8.32 KiB | 0 bytes/s, done. Total 40 (delta 10), reused 0 (delta 0) remote: Resolving deltas: 100% (10/10), done.
If you refresh your GitHub page for the repository you would observe that it has been updated with the content that we just pushed to it.
In this article, we demonstrated how we could use the ssh protocol and ssh public keys to authenticate with GitHub. This provides a more flexible mechanism for the integration of our git repositories with GitHub as the process can be automated/scripted. We hope that you found this article to be useful and we look forward to your suggestions and feedback.
Latest posts by Sahil Suri (see all)
- Docker networking basics explained - September 20, 2019
- How to push an image to Docker hub - September 18, 2019
- Ansible: insert a line after a string using lineinfile module - September 17, 2019
- Common Docker image and container management commands - September 16, 2019
- Docker Container life cycle explained - September 9, 2019