Multiple SSH Keys
Published on 2023-09-26.
Did you know that you could generate many ssh keys on your machine?
But, why do I need more than one ssh key?
I need many ssh keys for my personal and non-personal GitHub accounts. Well… non-personal is anything that is not using my personal account
If you have the same needs like me, but don't know how to achieve that. Don't worry, let me guide you step by step
Before we start. Let me show what's my machine and the SSH version that I used.
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.3 LTS Release: 22.04 Codename: jammy
$ ssh -V OpenSSH_8.9p1 Ubuntu-3ubuntu0.4, OpenSSL 3.0.2 15 Mar 2022
Now, let's start the walkthrough!
Generate SSH Key
The very first step is, of course, generating the SSH key.
$ ssh-keygen -t ed25519 -C "email@example.com"
Note: substitue the email with your associated email with GitHub
Normally, I would keep pressing
Enter to continue
as the SSH key would be stored in the default file the prompt
has assigned (id_rsa).
However, since I need many SSH keys, I need to name the keys differently.
This is to distinguish which one is which and also for later purposes.
Few things here:
- I need to type the whole pathname to where I want to save it and make sure to name the keys differently.
- It is totally up to you to fill in the passphrase, but I'm choosing the empty passphrase.
$ ssh-keygen -t ed25519 -C "firstname.lastname@example.org" Generating public/private ed25519 key pair. Enter file in which to save the key (/home/swardana/.ssh/id_ed25519): /home/swardana/.ssh/id_rsa_personal Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/swardana/.ssh/id_rsa_personal Your public key has been saved in /home/swardana/.ssh/id_rsa_personal.pub The key fingerprint is: SHA256:vlvKRXyDXG4W2NcZBMUrV+W08pOs8EjNII179mkdBYs email@example.com The key's randomart image is: +--[ED25519 256]--+ | .*++| | oo . B=| | o.oE.oo*| | oo++++oo| | S.==*oo* | | . .++=.+ o| | . o. * . | | . = . | | =. | +----[SHA256]-----+
Adding the SSH to ssh-agent
ssh-agent is a program by OpenSSH that stores private keys for SSH authentication.
Just imagine that
ssh-agent is like a single sign-on (SSO) service for your system.
It allows you to authenticate an SSH connection once, then use that authentication across multiple programs.
$ eval $(ssh-agent -s) Agent pid 85101 $ ssh-add .ssh/id_rsa_personal Identity added: .ssh/id_rsa_personal (firstname.lastname@example.org)
Set the SSH key Based on the Host
This step is optional.
It's only applicable if your org uses a private Git repository.
What you could do is open and modify the content of
This is an example of what you could achieve:
Host https://github.com <- change this to whatever git remote repository you're working on. PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_personal <- change this to whatever you name your ssh-key.
The final result would be like this:
Host https://github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_personal Host https://cgit.freedesktop.org/ PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_org