GitHub: Authenticated Access via SSH

This article describes how to set up authenticated access to a (private or public) GitHub repository from Linux.

Create & Set Up an SSH Key

SSH Key Creation

Create a new SSH key:

# 1. Adjust the email
# 2. Set a strong passphrase when asked
ssh-keygen -t ed25519 -C "you@example.com"

Rename the new key to whatever descriptive name you favor (I’m simply using github):

mv ~/.ssh/id_ed25519 ~/.ssh/github
mv ~/.ssh/id_ed25519.pub ~/.ssh/github.pub

SSH Key Setup: Local

Specify that the new key is to be used for github.com by adding the following to ~/.ssh/config:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github
  IdentitiesOnly yes

SSH Key Setup: GitHub

Add the public key to your GitHub account at Settings > SSH and GPG keys > New SSH key.

Enable Automatic Authentication

Start an SSH agent for your terminal session and add the key to be used. Supply the key’s passphrase when asked:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github

With the above in place, the git command won’t ask you for the passphrase to you private SSH key anymore - in the current terminal session.

Test Authentication

ssh -T git@github.com -v

More verbose output:

ssh -vvT git@github.com -v

Repo: Check Out via SSH (not HTTPS)

If the repository was checked out via HTTPS, switch to SSH:

git remote set-url origin "$(git remote get-url origin | sed -E 's#https://github.com/(.+)#git@github.com:\1#')"

Comments

Related Posts

Azure DevOps: Restricting Credentials to a Single Repository

Azure DevOps: Restricting Credentials to a Single Repository
You may find yourself in a situation where you need to limit a set of credentials to a single Git repository only - like I did when I was working on a Git-based configuration backup solution for Linux. In such a case, you want the Git credentials you are storing per machine to grant access to that machine’s repository only. As useful as such a setup is from a security point of view, it is currently difficult to implement in Azure DevOps.
Software development

Latest Posts