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:
-
Navigate to your home directory by running the following command:
-
Generate an SSH key pair by executing the following command:
Make sure to replace your_email@example.com
with the email associated with your GitHub account, and your_github_username
with your GitHub username.
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:
- Start the SSH agent by running the following command:
- Add your SSH private key to the agent by executing the command:
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
. - 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:
- Copy the SSH public key for the first GitHub account by executing the following command: This command copies the content of the public key to your clipboard.
- Open a web browser and navigate to GitHub. Sign in to the GitHub account you wish to associate with the SSH key.
- In the upper-right corner, click on your profile photo and select Settings from the dropdown menu.
- On the left sidebar, click on SSH and GPG keys.
- Click on the New SSH key button.
- Provide a descriptive title for the SSH key in the Title field.
- Paste the SSH public key into the Key field. To paste from your clipboard, use
Cmd + V
on macOS orCtrl + V
on Windows or Linux. - Finally, click on the Add SSH key button to save the SSH key to your GitHub account.
- 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:
-
Open your terminal and navigate to the SSH directory by running the following command:
-
Create the config file by executing the command:
-
Open the config file using a text editor of your choice:
Replace
touch
with the command for your preferred text editor, such asvim
orcode
. -
Inside the config file, define host entries for each GitHub account using the following format:
Customize the host names (
wisecodeteam
,dminhvu
, etc.),IdentityFile
paths, and GitHub account aliases according to your setup. -
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:
- Open a terminal window.
- Navigate to the directory where you want to clone the repository.
- Copy the repository's SSH URL from GitHub.
- Execute the following command, replacing
<github-account>
with the desired GitHub account alias defined in your config file: Replacewisecodeteam
with your GitHub username,username
with the repository owner's username andrepository
with the name of the repository you want to clone. - 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:
- GitHub Docs: Generating a new SSH key and adding it to the SSH agent
- GitHub Docs: Adding a new SSH key to your GitHub account
Now you're equipped to efficiently manage your multiple GitHub accounts on the same machine using SSH keys. Happy coding and collaborating!
Comments
Be the first to comment!