DEV Community


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.



Create a config file or alter the existing one.

nano ~/.ssh/config

In YAML syntax add following entries for your keys.

    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes

    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/
pbcopy < ~/.ssh/

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

Top comments (0)