Managing Multiple GitHub SSH Keys
When working with multiple GitHub accounts (e.g., personal and work) on the same machine, using a single default SSH key results in permission errors. This guide details how to generate named SSH keys, configure your SSH client to route traffic correctly, and update Git remotes.
Step 1: Generate the Named SSH Key
First, generate a new SSH key specifically for your secondary account. We will name the file github-myconfig to distinguish it from your default key.
macOS
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/github-myconfig
Linux
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/github-myconfig
Windows
ssh-keygen -t ed25519 -C "your_email@example.com" -f "$env:USERPROFILE\.ssh\github-myconfig"
When prompted to enter a passphrase, you can press Enter to leave it empty, or secure it with a password.
Step 2: Install the Public Key to GitHub
You must add the contents of your newly generated public key to the correct GitHub account. First, copy the public key to your clipboard.
macOS
pbcopy < ~/.ssh/github-myconfig.pub
Linux
cat ~/.ssh/github-myconfig.pub | xclip -selection clipboard
Note: If you do not have xclip installed, use cat ~/.ssh/github-myconfig.pub and copy manually.
Windows
Get-Content "$env:USERPROFILE\.ssh\github-myconfig.pub" | clip
- Log in to the specific account on GitHub (opens in a new tab).
- Go to Settings > SSH and GPG keys.
- Click the New SSH key button.
- Paste the copied key into the "Key" field, give it a descriptive title, and click Add SSH key.
Step 3: Update the SSH Config File
We need to tell your local SSH client to use this specific key when it sees a specific host alias. We do this by creating or editing the SSH config file.
Open the config file:
macOS
touch ~/.ssh/config && nano ~/.ssh/config
Linux
touch ~/.ssh/config && nano ~/.ssh/config
Windows
if (!(Test-Path "$env:USERPROFILE\.ssh\config")) { New-Item -ItemType File -Path "$env:USERPROFILE\.ssh\config" -Force | Out-Null }; notepad "$env:USERPROFILE\.ssh\config"
Add the configuration block:
Paste the following configuration. OpenSSH handles the ~/.ssh/ paths uniformly across all platforms, including Windows.
# Default GitHub Account (Personal/Existing)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Secondary GitHub Account (Custom Alias)
Host github-myconfig
HostName github.com
User git
IdentityFile ~/.ssh/github-myconfig
Step 4: Configure the Git Remote
Now that SSH routing is set up, you need to tell Git to use your new alias (github-myconfig) instead of the default github.com domain when pushing and pulling.
For updating an existing repository:
Navigate to your repository directory in your terminal and run:
git remote set-url origin git@github-myconfig:username/repository.git
For cloning a new repository:
When copying the SSH clone URL from GitHub, replace github.com with your alias before executing the command:
git clone git@github-myconfig:username/repository.git