DEV Community

Cover image for How to use a SSH key for GitHub on unix based systems (Linux or Mac)
Eddy Vinck
Eddy Vinck

Posted on

How to use a SSH key for GitHub on unix based systems (Linux or Mac)

Navigate to ~/.ssh

ssh-keygen -t rsa -b 4096 -C "your@email.com"
Enter fullscreen mode Exit fullscreen mode

When asked to give your key a name I recommend naming it after the service you are using it for, so for me this will be github.

Create ~/.ssh/config with the following content

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github
Enter fullscreen mode Exit fullscreen mode

Add the contents of the public key file (~/.ssh/github.pub) to your GitHub account https://github.com/settings/keys

Done! Now you don't have to specify which SSH key you want to use when using SSH to interface with GitHub.

connecting with SSH without specifying which key to use

This is how I do it.

Bonus: If you use multiple accounts you can do this in your config:

# account myfirstaccount (username)
Host github.com-myfirstaccount
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-myfirstaccount

# account mysecondaccount
Host github.com-mysecondaccount
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-mysecondaccount
Enter fullscreen mode Exit fullscreen mode

Top comments (0)