Use multiple SSH keys for different GitHub accounts
Written byPhuoc Nguyen
Category
Tip
Tags
Git
Created
19 Apr, 2021
Rather than entering the username and password, you often use SSH keys to access GitHub repositories. It's a more secured and recommended way to communicate with remote GitHub servers.
Sometimes you have more than one GitHub account. For example, one for accessing personal repositories, another one for your daily works.
The question is how your local Git recogranizes a repository that comes with which GitHub account. This tip will help you.
#Creating different keys
Assume that
`foo`
and `bar`
are two GitHub usernames that you would like to use in the same computer. You can follow the official GitHub guide to generate SSH key.// Generate SSH key for `foo`
$ ssh-keygen -t ed25519 -C "foo@domain.com"
When you're asked to indicate the file to save the key, don't use the default key. Change the name of file to something associate with the account, for example:
Enter a file in which to save the key (/home/you/.ssh/id_ed25519):
`/home/you/.ssh/id_foo`
Repeat the same steps for the
`bar`
account. Now, we have two private keys, `id_foo`
and `id_bar`
located at the `~/.ssh`
folder.#Adding keys to SSH agent
// Delete cached keys
$ ssh-add -D
// Start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
// Add your SSH private keys to the ssh-agent
$ ssh-add ~/.ssh/id_foo
$ ssh-add ~/.ssh/id_bar
#Mapping keys to GitHub repos
This step lets SSH know which private key should be used for particular hosts.
$ cd ~/.ssh
$ touch config
Add the following content to the
`config`
file:Host github.com-foo
HostName github.com
User git
IdentityFile ~/.ssh/id_foo
IdentitiesOnly yes
Host github.com-bar
HostName github.com
User git
IdentityFile ~/.ssh/id_bar
IdentitiesOnly yes
You'll realize that
`github.com-foo`
and `github.com-bar`
look invalid hosts, but actually they are treated as aliases. SSH maps it with the `HostName`
option and uses the private key in the `IdentityFile`
option.#Changing GitHub settings
Let's say that the
`foo`
account accesses a GitHub repose whose URL is `github.com/foo/a-foo-repos`
. Go to its cloned folder, and change the `.git/config`
file as below.It's worth noting that the SSH host
`github.com-foo`
created in the previous step is used:[remote "origin"]
url = git@github.com-foo:foo/a-foo-repos.git
Apply the similar settings for the
`bar`
repositories.#See also
Questions? 🙋
Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.
Recent posts ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
Newsletter 🔔
If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn