Using Public Keys with Multiple Github Accounts
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\nteh rundown
\n\nThe 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
teh walkdown
\n\nIf 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\nfyi: 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\nGenerate Teh Keyz
\n\nSuppose 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\nSo 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\nOpen a terminal, navigate to the ~/.ssh directory, and enter the following at the command prompt:
\n", nil, "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.
\n\nI happen to concur.
\n\nEnter a passphrase, and hit enter, and you'll see something like this:
\n", nil, "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+-----------------+
ssh-add the identity file
\n\nUnix 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, "ssh-add ~/.ssh/popsklstyx_githubIt'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\nHost Definition
\n\nKewlbots.
\n\nNext you should add new host definition in your ~/.ssh/config file. Don't have a .ssh/config file? Create one.
\n\nIt should look like this:
\n", nil, "Host popsklstyx_github\nHostName github.com\nUser git\nPreferredAuthentications publickey\nIdentityFile ~/.ssh/popsklstyx_dsa
Easy enough.
\n\nAdd the public key to github
\n\nYou'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\nCopy 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\nYou can test whether your public key connection is working now by going to a terminal and typing:
\n", nil, "ssh popsklstyx_github
If it's working, then you should see:
\n", nil, "ERROR: Hi popsklstyx! You've successfully authenticated, but GitHub does not provide shell access\nConnection to github.com closed.
Pretty self-explanatory.
\n\nIf 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\nAdd your remote
\n\nSo 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\nFirst navigate to the root of your local git repository. Then add your remote like this:
\n", nil, "git remote add origin popsklstyx_github:popsklstylx/repo_name_here.gitAnd 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\nA quick note on user configs
\n\nBecause 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\nBecause I'm a moron
\n\nAfter 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\nAs always let me know if you have any questions or think I've bungled something too spectacularly to leave alone.
\n"]