Using SSH with mulitple bitbucket accounts
This article was peer-reviewed by fellow Gravitywell developer Lucian Buzzo.
Here at Gravitywell we use the Git version control system combined with Bitbucket to help manage our project's code. If you're new to either Git or the idea of version control I suggest taking a look at this article. In short, Git allows you to share code and collaborate with other developers without having to worry about accidentally overwriting or deleting each others work.
Bitbucket then compliments this system by hosting your code on a remote server, allowing you to make changes and share them with your team across multiple computers.
When pushing or pulling changes from a Bitbucket code repository I like to use SSH to make the connection to their servers, due to its ease of use and security.
I also use a separate Bitbucket account for my personal code, which I connect to using SSH as well. This presents a problem when I'm trying to connect from a computer that has already been configured to access my work account.
Bitbucket does not allow you to reuse SSH keys across multiple accounts and will prevent the connection from being authenticated.
In this blog post I am going to detail the process of creating an SSH key specifically for your second account and how to use this second key to connect to a git repository hosted on Bitbucket via SSH.
I use a mac and so the following commands are applicable to OSX, though the process is similar for Linux.
To start off, if you do not have one already, you need to create an SSH config file.
An SSH config file will allow you to access a remote server using an alias/shortname instead of typing an entire SSH command into your command line, which is very convenient if you are regularly accessing remote servers.
The benefits covered in good detail by Joël Perras in his article on Nerderati.
Make sure you are in your home directory. The path will look something like this /Users/james or ~ Now you need to move into your SSH directory.
[~]$ cd .ssh
Once in the SSH directory type the following command to create the config file. We'll be adding an entry to this file later on, so just leave it there for the time being.
[~/.ssh]$ touch config
Now create an SSH key for the second account. Replace the filename with something applicable to the account.
[~/.ssh]$ ssh-keygen -f FILENAME
You'll be prompted for a passphrase, just leave it empty and hit enter. Once the key pair has been generated you should see two new files in your current directory: the one ending in `.pub` is the public key and the other is the private key. Together these two files are known as a keypair.
Copy the new public key and add it to the bitbucket account.
[~/.ssh]$ cat ~/.ssh/FILENAME.pub | pbcopy
The new key can be added to your profile in Bitbucket. Under security there is an SSH key menu option.
Click "add key" and paste the new key into the text area, naming the key appropriately.
Now that everything is set up in Bitbucket we need to add the alias entries into the SSH config file that we created earlier.
Open this file in a text editor and add the following, replacing the alias and filenames with your own.
You are now set up to use multiple bitbucket accounts from the same machine with different ssh keys for each account.
When it comes to adding the remote address for your remote repository don't forget to replace bitbucket.org with your alias in the URL for your git remote. This means that the correct ssh key will be used for the account you are trying to access.
Default address - firstname.lastname@example.org:<accountname>/<reponame>.git
Address with alias - git@alias:<accountname>/<reponame>.git
[~/code/test-project]$ git remote add origin git@ALIAS:jim_at_gravitywell/test-repo.git
If you need to modify an existing remote URL you can simply change it in your local git repository by editing .git/config.
Once this is set up it is easy to use multiple accounts from the same computer.
Although it takes a little longer to setup than using a basic https connection, SSH is more convenient, secure and should be used where possible.