Overview
Managing multiple GitHub accounts on a single Ubuntu system requires careful configuration of SSH keys and Git identity. Without proper setup, authentication failures and commit history errors are common.
This guide provides a systematic approach to:
- Generate and manage separate SSH keys for each GitHub account
- Configure SSH to route authentication correctly
- Set Git identity per repository
- Automate setup with scripts and aliases
The Core Problem
When managing multiple GitHub accounts on one system, three conflicts arise:
- SSH Authentication — By default, SSH tries a single key. With two accounts, the default key fails for one account.
- Git Identity — Global Git configuration applies to all repositories. Using the same name/email across both accounts produces incorrect commit history.
- Repository Management — Without a system, developers must manually track which key and identity applies to each repository.
The solution separates concerns: SSH handles authentication, Git handles identity, and configuration files automate the routing.
Prerequisites
- Ubuntu 22.04 LTS or later
- Access to two GitHub accounts
- Terminal access with
ssh-keygenandgitinstalled - Basic familiarity with SSH and Git
Step 1: Generate SSH Key Pairs
Create separate key pairs for each GitHub account using Ed25519 (preferred for security and performance).
Generate Personal Account Key
ssh-keygen -t ed25519 -C "personal.email@example.com" -f ~/.ssh/id_ed25519_personalWhen prompted for a passphrase, either press Enter for no passphrase or enter a secure passphrase. Passphrases add security but require SSH agent configuration (see Step 6).
This generates:
~/.ssh/id_ed25519_personal— Private key (must be kept secret)~/.ssh/id_ed25519_personal.pub— Public key (to be added to GitHub)
Generate Work Account Key
ssh-keygen -t ed25519 -C "work.email@company.com" -f ~/.ssh/id_ed25519_workVerify Key Generation
ls -la ~/.ssh/id_ed25519_*Expected output:
-rw------- id_ed25519_personal
-rw------- id_ed25519_personal.pub
-rw------- id_ed25519_work
-rw------- id_ed25519_work.pub
Keys should have permissions 600 (read/write for owner only).
Step 2: Add Public Keys to GitHub
Add Personal Key to Personal Account
Display the personal public key:
cat ~/.ssh/id_ed25519_personal.pub- Log in to the personal GitHub account
- Navigate to Settings → SSH and GPG keys
- Click New SSH key
- Enter a descriptive title (e.g., “Ubuntu Desktop - Personal”)
- Paste the complete public key into the Key field
- Click Add SSH key
Add Work Key to Work Account
Display the work public key:
cat ~/.ssh/id_ed25519_work.pub- Log in to the work GitHub account
- Navigate to Settings → SSH and GPG keys
- Click New SSH key
- Enter a descriptive title (e.g., “Ubuntu Desktop - Work”)
- Paste the complete public key into the Key field
- Click Add SSH key
Verify Key Authentication
Test each key independently:
ssh -i ~/.ssh/id_ed25519_personal -T git@github.comExpected output:
Hi personal-username! You've successfully authenticated, but GitHub does not provide shell access.
ssh -i ~/.ssh/id_ed25519_work -T git@github.comExpected output:
Hi work-username! You've successfully authenticated, but GitHub does not provide shell access.
If authentication fails, verify:
- The
.pubfile was copied (not the file without.pub) - The key was added to the correct GitHub account
- Account credentials are correct
Step 3: Configure SSH Config File
The SSH config file (~/.ssh/config) directs SSH to use the appropriate key based on the hostname alias.
Create or edit the SSH config file:
nano ~/.ssh/configAdd the following configuration:
# Personal GitHub Account
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
AddKeysToAgent yes
# Work GitHub Account
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
AddKeysToAgent yes
Save the file with Ctrl+X, then Y, then Enter.
Configuration Explanation
- Host — The alias used in Git commands (e.g.,
git@github.com-personal:...) - HostName — The actual hostname both aliases resolve to
- User — The SSH user (always
gitfor GitHub) - IdentityFile — The private key SSH uses for this host
- AddKeysToAgent — Automatically adds the key to the SSH agent (useful with passphrases)
When cloning from git@github.com-personal:username/repo.git, SSH uses the personal key. When cloning from git@github.com-work:username/repo.git, SSH uses the work key.
Step 4: Configure Git Identity
Git’s user.name and user.email settings determine the author information in commits. Configure a global default and override per repository.
Set Global Identity (Default Account)
git config --global user.name "Your Personal Name"
git config --global user.email "personal.email@example.com"This applies to all repositories unless overridden locally.
Override Identity Per Repository
For work repositories, override the global configuration locally:
cd ~/path/to/work/repository
git config user.name "Your Work Name"
git config user.email "work.email@company.com"This local configuration applies only to that repository and does not affect other repositories.
Verify Current Configuration
git config user.name
git config user.emailUse git config --global to check global settings. Without the flag, the command returns local settings if they exist, otherwise global settings.
Step 5: Clone Repositories with Correct SSH Alias
When cloning repositories, use the appropriate SSH hostname alias to ensure SSH routes to the correct key.
Clone Personal Repository
git clone git@github.com-personal:personal-username/repository.git
cd repositoryClone Work Repository
git clone git@github.com-work:work-username/repository.git
cd repositoryCritical Detail
The SSH hostname must match the SSH config alias:
- Use
git@github.com-personal:for personal repositories - Use
git@github.com-work:for work repositories
Using git@github.com: (without the alias) will not route to the correct key.
Step 6: Create Command Aliases
Command aliases reduce the likelihood of errors and simplify the workflow.
Add aliases to ~/.bashrc:
nano ~/.bashrcAppend the following lines:
# Git clone aliases
alias git-personal='git clone git@github.com-personal:'
alias git-work='git clone git@github.com-work:'
# Verify current Git configuration
alias git-whoami='echo "Name: $(git config user.name)" && echo "Email: $(git config user.email)"'
# List SSH keys
alias ssh-keys='ls -lah ~/.ssh/id_ed25519_*'Reload the shell configuration:
source ~/.bashrcUsing Aliases
Clone personal repository:
git-personal personal-username/repository.gitClone work repository:
git-work work-username/repository.gitVerify current Git identity before committing:
git-whoamiStep 7: Automate Repository Configuration
Create a script to automatically configure SSH and Git identity for repositories.
Create ~/setup-git-repo.sh:
nano ~/setup-git-repo.shPaste the following script:
#!/bin/bash
# Automatically configure SSH and Git identity for personal or work repositories
if [ $# -eq 0 ]; then
echo "Usage: setup-git-repo.sh <personal|work> <repository-path>"
echo ""
echo "Examples:"
echo " setup-git-repo.sh personal ~/Projects/my-app"
echo " setup-git-repo.sh work ~/Projects/work-app"
exit 1
fi
ACCOUNT_TYPE=$1
REPO_PATH=$2
if [ ! -d "$REPO_PATH" ]; then
echo "Error: Directory '$REPO_PATH' does not exist"
exit 1
fi
cd "$REPO_PATH"
if [ "$ACCOUNT_TYPE" = "personal" ]; then
echo "Configuring repository for PERSONAL account..."
git config user.name "Your Personal Name"
git config user.email "personal.email@example.com"
git remote set-url origin $(git config --get remote.origin.url | sed 's/github\.com/github.com-personal/')
echo "✅ Repository configured for personal account"
elif [ "$ACCOUNT_TYPE" = "work" ]; then
echo "Configuring repository for WORK account..."
git config user.name "Your Work Name"
git config user.email "work.email@company.com"
git remote set-url origin $(git config --get remote.origin.url | sed 's/github\.com/github.com-work/')
echo "✅ Repository configured for work account"
else
echo "Error: Account type must be 'personal' or 'work'"
exit 1
fi
echo ""
echo "Configuration Verification:"
echo " User Name: $(git config user.name)"
echo " User Email: $(git config user.email)"
echo " Remote URL: $(git remote get-url origin)"Make the script executable:
chmod +x ~/setup-git-repo.shUsing the Setup Script
After cloning a repository, run the script to automatically configure both SSH and Git:
cd ~/path/to/repository
~/setup-git-repo.sh personal $(pwd)or
~/setup-git-repo.sh work $(pwd)The script:
- Sets the Git user name and email
- Updates the remote URL to use the correct SSH alias
- Displays the configuration for verification
Step 8: SSH Agent Configuration (Optional)
If SSH keys use passphrases, SSH agent caches passphrases to avoid repeated prompts.
Manual SSH Agent Setup
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_workAutomatic SSH Agent Startup
Add to ~/.bashrc:
eval "$(ssh-agent -s)" 2>/dev/null
ssh-add ~/.ssh/id_ed25519_personal 2>/dev/null
ssh-add ~/.ssh/id_ed25519_work 2>/dev/nullThe SSH agent starts when a new terminal session begins and loads both keys.
Complete Workflow Example
Initial Repository Setup
# Clone work repository using work alias
git clone git@github.com-work:company-username/project.git
cd project
# Automatically configure SSH and Git
~/setup-git-repo.sh work $(pwd)
# Verify configuration
git-whoami
# Output:
# Name: Your Work Name
# Email: work.email@company.comDevelopment Workflow
# Make changes
echo "new feature" > feature.txt
# Stage changes
git add feature.txt
# Commit (uses configured identity)
git commit -m "Add new feature"
# Push (uses work SSH key)
git push origin mainSwitching Between Accounts
# Switch to personal repository
cd ~/path/to/personal/repository
# Verify correct identity
git-whoami
# Continue with development
git add .
git commit -m "Update personal project"
git push origin mainTroubleshooting
SSH Authentication Failure: “Permission denied (publickey)”
Cause: SSH unable to authenticate with the repository’s key.
Diagnosis:
ssh -vvv git@github.com-workSolution:
- Verify the SSH config file is correct:
cat ~/.ssh/config - Verify the public key was added to the correct GitHub account
- Verify the repository remote URL uses the correct alias:
git remote get-url origin - If cloned without the alias, update the remote:
git remote set-url origin git@github.com-work:username/repo.git
Commit History Shows Incorrect Author
Cause: Git identity was not configured when commits were made.
Prevention: Always verify Git identity before committing: git-whoami
Solution (if not yet pushed):
# Configure correct identity
git config user.email "correct.email@example.com"
# Amend the last commit
git commit --amend --no-edit
# Push the corrected commit
git push origin branch-nameSSH Passphrase Prompt Appears Repeatedly
Cause: SSH agent not configured or not running.
Solution:
# Start SSH agent for current session
eval "$(ssh-agent -s)"
# Add keys to agent
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_work
# For persistence, add to ~/.bashrc (see Step 8)Repository Cloned Without SSH Alias
Cause: Repository was cloned using git@github.com: instead of the alias.
Solution:
# Update the remote URL
git remote set-url origin git@github.com-work:username/repository.git
# Verify the change
git remote get-url origin
# Subsequent pushes will use the correct keyBest Practices
-
Always verify identity before committing:
git-whoami -
Configure new repositories immediately after cloning:
~/setup-git-repo.sh personal ~/path/to/repo -
Use descriptive SSH key titles in GitHub:
- Include the machine name or purpose
- Example: “Ubuntu Desktop - Work”
-
Use SSH config aliases consistently:
- Apply the same naming convention across projects
- Document the alias naming scheme for team members
-
Keep private keys secure:
# Verify file permissions (should be 600) ls -la ~/.ssh/id_ed25519_* -
Never commit private keys or configuration files containing secrets:
- Add
~/.ssh/configto.gitignoreif including machine-specific configuration
- Add
-
Test authentication after initial setup:
ssh -i ~/.ssh/id_ed25519_work -T git@github.com -
Use passphrases for private keys:
- Protects keys if the machine is compromised
- SSH agent mitigates repeated passphrase entry
Summary
Managing multiple GitHub accounts requires coordinating three components:
| Component | Purpose | Configuration |
|---|---|---|
| SSH Keys | Authentication | Two key pairs in ~/.ssh/ |
| SSH Config | Route keys | ~/.ssh/config with host aliases |
| Git Config | Author identity | Global default + per-repo overrides |
Following this workflow eliminates authentication errors, ensures correct commit authorship, and reduces configuration mistakes through automation.
Last updated: June 2026 | Tested on Ubuntu 22.04 LTS