How to use multiple SSH keys on a Mac with Github or Gitlab (or any other site)
Tags: devops, ssh, howto
Published: 2020-05-31 13:41:55

**A quick note on how to use multiple SSH Keys (Identities) on one machine.**

_This article was also published on [my site](https://diegocarrasco.com/how-to-use-multiple-ssh-keys-on-a-mac-with-github-or-gitlab/?utm_source=dev_to&utm_medium=article&utm_campaign=how_to)_

## A quick note on the use case
Why would you want to use multiple keys on a machine you ask? There are several reasons, one being perceived security, with contrary opinions and convenience. Using just one Identity makes everything easier and you don’t have to manage several Identities You can read more [here](https://security.stackexchange.com/questions/40050/best-practice-separate-ssh-key-per-host-and-user-vs-one-ssh-key-for-all-hos "Best Practice: ”separate ssh-key per host and user“ vs. ”one ssh-key for all hosts“") and [here](https://superuser.com/questions/121307/is-it-reasonable-to-have-multiple-ssh-keys "Is it reasonable to have multiple SSH keys?").   
My use case would be that of convenience. I just want to separate work from personal projects and I would like to use a separete key (non-work related) with those projects

## 1. Create a new public key 

```bash
ssh-keygen -t rsa -C "your_email@youremail.com"
```

This public key can be in any path, not just in `~/.ssh/` as default/ recommended. 
For security, it is recommended that you assign a unique password to this key.

## Add all the keys to your cached keys

```bash
ssh-add path-to-key
```

For example, if you have your new key in `~/Documents/ssh_keys/new_key` you would need to run `ssh-add ~/Documents/ssh_keys/new_key `. If you assigned a password in Setup 1, you will be asked for it.

## Step 3: Update/ Create your ssh config

```bash
nano ~/.ssh/config
```

If you already have entries in your ssh config, just add a new entry at the end of the file. If your file is empty or you didn’t have any before, just add the following.

```bash
Host gitlab-diego
	HostName gitlab.com # or github.com or any other domain
	User your-username # usually the one you use with that domain
	IdentityFile path-to-key
```

With the example data for GitLab and user `diego` this entry would look like the following:

```bash
Host gitlab-diego
	HostName gitlab.com
	User diego
	IdentityFile ~/Documents/ssh_keys/new_key
```

To save and close just press `ctrl + o`  and `enter` and `ctrl + x` to exit `nano`.

### Step 4: Add the key to GitLab, GitHub or other services

In my case, I wanted to use the new key with GitLab. They have [great instructions](https://gitlab.com/help/ssh/README#adding-an-ssh-key-to-your-gitlab-account "Adding an SSH key to your GitLab account") on how to do that but, for convenience, I list the steps here:
1. Copy your new key to the clipboard. 
	```bash
	pbcopy < path-to-key
	```
2. Log in into gitlab.com and go to [https://gitlab.com/profile/keys](https://gitlab.com/profile/keys)
3. Paste the key into the text box and click on „Add key“

**_Now you may clone a repository using this new key._**

# References and further reading
- [Multiple SSH Keys settings for different github account](https://gist.github.com/jexchan/2351996 "Multiple SSH Keys settings for different github account")
- [ GitLab and SSH keys](https://gitlab.com/help/ssh/README#locating-an-existing-ssh-key-pair "GitLab and SSH keys")