How to authenticate with GitHub using SSH
A step-by-step tutorial.
Step 1: Setting up local repository
Clone your repo in which you want to work, prefer to clone via SSH.
$ git clone git@github.com:OWNER/REPOSITORY.git
If you already have local repo, check it's remote origin.
$ git remote -v
> origin git@github.com:OWNER/REPOSITORY.git (fetch)
> origin git@github.com:OWNER/REPOSITORY.git (push)
If the output is something like above, then it means remote output is set correctly to ssh.
But if it's output is something like below, then it means repo's remote is set to HTTPS.
$ git remote -v
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)
Change your remote's URL from HTTPS to SSH with the git remote set-url command.
$ git remote set-url origin git@github.com:OWNER/REPOSITORY.git
Step 2: Setting up git
Check if you have configured you git name and email
$ git config --global user.name
$ git config --global user.email
If these commands returned your name or email then that means you've already configured your name and email.
If they returned nothing or you wish to update the name or email, then do so by running following commands.
$ git config --global user.name "YOUR_NAME"
$ git config --global user.email "YOUR_EMAIL"
Step 3: Setting up SSH
Caution: Be carefull with ssh-keys, always remember:
- The .pub file is the public key.
- The file without .pub is the private key.
- Only the public key should be uploaded to GitHub.
- Never share your private key.
If you have an existing SSH key, you can use the key to authenticate Git operations over SSH.
i. Checking for existing SSH keys
Before you generate a new SSH key, you should check your local machine for existing keys, by running.
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
By default, the filenames of supported public keys for GitHub are one of the following.
- id_rsa.pub
- id_ecdsa.pub
- id_ed25519.pub
Note: If you receive an error that ~/.ssh doesn't exist, you do not have an existing SSH key pair in the default location. You can create a new SSH key pair in the next step.
Either generate a new SSH key or upload an existing key.
- If you don't have a supported public and private key pair, or don't wish to use any that are available, 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 the key to the ssh-agent.
ii. Generating a new SSH key
Open Terminal and run the code below, replacing the email used in the example with your GitHub email address.
$ ssh-keygen -t ed25519 -C "your_email@example.com"
Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, use:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This creates a new SSH key, using the provided email as a label.
When you're prompted to "Enter a file in which to save the key", you can press Enter to accept the default file location. Please note that if you created SSH keys previously, ssh-keygen may ask you to rewrite another key, in which case we recommend creating a custom-named SSH key. To do so, type the default file location and replace id_ALGORITHM with your custom key name.
> Generating public/private ALGORITHM key pair.
> Enter a file in which to save the key (/home/YOU/.ssh/id_ALGORITHM):[Press enter]
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
iii. Adding your SSH key to the ssh-agent
- Start the ssh-agent in the background.
- Add your SSH private key to the ssh-agent.
$ eval "$(ssh-agent -s)"
> Agent pid 59566
If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519
iv. Adding a new SSH key to your Github account
This is Web browser method
You can add an SSH key and use it for authentication, or commit signing, or both. If you want to use the same SSH key for both authentication and signing, you need to upload it twice.
After adding a new SSH authentication key to your account on GitHub.com, you can reconfigure any local repositories to use SSH.
Copy the SSH public key to your clipboard.
If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any newlines or whitespace.
$ cat ~/.ssh/id_ed25519.pub
# Then select and copy the contents of the id_ed25519.pub file
# displayed in the terminal to your clipboard
Now the browser part:
- Open github.com in your web browser
- In the upper-right corner of any page on GitHub, click your profile picture, then click Settings.
- In the "Access" section of the sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the "Title" field, add a descriptive label for the new key. For example, if you're using a personal laptop, you might call this key "Personal laptop".
- Select the type of key, either authentication or signing.
- In the "Key" field, paste your public key.
- Click Add SSH key.
- If prompted, confirm access to your account on GitHub.
v. Testing your SSH connection
1. Open Terminal, enter the following:
$ ssh -T git@github.com
# Attempts to ssh to GitHub
You may see a warning like this:
> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
> ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
> Are you sure you want to continue connecting (yes/no)?
2. Verify that the fingerprint in the message you see matches GitHub's public key fingerprint. If it does, then type yes:
> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.
3. Verify that the resulting message contains your username.