Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save MinhHieu-Nguyen-dn/d20cabcdc20990e9fb6c13e8ed4c1fdc to your computer and use it in GitHub Desktop.

Select an option

Save MinhHieu-Nguyen-dn/d20cabcdc20990e9fb6c13e8ed4c1fdc to your computer and use it in GitHub Desktop.
(Ubuntu version) How To Work With Multiple Github Accounts on a single Machine

(Ubuntu version) How To Work With Multiple Github Accounts on a single Machine

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:

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

Step 1

Generating SSH Key Pairs

1.1 Create Separate Key Files

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_personal

If 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_work

Each 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).


Step 2

Starting and Configuring the SSH Agent

2.1 Manual (Per-Session)

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).

2.2 Persistent (Systemd)

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.socket
systemctl --user daemon-reload  
systemctl --user enable ssh-agent  
systemctl --user start ssh-agent

Ensure your shell loads the socket path on login:

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"

Step 3

Adding SSH Keys to the Agent

Unlock each key once per session to avoid repeated passphrase prompts:

ssh-add ~/.ssh/id_ed25519_work  
ssh-add ~/.ssh/id_ed25519_personal

Upon successful addition, you’ll see confirmation lines indicating each key’s identity (How to Set Up SSH Keys on Ubuntu 20.04 - Linuxize).


Step 4

Uploading Public Keys to GitHub

  1. Copy the Public Key
    sudo apt update && sudo apt install xclip  
    xclip -sel clip < ~/.ssh/id_ed25519_work.pub
  2. Add Key in GitHub

Repeat for the personal key.


Step 5

Configuring SSH with Host Aliases

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

Secure the config file:

chmod 600 ~/.ssh/config

Step 6

Cloning GitHub repositories using different accounts

So 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

Finally

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment