Skip to content

Instantly share code, notes, and snippets.

@hadibadjian
Last active January 4, 2016 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadibadjian/8eb627b57cbdde641218 to your computer and use it in GitHub Desktop.
Save hadibadjian/8eb627b57cbdde641218 to your computer and use it in GitHub Desktop.
Managing Multiple SSH Keys

Preparation

  • Create different SSH Keys for each account. A simple SSH key can be generated using ssh-keygen -t rsa -C 'email@example.com'
  • Create a blank config file and include host details as in provided example file. Add an indentifier to differentiate hosts for similar host names.

Tree structure of ~/.ssh folder

/Users/hadi/.ssh/
├── config
├── id_rsa_example_enterprise
├── id_rsa_example_enterprise.pub
├── id_rsa_example_personal
├── id_rsa_example_personal.pub
├── id_rsa_github
├── id_rsa_github.pub
└── known_hosts

Cloning Repositories

  • Clone repositories with the host changed to the one indicated in the config file matching the correct SSH key. For instance, use git clone git@example.com.enterprise:hadi/something.git instead of git clone git@example.com:hadi/something.git
Host example.com.enterprise
HostName example.com
User git
IdentityFile ~/.ssh/id_rsa_example_enterprise
Host example.com.personal
HostName example.com
User git
IdentityFile ~/.ssh/id_rsa_example_personal
Host github.com
HostName gitbub.com
User git
IdentityFile ~/.ssh/id_rsa_github
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment