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!
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.
Make sure to replace your_email@example.com with the email associated with your GitHub account, and your_github_username with your GitHub username.
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:
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:
console
eval "$(ssh-agent -s)"
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.
Figure: ssh-agent-add-ssh-key
Repeat the above step for each SSH private key you generated for your GitHub accounts.
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:
console
pbcopy < ~/.ssh/your_github_username.pub
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 or Ctrl + 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.
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:
console
cd ~/.ssh
Create the config file by executing the command:
console
touch config
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.
Inside the config file, define host entries for each GitHub account using the following format:
console
# wisecodeteam accountHost wisecodeteam HostName github.com User git IdentityFile ~/.ssh/wisecodeteam# dminhvu accountHost 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.
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.
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.
Figure: github-clone-ssh-key
Repeat the above steps for other repositories, specifying the desired GitHub account alias as needed.
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.
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
Success!
Receive Latest Updates 📬
Get every new post, special offers, and more via email. No fee required.
Comments
Be the first to comment!