Using Multiple GitHub Accounts | Cloning Repos with SSH
Learn how to manage multiple GitHub accounts on the same machine and securely clone private repositories using SSH keys.
When working with GitHub professionally, it's common to face two scenarios:
- Using multiple GitHub accounts on the same machine
- Cloning private repositories on your machine
- Cloning private repositories on EC2 instance
SSH basics
SSH authentication works using a key pair:
- Private key → stays on your machine / server
- Public key → added to GitHub
Once GitHub trusts your public key, it allows access without passwords or tokens.
Sounds fancy, but all you need is the following single line of code.
ssh-keygen -t ed25519 -f ~/.ssh/github_key -C "identity"Using multiple GitHub accounts on the same machine
1. Generate separate SSH keys
ssh-keygen -t ed25519 -f ~/.ssh/github_work_1 -C "github_work_1"
ssh-keygen -t ed25519 -f ~/.ssh/github_work_2 -C "github_work_2"Note: I prefer using email id as comments, like
ssh-keygen -t ed25519 -f ~/.ssh/github_work_1 -C "work@domain.com"
This creates:
~/.ssh/github_work_1
~/.ssh/github_work_1.pub
~/.ssh/github_work_2
~/.ssh/github_work_2.pubCheck out Typical Next Step
2. Add keys to GitHub accounts
-
Work account
- GitHub → Settings → SSH and GPG keys
- Add
github_work_1.pub
-
Work account
- GitHub → Settings → SSH and GPG keys
- Add
github_work_2.pub
3. Configure SSH to map accounts
Edit ~/.ssh/config:
Host github_work_1 github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_work_1
Host github_work_2 github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_work_2Set permissions, if needed:
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/github_work_1 ~/.ssh/github_work_24. Clone repos using the correct host
# Work repo 1
git clone git@github_work_1:username/repo.git
# Work repo 2
git clone git@github_work_2:username/repo.gitthen change config for each repos if needed:
git config user.email "work@doamin.com"
git config user.name "username"Each repo now automatically uses the correct GitHub account.
Cloning a private GitHub repo on an EC2 instance
When deploying from EC2, SSH keys are preferred over Personal Access Tokens. We can follow same procedure used above.
1. Generate SSH key on EC2
Generate a dedicated key for deployments:
ssh-keygen -t ed25519 -f ~/.ssh/ec2_github_key -C "ec2-deploy"2. Configure SSH on EC2
If you wish to use multiple githubs in the same ec2 machine then you can follow above config methods. If you are going to use single account then follow following procedure,
Add the key to SSH config so GitHub always uses it:
cat << 'EOF' >> ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/ec2_github_key
IdentitiesOnly yes
EOFIf permissions related issues occured:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config ~/.ssh/ec2_github_key
chmod 644 ~/.ssh/ec2_github_key.pub3. Add the key to GitHub
You have two options:
Option A: Add as account SSH key
- Copy the public key:
cat ~/.ssh/ec2_github_key.pub- GitHub → Settings → SSH and GPG keys → New SSH key
- Name:
EC2 ProdorEC2 Dev - Paste the key and save
Option B: Add as repo deploy key (recommended for prod)
Use this for single-repo access (most secure).
- Repo → Settings → Deploy keys → Add deploy key
- Paste
ec2_github_key.pub - Leave Write access unchecked unless required
All set now you can use ssh based repo cloning.
Final thoughts
- Use one SSH key per account or server
- Use deploy keys for production EC2
- Never reuse your personal laptop SSH keys on servers