Forked from oanhnn/using-multiple-github-accounts-with-ssh-keys.md
Created
April 21, 2024 16:23
-
-
Save RaymondSHANG/0ff65d5f102a1e62761d76a83e69d9ea to your computer and use it in GitHub Desktop.
Revisions
-
oanhnn revised this gist
Aug 30, 2023 . 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 @@ -100,7 +100,7 @@ The bash script that prompts for your git account. Thank @davorpa #!/bin/bash # silent prompt read -p 'GIT profile: ' profile # switch case $profile in -
oanhnn revised this gist
Aug 30, 2023 . 1 changed file with 65 additions and 11 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 @@ -8,6 +8,7 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou ## 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 @@ -24,6 +25,8 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou IdentitiesOnly yes ``` > NOTE: If you use any account frequently, you should use the default hostname (`github.com`). 3. [Add ssh private keys to your agent](https://help.github.com/articles/adding-a-new-ssh-key-to-the-ssh-agent/): ```shell @@ -34,18 +37,11 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou 4. Test your connection ```shell $ ssh-keyscan github.com >> ~/.ssh/known_hosts $ ssh -T [email protected] $ ssh -T git@github-superman ``` If everything is OK, you will see these messages: ```shell @@ -56,13 +52,71 @@ Use ssh keys and define host aliases in ssh config file (each alias for an accou Hi superman! You've successfully authenticated, but GitHub does not provide shell access. ``` 5. Now all are set, you need remeber ``` git@github-superman:org/project.git => user is superman [email protected]:org/project.git. => user is oanhnn ``` - If you need clone a repository, just do: ```shell $ git clone git@github-superman:org1/project1.git /path/to/project1 $ cd /path/to/project1 $ git config user.email "[email protected]" $ git config user.name "Super Man" ``` - If you already have the repo set up, after the ssh config instructions, you need change the URL of `origin`, just do: ``` $ cd /path/to/project2 $ git remote set-url origin git@github-superman:org2/project2.git $ git config user.email "[email protected]" $ git config user.name "Super Man" ``` - If you are creating a new repository on local: ``` $ cd /path/to/project3 $ git init $ git remote add origin git@github-superman:org3/project3.git $ git config user.email "[email protected]" $ git config user.name "Super Man" $ git add . $ git commit -m "Initial commit" $ git push -u origin master ``` Done! Goodluck! ## Addon: The bash script that prompts for your git account. Thank @davorpa ``` #!/bin/bash # silent prompt input -sp 'GIT profile: ' profile # switch case $profile in superman) git config user.email "[email protected]" git config user.name "superman" git config user.signingKey "superman_gpg_public_key" ;; oanhnn) git config user.email "[email protected]" git config user.name "oanhnn" git config user.signingKey "oanhnn_gpg_public_key" ;; # default case: raise error *) >&2 echo "ERR: Unknown profile: $profile" exit 1 esac ``` -
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 [email protected] $ 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 "[email protected]" $ 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 "[email protected]" $ 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 [email protected] $ 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 [email protected] $ 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 "[email protected]" $ 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 [email protected] $ 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 "[email protected]" $ git config user.name "Supper Men" ``` Done! Have goodluck!