When working with a GitHub repository, you will often need to identify yourself to GitHub using your username and password. An SSH key is an alternate way to identify yourself that doesn't require you to enter you username and password every time.
SSH keys come in pairs, a public key that gets shared with services like GitHub, and a private key that is stored only on your computer. If the keys match, you are granted access.
The cryptography behind SSH keys ensures that no one can reverse engineer your private key from the public one.
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
Checking for existing SSH keys
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
-
Open terminal
-
Enter ls -al ~/.ssh
to see if existing SSH keys are present. This command lists the files in your .ssh
directory, if they exist
-
Check the directory listing to see if you already have a public SSH key. By default, the filenames of supported public keys for GitHub are one of the following.
No existing SSH keys
If you don't already have an SSH key, you must generate a new SSH key to use for authentication.
First of all we need to create the directory where the key can be stored, if not already existing.
Then generate the ssh key.
If you are using a legacy system that doesn't support the Ed25519 algorithm, use this instead.
So in the above commands, your_email@example.com
should be replaced with the primary email that you had used to create your Github account.
This creates a new SSH key, using the provided email as a label.
Generating public/private algorithm key pair.
When you are prompted to Enter a file in which to save the key press Enter. This accepts the default file location.
At the prompt, type a secure passphrase. For more information, refer to Working with SSH key passphrases.
Add your SSH key to the ssh-agent
ssh-agent
is a program that stores your private keys. It starts when you log in. We will add a copy of our private key to this SSH agent.
Follow this detailed article by Github to add ssh key to ssh-agent
.
Add your public SSH key to GitHub
This is the final step. We will add the generated ssh key to Github.
Follow this detailed article by Github to add ssh key to your Github account.
Verifying
As to verify everything is working as intended, we can run the following in the terminal.
And it should ideally output something like this.