Skip to content

Instantly share code, notes, and snippets.

@ducito
Last active August 13, 2018 03:41
Show Gist options
  • Save ducito/edc52d4cb27fb592679211f257ee2cfa to your computer and use it in GitHub Desktop.
Save ducito/edc52d4cb27fb592679211f257ee2cfa to your computer and use it in GitHub Desktop.
SSH with multiple accounts

3 steps:

  • Create multi ssh key
  • Add config key
  • Change host to using the right key

Step 1: generate ssh key. Ex:

ssh-keygen -t rsa -f ~/.ssh/key1 -C “account1@gmail.com" ssh-keygen -t rsa -f ~/.ssh/key2 -C "account2@gmail.com"

Add the public key to each account.

Step 2: map keys with hosts on .ssh/config file.

Host home.projects
  User git
  Hostname gitlab.com
  PreferredAuthentications publickey
  IdentitiesOnly yes
  IdentityFile ~/.ssh/key1

Host work.projects
  User git
  Hostname bitbucket.org
  PreferredAuthentications publickey
  IdentitiesOnly yes
  IdentityFile ~/.ssh/key2

Step 3: Change the host name on the remote url:

ssh://git@hostname:<user_name>/<repo_name>.git

Ex: git clone git@gitlab.com:account1/rider.git —> git clone git@home.projects:account1/rider.git git clone git@bitbucket.org:account2/rider.git —> git clone git@work.projects:account2/rider.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment