Forked from oanhnn/using-multiple-github-accounts-with-ssh-keys.md
Created
September 1, 2021 01:38
Revisions
-
Oanh Nguyen revised this gist
Feb 23, 2016 . 1 changed file with 39 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,50 +1,68 @@ ## Problem I have two Github accounts: *oanhnn* (personal) and *superman* (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull). ## Solution Use ssh keys and define host aliases in ssh config file (each alias for an account). ## How to? 1. [Generate ssh key pairs for accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add them to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/). 2. Edit/Create ssh config file (`~/.ssh/config`): ```conf # Default github account: oanhnn Host github.com HostName github.com IdentityFile ~/.ssh/oanhnn_private_key IdentitiesOnly yes # Other github account: superman Host github-superman HostName github.com IdentityFile ~/.ssh/superman_private_key IdentitiesOnly yes ``` 3. [Add ssh private keys to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/): ```shell $ ssh-add ~/.ssh/oanhnn_private_key $ ssh-add ~/.ssh/superman_private_key ``` 4. Test your connection ```shell $ ssh -T git@github.com $ ssh -T git@github-superman ``` With each command, you may see this kind of warning, type `yes`: ```shell The authenticity of host 'github.com (192.30.252.1)' can't be established. RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx: Are you sure you want to continue connecting (yes/no)? ``` If everything is OK, you will see these messages: ```shell Hi oanhnn! You've successfully authenticated, but GitHub does not provide shell access. ``` ```shell Hi superman! You've successfully authenticated, but GitHub does not provide shell access. ``` 5. Now all are set, just clone your repositories ```shell $ git clone git@github-superman:org2/project2.git /path/to/project2 $ cd /path/to/project2 $ git config user.email "superman@org2.com" $ git config user.name "Super Man" ``` Done! Goodluck! -
Oanh Nguyen revised this gist
Feb 1, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ I have a GitHub account, it is *oanhnn*, but at office, I couldn't use my person I want use both two accounts on same computer (use without typing password each times, when git push or pull). ## Solution Using ssh keys and ssh config file to create an alias host and use multiple GitHub accounts ## How to? 1. [Make key pair for each accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add it to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/). -
Oanh Nguyen revised this gist
Feb 1, 2016 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,11 +14,13 @@ Using ssh keys and ssh configure file to alias host and using multiple GitHub ac Host github.com HostName github.com IdentityFile ~/.ssh/oanhnn_private_key IdentitiesOnly yes # Other github account: supermen Host github-supermen HostName github.com IdentityFile ~/.ssh/supermen_private_key IdentitiesOnly yes ``` 3. [Added ssh key to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/) by command: -
Oanh Nguyen revised this gist
Feb 1, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,7 +41,7 @@ Using ssh keys and ssh configure file to alias host and using multiple GitHub ac $ git clone git@github-supermen:org2/project2.git /path/to/project2 $ cd /path/to/project2 $ git config user.email "supermen@org2.com" $ git config user.name "Super Men" ``` -
Oanh Nguyen revised this gist
Feb 1, 2016 . 1 changed file with 14 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,46 +1,46 @@ ## Problem I have a GitHub account, it is *oanhnn*, but at office, I couldn't use my personal GitHub account. I have to use an other GitHub account, eg *supermen*. I want use both two accounts on same computer (use without typing password each times, when git push or pull). ## Solution Using ssh keys and ssh configure file to alias host and using multiple GitHub accounts ## How to? 1. [Make key pair for each accounts](https://help.github.com/articles/generating-a-new-ssh-key/) and [add it to GitHub accounts](https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/). 2. Make ssh configure file (`~/.ssh/config`) like: ``` # Default github account: oanhnn Host github.com HostName github.com IdentityFile ~/.ssh/oanhnn_private_key # Other github account: supermen Host github-supermen HostName github.com IdentityFile ~/.ssh/supermen_private_key ``` 3. [Added ssh key to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/) by command: ``` $ ssh-add ~/.ssh/oanhnn_private_key $ ssh-add ~/.ssh/supermen_private_key ``` 4. Check that repo recognizes keys. ``` $ ssh -T git@github.com $ ssh -T git@github-supermen ``` 5. Clone projects ``` $ git clone git@github-supermen:org2/project2.git /path/to/project2 $ cd /path/to/project2 $ git config user.email "supermen@org2.com" $ git config user.name "Supper Men" ``` -
Oanh Nguyen revised this gist
Jan 26, 2016 . 1 changed file with 8 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,6 +8,7 @@ Using ssh keys and ssh configure file to alias host and using multiple github ac ## How to? 1. Make key pair for each accounts and add it to github accounts. 2. Make ssh configure file (`~/.ssh/config`) like: ``` # Default github account: oanhnn Host github.com @@ -19,23 +20,29 @@ Using ssh keys and ssh configure file to alias host and using multiple github ac HostName github.com IdentityFile ~/.ssh/suppermen_key ``` 3. Added ssh key to your agent by command: ``` $ ssh-add ~/.ssh/oanhnn_key $ ssh-add ~/.ssh/suppermen_key ``` 4. Check that repo recognizes keys. ``` $ ssh -T git@github.com $ ssh -T git@github-suppermen ``` 5. Clone projects ``` $ git clone git@github-suppermen:org2/project2.git /path/to/project2 $ cd /path/to/project2 $ git config user.email "suppermen@org2.com" $ git config user.name "Supper Men" ``` Done! Have goodluck! -
Oanh Nguyen created this gist
Jan 26, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ ## Problem I have a github account, it is *oanhnn*, but when i join a private project in company, i have to use an other github account, eg *suppermen*. I want using two accounts without typing password each times, when git push or pull. ## Solution Using ssh keys and ssh configure file to alias host and using multiple github accounts ## How to? 1. Make key pair for each accounts and add it to github accounts. 2. Make ssh configure file (`~/.ssh/config`) like: ``` # Default github account: oanhnn Host github.com HostName github.com IdentityFile ~/.ssh/oanhnn_key # Other github account: suppermen Host github-suppermen HostName github.com IdentityFile ~/.ssh/suppermen_key ``` 3. Added ssh key to your agent by command: ``` $ ssh-add ~/.ssh/oanhnn_key $ ssh-add ~/.ssh/suppermen_key ``` 4. Check that repo recognizes keys. ``` $ ssh -T git@github.com $ ssh -T git@github-suppermen ``` 5. Clone projects ``` $ git clone git@github-suppermen:org2/project2.git /path/to/project2 $ cd /path/to/project2 $ git config user.email "suppermen@org2.com" $ git config user.name "Supper Men" ``` Done! Have goodluck!