Setup Multiple GitHub Accounts with SSH Keys on the Same Machine

Minh Vu

By Minh Vu

Updated Oct 18, 2023

Figure: Setup Multiple GitHub Accounts with SSH Keys on the Same Machine

Disclaimer: All content on this website is derived directly from my own expertise and experiences. No AI-generated text or automated content creation tools are used.

As a software developer, you might find yourself needing to manage multiple GitHub accounts for various projects or collaborations. However, juggling multiple accounts on the same machine can be a bit tricky. That's where SSH keys come to the rescue!

In this article, we'll explore how to set up multiple GitHub accounts on the same machine using SSH keys, ensuring secure and seamless authentication. So, let's dive in and get started!

Contents

How to Set Up Multiple GitHub Accounts with SSH Keys

Step 1: Create SSH Keys for All Accounts

The first step is to generate unique SSH keys for each of your GitHub accounts. These keys will serve as your secure credentials when authenticating with GitHub.

If you are using Windows, I have already brought you a tutorial on how to create SSH keys on Windows.

To create an SSH key, open your terminal and follow these steps:

  1. Navigate to your home directory by running the following command:

    console
    cd ~
  2. Generate an SSH key pair by executing the following command:

    console
    ssh-keygen -t ed25519 -C "your_email@example.com" -f "your_github_username"

Make sure to replace your_email@example.com with the email associated with your GitHub account, and your_github_username with your GitHub username.

create-ssh-key-github
Figure: create-ssh-key-github
3. Next, you will be prompted to enter a passphrase. While it's optional, adding a passphrase adds an extra layer of security to your SSH keys. Note that the passphrase won't be visible while typing. 4. Repeat the above steps to create SSH keys for all your GitHub accounts, ensuring a unique key pair for each account. In this case, I will create two accounts as follows:

console
ssh-keygen -t ed25519 -C "wisecodeteam@gmail.com" -f "wisecodeteam" ssh-keygen -t ed25519 -C "dminhvu@gmail.com" -f "dminhvu"

Step 2: Add SSH Keys to SSH Agent

Once you have generated SSH keys for all your GitHub accounts, the next step is to add them to the SSH agent. The SSH agent will securely manage your SSH keys and handle authentication with GitHub.

Follow these steps to add your SSH keys to the SSH agent:

  1. Start the SSH agent by running the following command:
    console
    eval "$(ssh-agent -s)"
  2. Add your SSH private key to the agent by executing the command:
    console
    ssh-add ~/.ssh/your_github_username
    Replace ~/.ssh/your_github_username with the path to your SSH private key.

    Note that since we have already specified the file name using -f "your_github_username" in the above steps, then your SSH key will be store in ~/.ssh/your_github_username. If you specified another name, -f "something_different", the SSH key will be stored in ~/.ssh/something_different.

    ssh-agent-add-ssh-key
    Figure: ssh-agent-add-ssh-key

  3. Repeat the above step for each SSH private key you generated for your GitHub accounts.

Step 3: Add SSH Public Keys to GitHub

Now that your SSH keys are set up and added to the SSH agent, it's time to associate them with your GitHub accounts. By adding the SSH public keys to your GitHub account settings, you enable secure authentication using the corresponding private keys.

To add your SSH public key to GitHub, follow these steps:

  1. Copy the SSH public key for the first GitHub account by executing the following command:
    console
    pbcopy < ~/.ssh/your_github_username.pub
    This command copies the content of the public key to your clipboard.
  2. Open a web browser and navigate to GitHub. Sign in to the GitHub account you wish to associate with the SSH key.
  3. In the upper-right corner, click on your profile photo and select Settings from the dropdown menu.
  4. On the left sidebar, click on SSH and GPG keys.
  5. Click on the New SSH key button.
  6. Provide a descriptive title for the SSH key in the Title field.
  7. Paste the SSH public key into the Key field. To paste from your clipboard, use Cmd + V on macOS or Ctrl + V on Windows or Linux.
  8. Finally, click on the Add SSH key button to save the SSH key to your GitHub account.
  9. Repeat the above steps to add SSH public keys to the remaining GitHub accounts.

Step 4: Create a Config File and Make Host Entries

To seamlessly switch between different GitHub accounts, we'll create a configuration file and define host entries. This allows Git to recognize which SSH key to use for each repository based on the configured host.

Follow these steps to create the config file and make host entries:

  1. Open your terminal and navigate to the SSH directory by running the following command:

    console
    cd ~/.ssh
  2. Create the config file by executing the command:

    console
    touch config
  3. Open the config file using a text editor of your choice:

    console
    nano config

    Replace touch with the command for your preferred text editor, such as vim or code.

  4. Inside the config file, define host entries for each GitHub account using the following format:

    console
    # wisecodeteam account Host wisecodeteam HostName github.com User git IdentityFile ~/.ssh/wisecodeteam # dminhvu account Host dminhvu HostName github.com User git IdentityFile ~/.ssh/dminhvu

    Customize the host names (wisecodeteam, dminhvu, etc.), IdentityFile paths, and GitHub account aliases according to your setup.

  5. Save the config file.

Congratulations! You have successfully set up SSH keys for your multiple GitHub accounts on the same machine. You're now ready to clone repositories and authenticate with different accounts seamlessly.

Cloning GitHub Repositories

To clone GitHub repositories using the appropriate GitHub account, follow these steps:

  1. Open a terminal window.
  2. Navigate to the directory where you want to clone the repository.
  3. Copy the repository's SSH URL from GitHub.
  4. Execute the following command, replacing <github-account> with the desired GitHub account alias defined in your config file:
    console
    git clone git@wisecodeteam:username/repository.git
    Replace wisecodeteam with your GitHub username, username with the repository owner's username and repository with the name of the repository you want to clone.
    github-clone-ssh-key
    Figure: github-clone-ssh-key
  5. Repeat the above steps for other repositories, specifying the desired GitHub account alias as needed.

Conclusion

Setting up multiple GitHub accounts with SSH keys on the same machine provides a convenient and secure way to manage your repositories.

Remember, if you encounter any issues during the setup process or have questions, refer to the official GitHub documentation and reach out to the supportive community of developers.

External Resources:

Now you're equipped to efficiently manage your multiple GitHub accounts on the same machine using SSH keys. Happy coding and collaborating!

Minh Vu

Minh Vu

Software Engineer

Hi guys 👋, I'm a developer specializing in Elastic Stack and Next.js. My blog shares practical tutorials and insights based on 3+ years of hands-on experience. Open to freelance opportunities — let's get in touch!

Comments

Be the first to comment!

Leave a Comment

Receive Latest Updates 📬

Get every new post, special offers, and more via email. No fee required.