Skip to content

Instantly share code, notes, and snippets.

@souhaiebtar
Created June 30, 2025 10:34
Show Gist options
  • Save souhaiebtar/b32159639f5fe8828787663254ef2adc to your computer and use it in GitHub Desktop.
Save souhaiebtar/b32159639f5fe8828787663254ef2adc to your computer and use it in GitHub Desktop.
[ssh config] ssh config #ssh #config
To configure your .ssh/config file to use two different GitHub accounts with separate SSH keys, follow these steps based on best practices:
Generate distinct SSH key pairs for each GitHub account, if you haven't already, for example:
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_account1
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_account2
Add the SSH keys to the ssh-agent:
ssh-add ~/.ssh/id_rsa_account1
ssh-add ~/.ssh/id_rsa_account2
Configure your ~/.ssh/config file with Host aliases pointing to GitHub but specifying different IdentityFiles:
# Default GitHub account (account1)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_account1
IdentitiesOnly yes
# Second GitHub account (account2) with alias
Host github-account2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_account2
IdentitiesOnly yes
Usage: When cloning or working with repositories under the second GitHub account, use the host alias from the config:
git clone git@github-account2:username/repo.git
For your primary account, you can continue to use:
git clone [email protected]:username/repo.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment