Using Public Keys with Multiple Github Accounts

by Mike Zazaian at 2010-06-13 08:58:21 UTC in unix

A quick tutorial on how to post to multiple github accounts from a single unix user without conflict.

no comments 2 links

This has happened to me a couple of times, and while the ease of dispatching this matter didn't occur to me the first time it happened, it clicked today and it's pretty easy to deal with.

teh rundown

The really quick overview is as such:

  • Create a new public key in ~/.ssh/
  • ssh-add the resulting identity file
  • Create a separate host definition in ~/.ssh/config for github.com that references the new public key
  • Add the new public key to the SSH Public Keys section in your new github account
  • Use the hostname of your new host definition in lieu of github.com in your github remote url for all repos owned by your new github account

teh walkdown

If you need a bit more specificity, or if I'm just too in love with myself/the English language to say something in five sentences when I can say it in fifty (more likely), here's some elaboration:

fyi: if at any point you're having peculiar issues with certain steps not registering/functioning properly, spawn a new terminal emulator and try again. Some of these steps act differently across operating systems

Generate Teh Keyz

Suppose you just created a new github account with the username popsklstyx, and your email address is popsicled00d@yourunclewalter.biz. Yeah, you're pretty weird, but you're using github and unix so that makes up for some of it.

So the first thing you should do is generate a new public key for your account, as public keys have to be unique across github, even between different user accounts.

Open a terminal, navigate to the ~/.ssh directory, and enter the following at the command prompt:

ssh-keygen -t dsa -C "popsicled00d@yourunclewalter.biz" -f popsklstyx_github

It'll prompt you for a passphrase, and as Github and its brain trust Chris Wanstrath suggest, you should use one. A strong one.

I happen to concur.

Enter a passphrase, and hit enter, and you'll see something like this:

Your identification has been saved in popsklstyx_github.
Your public key has been saved in popsklstyx_github.pub.
The key fingerprint is:
3c:2f:55:2f:e4:d7:da:75:d2:fc:7e:db:c6:e5:9f:2c user@host
The key's randomart image is:
+--[ DSA 1024]----+
|                 |
|    +            |
|            o    |
|  .    x   + . + |
|        S . o + *|
|  D          o +=|
|        . .   .o+|
|         .   E..B|
|    B         .=B|
+-----------------+

ssh-add the identity file

Unix knows to look for files called id_dsa or id_rsa, but you'll have to manually add identity files named anything else using the ssh-add utility. In our case it'll look like this:

ssh-add ~/.ssh/popsklstyx_github

It'll then prompt you for a passphrase (assuming that you chose to use one). Just enter it, confirm it, and your new identification/publickey will be properly recognized.

Host Definition

Kewlbots.

Next you should add new host definition in your ~/.ssh/config file. Don't have a .ssh/config file? Create one.

It should look like this:

Host popsklstyx_github
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/popsklstyx_dsa

Easy enough.

Add the public key to github

You'll notice that you now have two new files in ~/.ssh, one called popsklstyx_github and one called popsklstyx_github.pub. The contents of popsklstyx_github are what we're going to give to github to allow it to authenticate us.

Copy the contents of the public key (.pub) file, go to the Account Settings > SSH Public Keys section of your github (web) account, and paste the whole contents of that file in as a new public key. Maybe give it a nice descriptive related to your local environment, too, like "Uncle Walter's Asus" or something of that ilk.

You can test whether your public key connection is working now by going to a terminal and typing:

ssh popsklstyx_github

If it's working, then you should see:

ERROR: Hi popsklstyx! You've successfully authenticated, but GitHub does not provide shell access
Connection to github.com closed.

Pretty self-explanatory.

If it doesn't work, however, you'll get a permission denied(publickey) error. If that's the case make sure that you're calling your identity file properly from your host definition, and that your publickey has been properly added (in full) to github.

Add your remote

So the last step here is that in lieu of the standard git@github.com: user/host combo for the github ssh repo path, we're going to use the host that we created.

First navigate to the root of your local git repository. Then add your remote like this:

git remote add origin popsklstyx_github:popsklstylx/repo_name_here.git

And that'll do ya. Just give it a pull or push, and as long as you don't see one of the errors that we saw a moment ago, you're golden.

A quick note on user configs

Because publickeys validate as unique unilaterally across github, you shouldn't have to worry about setting git config github.user xxxx in either a global or repo-local capacity. The public key/ident files here will take care of that for you, and will actually ignore whatever settings you've got in the respective git config files.

Because I'm a moron

After I started writing this article I realized that there's a similar tutorial on help.github.com that I could swear wasn't here when I first looked at this a month or two ago. Either way, I'm not one to abandon an article, so hopefully I've helped shed some additional light here.

As always let me know if you have any questions or think I've bungled something too spectacularly to leave alone.

no comments

Comments closed

latest links

Help.GitHub - Multiple SSH keys The article from github help mirroring this process
ones zeros majors and minors ones zeros majors and minors: esoteric adventures in solipsism, by chris wanstrath
ActiveScaffold A Ruby on Rails plugin for dynamic, AJAX CRUD interfaces

login

register activate reset

feeds

articles/rss

topics

staff

editor

about

doblock focuses on ruby, rails, and all things that can help ruby and/or rails programmers hone their skills.

Techniques, tutorials, news, and even free open-source applications, doblock seeks to fill in the cracks of the ruby/rails blogosphere.

doblock v. 0.10.1 powered by Rails