Using Multiple SSH Keys
Published: 2023-10-10 16:51:48

Hi everyone! This is my first post and I'm here to talk about SSH and the pain it is to use it sometimes. So let's go directly to the point. Have you ever had the feeling you can´t use GitHub and GitLab at the same time on your devices through SSH? Well, you can!

## First Things First
Enter `ls -al ~/.ssh` and if the output is like

```
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub
```
It means that you have the folder we need and also you already have the default keys in place (we're not using those).

## Creating Your Keys
### GitHub
On your terminal enter

```
ssh-keygen -f ~/.ssh/id_ed25519_github -t ed25519 -C "your_email@example.com"
```
For some old systems you'll have to use RSA, so do this instead
```
ssh-keygen -f ~/.ssh/id_rsa_github -t rsa -b 4096 -C "your_email@example.com"
```

In both ways, they're gonna ask you for names and passwords but I just press ENTER and move on.

### GitLab
Same thing. At terminal we enter
```
ssh-keygen -f ~/.ssh/id_ed25519_gitlab -t ed25519 -C "your_email@example.com"
```
For RSA
```
ssh-keygen -f ~/.ssh/id_rsa_gitlab -t ed25519 -C "your_email@example.com"
```
## Add Your Keys To ssh-agent
Be sure that it is running 
```
eval `ssh-agent -s`
```
And now enter the name of the keys you registered
GitHub
```
ssh-add ~/.ssh/id_ed25519_github
```
GitLab
```
ssh-add ~/.ssh/id_ed25519_gitlab
```
ATTENTION: If you're using the RSA one be sure to put the correct names of the files.

## Register your SSH Key
### GitHub
#### Terminal
- Enter the ssh filter with `cd ~/.ssh`
- Type `cat id_ed25519_github.pub`
- Copy the content
#### Web Site
- Click your photo profile
- Settings
- SSH and GPG Keys
- New SSH Key
- Give the registry a name on the Title
- Paste the key that you copied from the terminal on the key
- Add SSH Key

###GitLab
#### Terminal
- Enter the ssh filter with `cd ~/.ssh`
- Type `cat id_ed25519_gitlab.pub`
- Copy the content
#### Web Site
- Click your photo profile
- Settings
- SSH Keys
- Paste the key that you copied from the terminal on the key
- Give the registry a name on the Title
- Add key

## The Config File
Now is the tricky part. When you're using the keys on .ssh there is no config file so you need to create one.
- Enter the .ssh folder with `cd ~/.ssh`
- Create a file with `touch config`
- Now open the file with  `nano config`
- Paste the configuration below
```
# GITHUB
Host github.com
   HostName github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_github

# GITLAB
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_gitlab
```

### Sometimes: ERROR
GitHub likes to make us lose some hair over configuration and sometimes you'll get some errors like:
```
kex_exchange_identification: read: connection reset by peer ubuntu
```
If the error has **kex_exchange_identification** try these changes on the config file:
```
# GITHUB
Host github.com
   HostName ssh.github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_github
   Port 443
   User git

# GITLAB
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_ed25519_gitlab
```
If you set the port to 443 and put the "ssh." on HostName

Also, there is this annoying possibility you get a permission error. To fix that set your config file to 600:
```
cd ~/.ssh
chmod 600 config
```

## 123 Testing 
Now you try `ssh -T git@github.com` and `ssh -T git@gitlab.com` and see if the message is a friendly one. 

The best part is that you can add more versioning sites to use your keys and help with productivity (and also you'll be way less annoyed :) )

PS: If you're still getting errors like the "bad owner or permission" one, try `chmod 600 ~/.ssh/config`.