How do I setup SSH key based authentication for GitHub by using ~/.ssh/config file?

24,971

Solution 1

Here is а short manual how to setup SSH key based authentication for GitHub.

1. Install the openssh-client if it is not already installed, and of course git:

sudo apt update && sudo apt install -y openssh-client git

2. Create user's ssh directory and a sub directory where your dedicated GitHub ssh key will be stored:

mkdir -p -m 700 ~/.ssh/github
  • the option -m 700 is equivalent to chmod 700 ~/.ssh ~/.ssh/github.

3. Generate the SSH key (the output key will have octal permissions 600):

ssh-keygen -t rsa -b 4096 -C '[email protected]' -f ~/.ssh/github/id_rsa -q -N ''
  • -q - silence ssh-keygen; -N '' - empty (without) passphrase, you can assign one if you want.

4. Copy the content of the file id_rsa.pub, use the following command to output it:

cat ~/.ssh/github/id_rsa.pub

5. Go to your GitHub account and follow these steps:

  • From the drop-down menu in upper right corner select Settings.
  • Then from the menu at the left side select SSH and GPG keys.
  • Click on the New SSH Key button.
  • Type some meaningful for a Title and paste the content of ~/.ssh/github/id_rsa.pub in the field Key.
  • Then click on the Add SSH Key button.

enter image description here

6. Create the ~/.ssh/config file, if it doesn't already exist:

touch ~/.ssh/config
chmod 600 ~/.ssh/config

Edit the config file and add the following entry for the new SSH key:

Host github.com
    IdentityFile ~/.ssh/github/id_rsa

7. Test the setup. Use the following command:

ssh -T [email protected]

On the question - Are you sure you want to continue connecting (yes/no)? - answer with yes. If everything went well you should receive a greeting message like this:

Hi pa4080! You've successfully authenticated, ...

How to use the SSH key.

1. If you have already cloned repository through HTTPS, by using a command as these:

git clone https://github.com/username/repository-name.git
git clone git://github.com/username/repository-name

Go inside the repository's directory and execute the next command to allow work via SSH:

git remote set-url origin [email protected]:username/repository-name.git

2. Direct clone a repository via SSH:

git clone [email protected]:username/repository-name.git

3. In addition if you are using VSC it will work without problems with this setup. For already clonned repositories just use the Open Folder option and all VSC Git features will work.

Solution 2

this file is not available by default. You have to create it. Please be aware SSH keys and ~/.ssh/config are separate files (with different purpose).

your SSH keys are stored in ~/.ssh (use ls -al ~/.ssh to see them all) and your SSH config is stored in the ~/.ssh/config. If you don't have it feel free to use touch ~/.ssh/config to create it.

If you want to use your key with github/bitbucket/gitlab use the following:

eval `ssh-agent`
ssh-add ~/.ssh/id_rsa 

in the above case id_rsa is your private SSH key file, just change it to your real private key file name

Share:
24,971
pa4080
Author by

pa4080

I have a degree as a Mechanical Engineer and Doctor of Engineering Science as well. However, since I am self-educated at Computer Science and English language, please be tolerant at my mistakes and feel free to correct them when it is necessary. Actually my real name is Sраs Zdrаvkоv Sраsоv. In my birthplace Pacho is "short" for Spas. So the first two letters of my nickname - 'pa' - comes from there. Written in Bulgarian, the number '4' starts with the letter 'ч' that is pronounced as 'ch'. The number '0' looks like the letter 'o'. So we have 'pa40', and finally '80' is my birth year ;) This nickname originates from the time before ICQ and mIRC was modern. Create Digital Ocean account and get $100 in credit to use for 2 months just for signing up.

Updated on September 18, 2022

Comments

  • pa4080
    pa4080 over 1 year

    I am trying to set up my SSH keys for GitHub and created a new SSH key for the same. I have managed to setup the SSH key but I wish to retain these settings and save them in the configuration file ~/.ssh/config which is not available. Where can I add this key path to retain the configuration?

  • Underverse
    Underverse over 5 years
    There is a strange problem where if the ~/.ssh/config file doesn't exist then the configuration settings don't set. Best to always create it if it doesn't exist.
  • tomasantunes
    tomasantunes over 2 years
    It's not on the Profile, it's on the Settings.
  • pa4080
    pa4080 over 2 years
    Hi, @tomasantunes, you are right. I've updated the answer. Time ago the menu was different :)