Table of Contents:
Checking for existing SSH keys
Before you generate an SSH key, you can check to see if you have any existing SSH keys.
-
Open Git Bash.
-
Enter
ls -al ~/.ssh
to see if existing SSH keys are present:
# Lists the files in your .ssh directory, if they exist
ls -al ~/.ssh
- Check the directory listing to see if you already have a public SSH key.
By default, the filenames of the public keys are one of the following, based on the SSH key type:
- id_dsa.pub
- id_ecdsa.pub
- id_ed25519.pub
- id_rsa.pub
If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to GitHub, then generate a new SSH key. If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub, you can add your SSH key to the ssh-agent.
After you've checked that you don't have an existing SSH key or you simply want a new one, you can generate a new SSH key to use for authentication, then add it to the ssh-agent.
Generating a new SSH key
This will generate a key that is generally compatible with most system to date while using a more state of the art algorithm, ED25519.
-
Open Terminal.
-
Paste the text below, substituting in your GitHub email address.
# Creates a new ssh key, using the provided email as a label
$ ssh-keygen -t ed25519 -b 4096 -C "your_email@example.com"
> Generating public/private ed25519 key pair.
- When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.
> Enter a file in which to save the key (/Users/you/.ssh/id_ed25519): [Press enter]
- At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases".
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
Adding your SSH key to the ssh-agent
Before adding a new SSH key to the ssh-agent, you should have checked for existing SSH keys and generated a new SSH key.
- Ensure ssh-agent is enabled:
# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
> Agent pid 59566
- Add your SSH key to the ssh-agent. If you used an existing SSH key rather than generating a new SSH key, you'll need to replace id_ed25519 in the command with the name of your existing private key file.
$ ssh-add ~/.ssh/id_ed25519
If you will be using this for GitHub, continue. Otherwise, enjoy your new ssh key.
Adding your key to your GitHub Account
Visit this GitHub article.