SSH Key Auth for Two Github Accounts

Today I Learned

Sometimes I have needed/wanted to keep authentication via SSH keys to my two Github accounts in the same machine. For such, the following arrangement in the .ssh/config file seems to work:

Github Account Number 1: alex

Host github.com-alex
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/alex-key
  IdentitiesOnly yes

Github Account Number 2: xander

Host github.com-xander
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/xander-key
  IdentitiesOnly yes

Explanation:

  • Host: It is like an alias for the host to which I will connect. For the example above, the two aliases would be: “github.com-alex” and “github.com-xander”.
  • Hostname: It is the the real name for the host to which I will connect. In my example, it is github.com since I want to keep two different accounts.
  • UseKeychain: The SSH keys will be used instead of a password.
  • AddKeysToAgent: It will add the keys to the ssh-agent after each restart of the machine.
  • IdentityFile: To specify where the SSH key resides.
  • IdentitiesOnly: It enforces the use of the IdentityFile.

Drawback:

I gotta remember to connect to the alias when interacting in the CLI. For example:

❯ git clone git@github.com-alex:alex/a_repo_example.git

Instead of just:

❯ git clone git@github.com:alex/a_repo_example.git

 Share!

 
comments powered by Disqus