Let suppose I have two github accounts, https://github.com/nmhieu-office and https://github.com/nmhieu-personal. Now i want to setup my Ubuntu machine to easily talk to both the github accounts.
NOTE: This logic can be extended to more than two accounts also. :)
The setup can be done in 6 easy steps:
- Step 1 : Create SSH keys for all accounts
- Step 2 : Starting and Configuring the SSH Agent
- Step 3 : Adding SSH Keys to the Agent
- Step 4 : Uploading Public Keys to GitHub
- Step 5 : Configuring SSH with Host Aliases
- Step 6 : Cloning GitHub repositories using different accounts
Generate an Ed25519 key pair for each GitHub account to ensure strong security and performance:
ssh-keygen -t ed25519 -C "work_email@example.com" -f ~/.ssh/id_ed25519_work
ssh-keygen -t ed25519 -C "personal_email@example.com" -f ~/.ssh/id_ed25519_personalIf Ed25519 is unavailable, you may use RSA with 4096 bits:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa_workEach command produces a private key file (no extension) and a public key file (.pub) (Generating a new SSH key and adding it to the ssh-agent).
Launch the agent and set environment variables by running:
eval "$(ssh-agent -s)"This outputs the agent’s PID and sets SSH_AUTH_SOCK for the session (Generating a new SSH key and adding it to the ssh-agent).
Optionally, create a user‐level systemd service (~/.config/systemd/user/ssh-agent.service) so the agent survives reboots, then enable and start it:
[Service]
ExecStart=/usr/bin/ssh-agent -D -a $XDG_RUNTIME_DIR/ssh-agent.socketsystemctl --user daemon-reload
systemctl --user enable ssh-agent
systemctl --user start ssh-agentEnsure your shell loads the socket path on login:
export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"Unlock each key once per session to avoid repeated passphrase prompts:
ssh-add ~/.ssh/id_ed25519_work
ssh-add ~/.ssh/id_ed25519_personalUpon successful addition, you’ll see confirmation lines indicating each key’s identity (How to Set Up SSH Keys on Ubuntu 20.04 - Linuxize).
- Copy the Public Key
sudo apt update && sudo apt install xclip xclip -sel clip < ~/.ssh/id_ed25519_work.pub
- Add Key in GitHub
- Go to Settings → SSH and GPG keys → New SSH key
- Paste the key, add a descriptive title, and save (Adding a new SSH key to your GitHub account).
Repeat for the personal key.
Edit (or create) ~/.ssh/config and add entries for each account:
# Work account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
# Personal account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
- Host
github-workand Hostgithub-personalact as distinct aliases (Managing multiple accounts - GitHub Docs). IdentitiesOnly yesrestricts SSH to use only the specified key for each alias.
Secure the config file:
chmod 600 ~/.ssh/configSo we are done with our setups and now its time to see it in action. We will clone a repository using one of the account we have added.
Make a new project folder where you want to clone your repository and go to that directory from your terminal.
For Example:
You are working in company project with owner MinhHieu-Nguyen-dn and need to clone the private repo machine_learning_from_scratch to your personal laptop.
You have access to this repo with GitHub account id_ed25519_work (company GitHub user - using company's email) but not id_ed25519_personal (normal, default GitHub user on your local machine - using personal email).
Now for cloning the repo use the below command:
git clone git@{your host from Step 4}:{Repo's owner Git username}/{Repo's name}.git
[e.g.] git clone git@github-work:MinhHieu-Nguyen-dn/machine_learning_from_scratch.git
From now on, to ensure that our commits and pushes from each repository on the system uses the correct GitHub user — we will have to configure user.email and user.name in every repository freshly cloned or existing before.
To do this use the following commands.
git config user.email "my_office_email@gmail.com"
git config user.name "...-office"
git config user.email "my_personal_email@gmail.com"
git config user.name "...-personal"
Pick the correct pair for your repository accordingly.
To push or pull to the correct account we need to add the remote origin to the project
git remote add origin git@{host}:{Git username}
git remote add origin git@git-personal:nmhieu-personal
git remote add origin git@git-office:nmhieu-office
Now you can use:
git push
git pull
By following this document, you can maintain multiple GitHub accounts on Ubuntu with secure SSH configurations, streamlined authentication, and properly managed Git remotes.
Original Post: https://gist.github.com/rahularity/86da20fe3858e6b311de068201d279e3.
Windows version: https://gist.github.com/MinhHieu-Nguyen-dn/9301f14e173695ee334dccdbbd0909af.