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?
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 coursegit
:2. Create user's ssh directory and a sub directory where your dedicated GitHub ssh key will be stored:
-m 700
is equivalent tochmod 700 ~/.ssh ~/.ssh/github
.3. Generate the SSH key (the output key will have octal permissions
600
):-q
- silence ssh-keygen;-N ''
- empty (without) passphrase, you can assign one if you want. If it is passphrase protected key, you can add-a 256
(default is 16) to increase the security of the passphrase by decreasing its verification.4. Copy the content of the file
id_ed25519.pub
, use the following command to output it:5. Go to your GitHub account and follow these steps:
~/.ssh/github/id_ed25519.pub
in the field Key.6. Create the
~/.ssh/config
file, if it doesn't already exist:Edit the
config
file and add the following entry for the new SSH key:7. Test the setup. Use the following command:
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:
How to use the SSH key.
1. If you have already cloned repository through HTTPS, by using a command as these:
Go inside the repository's directory and execute the next command to allow work via SSH:
2. Direct clone a repository via SSH:
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.
Thanks to @k..a..b the examples in the answers were updated from using RSA key pair to using ED25519 which is more secure. Here are few references about that:
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 usetouch ~/.ssh/config
to create it.If you want to use your key with GitHub/Bitbucket/GitLab use the following:
Start the ssh-agent in the background:
Then add the SSH private key to the ssh-agent and store your passphrase (if created) in the keychain:
In the above case
id_ed25519
is your private SSH key file. If you have a different name for your private key, likely because you used a different cryptographic algorithm (i.e.RSA
orECDSA
) just change it to your real private key file name.If key pair is generated with RSA algorithm it should appears as follows:
Else, it will appears as so if keys are generated with ECDSA: