Skip to content

Instantly share code, notes, and snippets.

@Valkhan
Created September 27, 2024 17:46
Show Gist options
  • Save Valkhan/8010d5f25839a96950a02d2313326967 to your computer and use it in GitHub Desktop.
Save Valkhan/8010d5f25839a96950a02d2313326967 to your computer and use it in GitHub Desktop.

Using multiple SSH accounts

I'm assuming you're familiar with git and it's trying to use SSH keys to access your repos and have multiple github accounts, lets say a personal a acc and a business acc.

Create ssh-key with your e-mail linked to your github account

ssh-keygen -t ed25519 -C "[email protected]"

Add the .pub key to your github>profile>ssh as a new authentication key

  • Copy the contents of your .pub file
  • Paste the contents and save

Copy the generated key and key.pub into your Windows/linux .ssh folder under your O.S user profile folder:

  • windows: %userprofile%/.ssh (if not exists, create the .ssh folder)
  • linux: ~/.ssh

Using multiple keys:

In order to use multiple accounts/keys you'll have to use "domain alias" to specify a target key.

A common clone command looks like: git clone [email protected]:[user_name]/[repo_name].git A "domain alias" is the @github.com after git.

Create a file or edit if exists on [filepath].ssh/config

The file name is "config" without extensions inside the .ssh folder.

Add the contents below:

# Comment to identify your account 1
Host github-acc-1
  HostName github.com
  User git
  IdentityFile ~/.ssh/[your key file name without .pub]
  IdentitiesOnly yes

# Comment to identify your account 2
Host github-acc-1
  HostName github.com
  User git
  IdentityFile ~/.ssh/[your key file name without .pub]
  IdentitiesOnly yes

Clone the repo using the domain alias replacing github.com with the acc you want to use

git clone git@github-acc-1:[user_name]/[repo_name].git

Your're good to go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment