Using GitHub SSH Keys for Auth from terminal
Steps
Generate SSH keys on your machine.
Add ssh keys to your local ssh-agent.
Add your public ssh key to your GitHub account.
Test your SSH key from your machine.
Push code!
1. Generate SSH Keys
To generate ssh keys on any computer running a Linux operating system. Type in the following command onto your terminal:
ssh-keygen
On executing the command, you will be prompted to choose a name and passphrase for your SSH keys. On providing the passphrase and the SSH key name, your SSH key pair will get generated. You will then have to transfer both the public and private keys to ~/.ssh/
directory.
2. Add ssh keys to your local ssh-agent
After you create the SSH key pair and move it to ~/.ssh/
directory, you need to run the following command to register your SSH keys with the SSH agent on your machine. Before doing this, you will have to check if the SSH agent is already running on your machine. This command can be used to do the same:
After confirming that the SSH agent is running, you can add your private ssh-key to the list maintained by ssh-agent on your machine. This can be done using the following command:
ssh-add ~/.ssh/path-to-your-private-ssh-key
After that is done, a confirmation is displayed. We will now have to add the public key part of our SSH keypair to our GitHub account. To display the contents of your SSH public key, you can use the following command:
cat ~/.ssh/path-to-your-public-key.pub
Once the above command is run, the contents of your public SSH key is displayed on the terminal as shown below:
You can now copy this and proceed to the next step.
3. Add your public ssh key to your GitHub account.
Once you have copied the contents of your SSH public key, you need to log in to your GitHub account from a web browser and go to your account settings. In the account settings page click on this option:
You will be taken to a new page on clicking that menu option. On the new page, you can find a button that says "New SSH Key". Click on that button and fill in the Title and Key fields in the form that is displayed. Let the "Key Type" stay as "Authentication Key". After you paste your public SSH key into the form and give the key a title, you can hit submit.
4. Test your SSH key from your terminal.
You can test if you can access your GitHub account through ssh from your terminal using this command:
ssh -T git@github.com
After you execute this line on your terminal, if you have done all the steps correctly, you should be able to see a message like this:
Congratulations! You can now access all your GitHub repositories through SSH from your terminal. This also works with other Git providers like BitBucket, GitLab etc.
In case anything does not work, please drop a comment, I'll try to reply as soon as possible :)
Subscribe to my newsletter
Read articles from Abhinav Gorantla directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by