Multiple Git Account Keys
This is an old post from my previous blog
My old GitHub account was a complete mess. Code snippets, configuration files, dead projects and crappy stuff that made sense only to me, I didn’t want to get rid of the whole thing but when I started this site I opted for creating a new account. The newly created account will host this site source and my recent projects.
Hence the need to manage two different GitHub accounts. In this post I will describe what you need to do in order to manage two GitHub accounts in the console.
- Basic GNU/Linux console knowledge
- Two or more GitLab accounts
- A working SSH client configuration
This guide deals only with SSH access to GitLab since the SSH protocol is the more convenient and practical way to connect with GitHub without supplying username and personal access token over and over. If you work on a Linux box SSH is very likely to be already installed and working properly.
GitHub SSH setup
GitHub allows you to authenticate with SSH keys, but it doesn’t actually create a new SSH user for each account. On GitHub everyone is authenticating with the git SSH user and the system recognises the GitHub account by the SSH key in use. Basically, if you want to access distinct account via SSH you will need distinct SSH keys.
If you don’t have already a SSH key for your new account you will need to generate it and upload it. As usual you can generate a new SSH key with ssh-keygen:
$ ssh-keygen -t rsa -b 2048
You will be prompted to enter a passphrease for the newly created key, you can leave it blank if you will:
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/sdurz/.ssh/id_rsa):
Finally, follow GitHub instructions to add your key to your new account.
GitHub SSH hosts
When you clone a GitHUB repository, like this:
$ git clone email@example.com:auser/repo.git
you are basically specifiying a SSH user and hostname in the form user@hostname to the SSH client. Under the hood the SSH client will look through its configuration files and resolve the actual hostname and connection options before establishing the connection. The hostname part is treated as a plain hostname only if SSH can’t find a corresponding Host entry in its configuration files (namely, the ~/.ssh/config file in your home directory or the global /etc/ssh/config).
Say you have the following declarations in ~/.ssh/config:
Host gh Hostname github.com User git
SSH will look for a Host entry matching the gh name and determine the correct user and hostname, user git and hostname github.com. So you could issue the former command as:
$ git co gh:/auser/repo.git
Of course there is not much of use to in a shorter alias. But with proper Host directives we can configure different aliases with different IdentityFile, different IdentityFile(s) mean different SSH keys an thus different GiHub accounts.
Multiple Host definitions for GitHub
By configuring different aliases for github.com you can select a different account just by specifying the corresponding Host alias on the command line.
So I created a specific Host definition, pointing to my old key:
# Old account dual75 Host github-old Hostname github.com User git IdentityFile /home/sdurz/.sdurz/id_rsa
Then I added a second definition:
# New account sdurz Host github Hostname github.com User git IdentityFile /home/sdurz/.ssh/id_rsa_new
Now I have two Host definitions for GitHub, they differ only in the SSH key. The old one still uses my original key, the newer uses my new key. Please note that the account name does not appear anywhere in the file.
Testing the SSH aliases
You can’t test the configuration by connecting via SSH without allocating a terminal (GitHub won’t let you do it anyway, -T will prevent the client from trying). Here I will check for both my Host definitions. Verify that the resulting message contains your username.
I can check my old account, dual75:
$ ssh -T github-old Hi dual75! You've successfully authenticated, but GitHub does not provide shell access.
And the new one, sdurz:
$ ssh -T github Hi sdurz! You've successfully authenticated, but GitHub does not provide shell access.
Everything seem to work properly.
If something shouln’t work check this page on GitHub
That’s it. From now on I will clone repositories with the account of choice just by using github-old instead of github.
Probably you will want to update the origin of your existing local repositories:
$ git remote -v origin ssh://firstname.lastname@example.org/sdurz/sdurz-me.git (fetch) origin ssh://email@example.com/sdurz/sdurz-me.git (push) $ git remote set-url origin ssh://github/sdurz/sdurz-me.git
so that SSH can pick the right key automatically.