Managing Multiple SSH Keys for Git Access
A comprehensive guide for managing different SSH keys across multiple computers, Git servers, and accounts.
Overview
Section titled “Overview”This guide helps you configure Git to use different SSH credentials per repository, rather than relying on a single global configuration. This approach allows you to:
- Access multiple Git servers (GitHub, GitLab, self-hosted instances)
- Use different accounts on the same server (personal and work accounts)
- Manage separate keys per computer for better security
- Easily revoke access if a device is lost or compromised
How it works
Section titled “How it works”Git uses SSH to authenticate with remote servers. SSH identifies you using a key pair (public and private keys). By configuring SSH to use different keys for different “hosts,” you can route connections appropriately. The trick is using SSH host aliases that map to the actual server but specify which key to use.
Example scenario
Section titled “Example scenario”Git servers:
- github.com
- gitlab.company.edu (work server)
Computers:
- Home laptop (Linux)
- Work Windows PC
- Work MacBook
Accounts:
- personal@example.com → GitHub personal account
- secondary@example.com → GitHub secondary account
- workid@company.edu → Company GitLab work account
Security strategy: Create separate SSH keys for each laptop. If a device is compromised or lost, revoke only that device’s key without affecting other machines.
Complete setup process
Section titled “Complete setup process”Step 1: Create SSH config file
Section titled “Step 1: Create SSH config file”The SSH config file tells SSH which key to use for which connection.
Linux/macOS
Section titled “Linux/macOS”cd ~/.sshtouch configchmod 600 configThe chmod 600 command sets permissions so only you can read/write the
file.
Windows
Section titled “Windows”Navigate to C:\Users\YourUsername\.ssh\ and create a file named config
(no extension) if it doesn’t exist.
Step 2: Generate SSH keys
Section titled “Step 2: Generate SSH keys”Create one SSH key per account per computer. Use descriptive names that identify the device, account, and server.
Command:
ssh-keygen -t ed25519 -C "email@example.com" -f ~/.ssh/filenameExample naming convention for home laptop:
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_homelaptop_personal_githubssh-keygen -t ed25519 -C "secondary@example.com" -f ~/.ssh/id_ed25519_homelaptop_secondary_githubssh-keygen -t ed25519 -C "workid@company.edu" -f ~/.ssh/id_ed25519_homelaptop_work_gitlabStep 3: Configure SSH host aliases
Section titled “Step 3: Configure SSH host aliases”Edit ~/.ssh/config to map custom host aliases to the actual servers with
specific keys.
Example config:
Host personal.github.com Hostname github.com User git IdentityFile ~/.ssh/id_ed25519_homelaptop_personal_github IdentitiesOnly yes
Host secondary.github.com Hostname github.com User git IdentityFile ~/.ssh/id_ed25519_homelaptop_secondary_github IdentitiesOnly yes
Host work.gitlab.company.edu Hostname gitlab.company.edu User git IdentityFile ~/.ssh/id_ed25519_homelaptop_work_gitlab IdentitiesOnly yesWhat each field means:
Host— the alias you’ll use in Git commandsHostname— the actual server addressUser— alwaysgitfor Git serversIdentityFile— path to your private keyIdentitiesOnly yes— only use this specific key (don’t try others)
Step 4: Upload public keys to Git servers
Section titled “Step 4: Upload public keys to Git servers”GitHub
Section titled “GitHub”- Copy your public key:
Terminal window cat ~/.ssh/id_ed25519_homelaptop_personal_github.pub - Log in to GitHub
- Go to https://github.com/settings/keys
- Click “New SSH key”
- Paste the public key and give it a descriptive title (e.g., “Home Laptop”)
GitLab (company)
Section titled “GitLab (company)”- Copy your public key:
Terminal window cat ~/.ssh/id_ed25519_homelaptop_work_gitlab.pub - Log in to GitLab
- Navigate to your SSH keys settings (typically under user settings)
- Paste the public key and add a descriptive title
Step 5: Test SSH connection
Section titled “Step 5: Test SSH connection”Verify that authentication works using your host alias:
ssh -T personal.github.comSuccessful response from GitHub:
Hi personaluser! You've successfully authenticated, but GitHub does not provide shell access.Test each configured host:
ssh -T secondary.github.comssh -T work.gitlab.company.eduWorking with repositories
Section titled “Working with repositories”Cloning a new repository
Section titled “Cloning a new repository”Replace the standard hostname with your SSH config alias:
Standard clone command:
git clone git@github.com:username/website.gitUsing SSH alias:
git clone git@secondary.github.com:username/website.gitThe alias ensures the correct SSH key is used automatically.
Configuring Git identity per repository
Section titled “Configuring Git identity per repository”After cloning, set the user identity for commits in that specific repository:
cd your-repogit config user.email "you@example.com"git config user.name "Your Name"Updating existing repositories
Section titled “Updating existing repositories”If you have repositories that were cloned with the standard hostname, update them to use your SSH alias:
1. Check current remote URL:
git remote -vExample output:
origin git@github.com:username/repository.git (fetch)origin git@github.com:username/repository.git (push)2. Update to use SSH alias:
git remote set-url origin git@personal.github.com:username/repository.git3. Verify the change:
git remote -vUpdated output:
origin git@personal.github.com:username/repository.git (fetch)origin git@personal.github.com:username/repository.git (push)Quick reference
Section titled “Quick reference”File locations
Section titled “File locations”| OS | SSH config location |
|---|---|
| Linux/macOS | ~/.ssh/config |
| Windows | C:\Users\YourUsername\.ssh\config |
Common commands
Section titled “Common commands”# Generate new keyssh-keygen -t ed25519 -C "email@example.com" -f ~/.ssh/keyname
# View public keycat ~/.ssh/keyname.pub
# Test SSH connectionssh -T host.alias.com
# Clone with aliasgit clone git@host.alias.com:username/repo.git
# Update remote URLgit remote set-url origin git@host.alias.com:username/repo.git
# Set repo-specific identitygit config user.email "email@example.com"git config user.name "Your Name"Troubleshooting
Section titled “Troubleshooting”Permission denied (publickey)
Section titled “Permission denied (publickey)”Symptoms: Git operations fail with authentication errors.
Solutions:
- Verify your public key is uploaded to the Git server
- Test the SSH connection:
ssh -T your.host.alias - Check SSH config syntax (no typos in paths or aliases)
- Ensure private key file permissions are restrictive:
chmod 600 ~/.ssh/keyname - Verify
IdentityFilepath in config points to the correct private key
SSH uses wrong key
Section titled “SSH uses wrong key”Symptoms: Authentication succeeds but with the wrong account.
Solutions:
- Add
IdentitiesOnly yesto your SSH config host entry - Ensure you’re using the host alias in git commands, not the actual hostname
- Check that the host alias in your Git remote URL matches your SSH config
“Could not open a connection to your authentication agent”
Section titled ““Could not open a connection to your authentication agent””Symptoms: On some systems, SSH agent issues prevent key usage.
Solution: Start the SSH agent and add your key:
eval "$(ssh-agent -s)"ssh-add ~/.ssh/your_private_keyBest practices
Section titled “Best practices”- Descriptive key names — include device, account, and server in the filename
- One key per device — makes it easy to revoke access for lost/stolen devices
- Use passphrases — protect your private keys with strong passphrases
- Repository-specific config — never use
git config --globalfor account-specific settings - Test after setup — always test SSH connections before attempting Git operations
- Document your aliases — keep a note of which alias corresponds to which account