项目作者: littlemissbot

项目描述 :
Use multiple ssh keygen for different git accounts.
高级语言:
项目地址: git://github.com/littlemissbot/multi-ssh-keygen.git
创建时间: 2019-06-08T14:59:06Z
项目社区:https://github.com/littlemissbot/multi-ssh-keygen

开源协议:

下载


Multiple SSH Keygen Configuration

If you have multiple git accounts it can be hard to push and pull from repositories with different credentials.

To do it the right way, we create an SSH key and configure it to the git account. But when you have multiple accounts like one in github.com and one in bitbucket.org, you cannot use one SSH key in both the accounts. So here is the solution,

  1. Create SSH keys for each github.com account and bitbucket.org account
  2. Register the correct SSH keys with the correct github.com account and bitbucket.org account
  3. Create a git config file

Creating SSH Keys

You create a new key by running the following command, substituting for your own email address:

  1. cd ~
  2. mkdir .ssh
  3. cd ~/.ssh
  4. ssh-keygen -t rsa -b 4096 -C your_email@example.com

You’ll be prompted to “Enter file in which to save the key”. So let’s assume you need to make accounts for a personal SSH key and a work one. You can create as many SSH keys as you like.

Call the first key rsa_personal, next secure the key with a passphrase. Now repeat the steps to create a new SSH key for your work account, name the file rsa_work and secure the key with a passphrase.

Listing all SSH Keys

To view all the SSH Keys generated and the one already present in system, go to folder ~/.ssh using following command:

  1. cd ~/.ssh
  2. ls

You will get below results

  1. rsa_personal rsa_personal.pub rsa_work_1 rsa_work_1.pub rsa_work_2 rsa_work_2.pub

Registering all SSH Keys

Copy keys and register SSH keys to github.com or bitbucket.org. To copy ssh key run below command:

  1. pbcopy < ~/.ssh/rsa_personal.pub

Paste the public key into SSH configuration in git account. Repeat that for all the keys.

Create git config

Now let’s create a git configuration file in ~/.ssh folder, so when we try to access keys ssh will know which one to use. Go to folder ~/.ssh using following command:

  1. cd ~/.ssh
  2. touch config
  3. nano config

Add the following configure for each SSH Key:

  1. #github account configuration
  2. Host github.com
  3. HostName ssh.github.com
  4. Port 443
  5. User git
  6. IdentityFile ~/.ssh/rsa_personal
  7. #bitbucket account
  8. Host bitbucket.org
  9. HostName altssh.bitbucket.org
  10. Port 443
  11. User git
  12. IdentityFile ~/.ssh/rsa_work_1
  13. #azure account
  14. Host azure.com
  15. HostName ssh.dev.azure.com
  16. Port 443
  17. User git
  18. IdentityFile ~/.ssh/rsa_work_2

Test Connection

Now, this is the final step. Verify your configuration and ssh key connection to confirm everything works smoothly.

To test connection with github run the command

  1. ssh -T git@github.com

To test connection with bitbucket run the command

  1. ssh -T git@bitbucket.org

To test connection with azure run the command

  1. ssh -T git@ssh.dev.azure.com

Conclusion

If you followed along you hopefully now have everything you need to painlessly manage multiple projects hosted across different git accounts (github.com or bitbucket.org).