Need for multiple SSH keys
Working on various projects often requires access to different systems and environments. For secure authentication, we need to maintain multiple SSH keys.
This is crucial for:
- Enhanced security: Each key is associated with a specific user or device, limiting access only to authorized individuals.
- Flexibility: Different keys work with different protocols and services.
- Efficiency: Automated scripts can easily use different keys for authentication.
- Collaboration: Team members can have different SSH keys, enabling secure access to shared resources.
Below is a 5 step guide in setting up and maintaining the SSH keys
5 Steps for configuring multiple SSH
- Create a ssh key using ssh-keygen by providing email id and the id the keys should be generated.
Replace
test@test.comwith your email id andid_rsa_githubwith the file name
ssh-keygen -t ed25519 -C test@test.com -f id_rsa_github
Once you run the above command press enter for empty passphrase. Two files would be created
- id_rsa_github –> This is your private key Never share this key
- id_rsa_github.pub –> This is your public key and can be shared.
- Add the private key i.e. not ending with
.pubtossh-add
ssh-addis a tool that is used to manage and cache private key passphrases in the SSH agent. The SSH agent is a background process that stores decrypted private keys and provides them to SSH client programs when needed. This program runs only in your computer.
ssh-add id_rsa_github
Check added private ssh key using the below command
ssh-add -l
- Create a config file in ~/.ssh named as config
- Host is how we would be communicating with github or gitlab. See point 5 for more details.
- Add the private key path to IdentityFile
# Github
Host gh.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# Gitlab
Host gl.github.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
- Add the public key to gitlab, github.
Copy the content of the file .pub to clipboard
cat id_rsa_github.pub
Github adding public key
- In the upper-right corner of any page on GitHub, click your profile photo, then click Settings.
- In the “Access” section of the sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the “Title” field, add a descriptive label for the new key
Click below links for more detailed info
- Test your new keys are working
In config file we added Host as gh.github.com to test we would use this in our command
ssh -T git@gh.github.com
ssh -T git@gl.github.com
Following info should be seen
Hi <username> You’ve successfully authenticated, but GitHub does not provide shell access.
This confirm the ssh keys are correctly setup.