DEV Community

l579
l579

Posted on

Generate Multiple SSH Keys for Multiple Remote Accounts

If you have multiple Bitbucket accounts (let's say one for your company and one private one) and additionally one Github account that you all want to authenticate with SSH, you can follow this guide to add all accounts to your local machine so you don't need to struggle with different accounts anymore.

Generate SSH Keys

Open your terminal and navigate to your home directory. Start by generating your first default SSH key.

ssh-keygen -t rsa
ssh-add ~/.ssh/id_rsa

Store the keys in the default location.
For each additional account generate new SSH keys but be careful not to overwrite the previously generated one by giving it a new name.

ssh-keygen -t rsa -C "companyName"
ssh-add ~/.ssh/companyName

Store this key under the companyName so when you are asked where to store it, enter the location but exchange the filename.

/Users/user/.ssh/companyName

Config

Create a config file or alter the existing one.

nano ~/.ssh/config

In YAML syntax add following entries for your keys.

Host bitbucket.org
    HostName bitbucket.org
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes

Host companyName.bitbucket.org
    HostName bitbucket.org
    IdentityFile ~/.ssh/companyName
    IdentitiesOnly yes

Save the configuration file with name config.

Configure Remote Repos

Copy your SSH key for the account you want to use it.

pbcopy < ~/.ssh/id_rsa.pub
pbcopy < ~/.ssh/companyName.pub

In Bitbucket, login to your account where you want to add the copied key and navigate to Bitbucket Settings - Security - SSH Keys and add the key. Repeat for all accounts that you created a key for.
In Github navigate to Settings - SSH and GPG keys and add your Github key there.

Cloning from Company Account

If you clone from your default account just copy the output when you click Clone inside your remote repository.
If you clone from your company account, you need to alter the host with the host you specified in your config.

git clone git@companyName.bitbucket.org:user/repo.git

Top comments (0)