Managing multiple GitHub accounts (e.g., work, personal, or client projects) on a single macOS machine can lead to SSH key conflicts. Using separate SSH keys for each account ensures seamless authentication and better organization. In this guide, you’ll learn how to set up and manage multiple SSH keys for GitHub on macOS.
Why Use Multiple SSH Keys for GitHub?
- Avoid Conflicts: Prevent authentication errors when switching between accounts.
- Enhanced Security: Isolate work and personal projects.
- Organization: Easily manage contributions to different repositories.
Prerequisites
- macOS installed (any modern version).
- Basic familiarity with Terminal and GitHub.
- Two or more GitHub accounts.
Step 1: Generate SSH Keys for Each GitHub Account
1. Open Terminal and navigate to your SSH directory:
cd ~/.ssh2. Create a new SSH key for your first account (e.g., work
ssh-keygen -t ed25519 -C "[email protected]"- When prompted, name the key:
id_ed25519_work. - Add a passphrase for security (optional).
3. Repeat for your second account (e.g., personal):
ssh-keygen -t ed25519 -C "[email protected]"Name this key: id_ed25519_personal.
Step 2: Add SSH Keys to the SSH Agent
The SSH agent manages your keys in the background.
1. Start the SSH agent:
eval "$(ssh-agent -s)"2. Add your keys to the agent and macOS Keychain (saves passphrases):
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personalStep 3: Configure the SSH Config File
Create/update ~/.ssh/config to route traffic to the correct keys.
1. Open the config file in a text editor:
nano ~/.ssh/config2. Add the following entries (adjust HostName, User, and IdentityFile):
# Work GitHub
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
# Personal GitHub
Host github.com-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes3. Save and exit (Ctrl + X, then Y).
Leave a Reply