DEV Community

Dinesh Papineni
Dinesh Papineni

Posted on • Originally published at Medium

How to configure multiple git accounts with ssh keys on macOS

macos git github

Clean up your ssh directory and agent

  1. Navigate to ssh directory ~/.ssh
  2. Clean up the existing configuration by deleting config, known_hosts and other ssh public and private key files
  3. Also clean up old keys from the ssh agent
ssh-add -D

Create a new ssh keys using ssh keygen

Steps for creating keys for enterprise github account

  1. Generate ssh keys for enterprise github with work email address

    ssh-keygen -t rsa -b 4096 -C
    "your_work_email@work_domain.com"
    
  2. On prompt enter the key name for work account id_rsa_work

  3. On the next prompt enter a passphrase for the ssh key

  4. You should see two new files named id_rsa_work and id_rsa_work.pub in your ssh directory ~/.ssh

Repeat the steps for creating keys for personal github account

  1. Generate ssh keys for personal github with personal email address

    ssh-keygen -t rsa -b 4096 -C 
    "your_personal_email@domain.com"
    
  2. On prompt enter the key name for personal account id_rsa_personal

  3. On the next prompt enter the passphrase for the ssh key

  4. You should see two new files named id_rsa_personal and id_rsa_personal.pub in your ssh directory ~/.ssh

Add keys to ssh agent using the config

  1. Start the ssh agent in your terminal

    eval "$(ssh-agent -s)"
    
  2. Create a new config file

    touch ~/.ssh/config
    
  3. Add keys to ssh agent

    ssh-add -K id_rsa_work
    ssh-add -K id_rsa_personal
    
  4. Verify keys are stored in ssh agent

    ssh-add -l
    
  5. Add the host and ssh information to the config file. Replace the work domain name below with your enterprise github hostname. Example- github.google.com

Host work
  HostName github.<workdomain>.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_work
  ServerAliveInterval 600
  TCPKeepAlive yes
  IPQoS=throughput
Host personal
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa_personal
  ServerAliveInterval 600
  TCPKeepAlive yes
  IPQoS=throughput

Note: We have added additional config here to keep the server connection alive in order to avoid connection drops. This helps in case you are seeing following failure:

Broken pipe fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.

Add SSH keys to your github accounts

Steps for adding ssh key to enterprise github account

  1. Copy your work public ssh key using the following command

    pbcopy < id_rsa_work.pub
    
  2. In the browser navigate to your enterprise github account(https://github.workdomain.com). From the top right drop down click Settings. Now click on the SSH and GPG keys on the left navigation bar.

  3. Click New SSH Key button. Enter the name of your choice and paste the key copied in step 1 of this section. Click Add SSH key

Repeat the steps for adding ssh key for personal github account

  1. Copy your personal public ssh key using the following command

    pbcopy < id_rsa_personal.pub
    
  2. In the browser navigate to your personal github accounts(https://github.com). From the top right drop down click Settings. Now click on the SSH and GPG keys on the left navigation bar.

  3. Click New SSH Key button. Enter the name of your choice and paste the key copied in step 1 of this section. Click Add SSH key

Verify SSH keys are added to your github accounts

Steps for verifying ssh key for work account is recognized by github

  1. Enter the below command in terminal

    ssh -T work
    
  2. When prompted to add the hosts to your know_hosts file type yes

    The authenticity of host 'github.<work_domain>.com
    (<work_domain_ip)' can't be established.
    ECDSA key fingerprint is <key>.Are you sure you want to
    continue connecting (yes/no/[fingerprint])? yes

  3. You should see a confirmation that you are connected

    Warning: Permanently added 'github.<work_domain>.com,
    <work_domain_ip>' (ECDSA) to the list of known hosts.
    Hi <work_user>! You've successfully authenticated, but
    GitHub does not provide shell access.

Repeat the steps to verify ssh key for personal account is recognized by github

  1. Enter the below command in terminal

    ssh -T personal
    
  2. When prompted to add the hosts to your know_hosts file type yes

    The authenticity of host 'github.com (<domain_ip>)'
    can't be established.
    RSA key fingerprint is <key>.Are you sure you want to
    continue connecting (yes/no/[fingerprint])? yes

  3. You should see a confirmation that you are connected

    Warning: Permanently added 'github.com,<domain_ip>' (RSA)
    to the list of known hosts.
    Hi <personal_user>! You've successfully authenticated, but
    GitHub does not provide shell access.

Note: The ssh agent on mac will lose the keys on reboot. You can resolve that by simply running this command

ssh-add -A

Additional references:

  1. https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
  2. https://help.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

Top comments (1)

Collapse
 
katepapineni profile image
Kate

Thanks for the info on how to solve the github connection issue with the config settings!