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.

\n\n

teh rundown

\n\n

The really quick overview is as such:

\n\n
    \n
  • Create a new public key in ~/.ssh/
  • \n
  • ssh-add the resulting identity file
  • \n
  • Create a separate host definition in ~/.ssh/config for github.com that references the new public key
  • \n
  • Add the new public key to the SSH Public Keys section in your new github account
  • \n
  • 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
  • \n
\n\n\n

teh walkdown

\n\n

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:

\n\n

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

\n\n

Generate Teh Keyz

\n\n

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.

\n\n

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.

\n\n

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

\n", nil, "
\n
ssh-keygen -t dsa -C "popsicled00d@yourunclewalter.biz" -f popsklstyx_github
\n
\n", nil, "

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

\n\n

I happen to concur.

\n\n

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

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

ssh-add the identity file

\n\n

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:

\n", nil, "
\n
ssh-add ~/.ssh/popsklstyx_github
\n
\n", nil, "

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.

\n\n

Host Definition

\n\n

Kewlbots.

\n\n

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

\n\n

It should look like this:

\n", nil, "
\n
Host popsklstyx_github\nHostName github.com\nUser git\nPreferredAuthentications publickey\nIdentityFile ~/.ssh/popsklstyx_dsa
\n
\n", nil, "

Easy enough.

\n\n

Add the public key to github

\n\n

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.

\n\n

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.

\n\n

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

\n", nil, "
\n
ssh popsklstyx_github
\n
\n", nil, "

If it's working, then you should see:

\n", nil, "
\n
ERROR: Hi popsklstyx! You've successfully authenticated, but GitHub does not provide shell access\nConnection to github.com closed.
\n
\n", nil, "

Pretty self-explanatory.

\n\n

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.

\n\n

Add your remote

\n\n

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.

\n\n

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

\n", nil, "
\n
git remote add origin popsklstyx_github:popsklstylx/repo_name_here.git
\n
\n", nil, "

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.

\n\n

A quick note on user configs

\n\n

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.

\n\n

Because I'm a moron

\n\n

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.

\n\n

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

\n"]

no comments

login to post comments, or register to post a comment

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