Step 1: Generate SSH Keys
-
Open your terminal.
-
Generate a new SSH key for each GitHub account. Replace
youremail@example.comwith your email associated with each GitHub account.For the first account:
ssh-keygen -t rsa -b 4096 -C "youremail1@example.com"When prompted to save the key, give it a unique name (e.g.,
id_rsa_github_personal):Enter file in which to save the key (/home/you/.ssh/id_rsa): /home/you/.ssh/id_rsa_github_personalFor the second account:
ssh-keygen -t rsa -b 4096 -C "youremail2@example.com"Save this key with another unique name (e.g.,
id_rsa_github_work):Enter file in which to save the key (/home/you/.ssh/id_rsa): /home/you/.ssh/id_rsa_github_work
Step 2: Add SSH Keys to the SSH Agent
-
Start the SSH agent:
eval "$(ssh-agent -s)"
-
Add your SSH keys:
ssh-add ~/.ssh/id_rsa_github_personal ssh-add ~/.ssh/id_rsa_github_work
Step 3: Add SSH Keys to Your GitHub Accounts
-
Copy the SSH key to your clipboard:
cat ~/.ssh/id_rsa_github_personal.pubGo to GitHub (for the first account) > Settings > SSH and GPG keys > New SSH key. Paste the key and save.
-
Repeat the process for the second account:
cat ~/.ssh/id_rsa_github_work.pubAdd this key to the second GitHub account in the same way.
Step 4: Configure SSH Config File
-
Open (or create) the SSH config file:
nano ~/.ssh/config -
Add configurations for both accounts:
# Personal account Host github.com-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_personal # Work account Host github.com-work HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_work
Step 5: Clone Repositories Using the Correct Host
When cloning repositories, use the corresponding host defined in your SSH config:
For the personal account:
git clone git@github.com-personal:username/repo.gitFor the work account:
git clone git@github.com-work:username/repo.gitConclusion
You should now be able to manage two GitHub accounts using SSH without any issues! Each account will use the appropriate SSH key when you clone or push to repositories.