Skip to content

Instantly share code, notes, and snippets.

@Oluwatobilobaoke
Last active March 28, 2024 04:41
Show Gist options
  • Save Oluwatobilobaoke/2e6e9e502a42d36dcc06dc292de44846 to your computer and use it in GitHub Desktop.
Save Oluwatobilobaoke/2e6e9e502a42d36dcc06dc292de44846 to your computer and use it in GitHub Desktop.
How to setup multiple ssh keys for multiple github accounts (Mac OS)
First check if you have the .ssh directory in the home repository. If not, do not worry when you create your key it will create automatically
You can list your keys in your pc with
```
ls -l ./.ssh
```
First you have to generate SSH Keys with the following command
```
ssh-keygen -t rsa -b 2048
```
Then you will receive this notification
```
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
```
In the path to save the key you have to write the following
```
/Users/your_user/.ssh/id_rsa_{{preferredName}}
```
eg:
```
/Users/your_user/.ssh/id_rsa_personal
/Users/your_user/.ssh/id_rsa_firm
```
This key is generate for Github, then you have to repeat the same steps for Gitlab
If you check the .ssh directory you will see
The pub file is the public key that you have to enter in the Github and Gitlab repository. So the next step is to copy the pub key in the repositories
Github
Go to Setting, then to SSH and GPG keys, then New SSH Key
Enter a title to the keys and then Add SSH Key
In this part Github will ask for your password, after that you have just add your public key to Github
Register your keys
Next you need to register the SSH keys locally
```
ssh-add ./.ssh/id_rsa_{{preferredName}}
```
After that you need to create a config file. Complete the file with the following code
Open the ~/.ssh/config file in any text editor and put the instructions below
```
Host personal
HostName github.com
IdentityFile ~/.ssh/id_rsa_personal
Host firm
HostName github.com
IdentityFile ~/.ssh/id_rsa_firm
```
We’ve created 2 configs for github.com with aliases personal and firm.
```
git clone [email protected]:oluwatobilobaoke/my_project.git -> git clone git@personal:oluwatobilobaoke/my_project.git
git clone [email protected]:firmgit/savings_project.git -> git clone git@firm:firmgit/savings_project.git
```
If you already cloned your project, just set the URL with the respective alias as the origin:
git remote add origin git@personal:tobioke/my_project.git — this would add to list of URLs
git remote set-url origin git@personal:tobioke/my_project.git — this would set as origin URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment