Clean up your ssh directory and agent
- Navigate to ssh directory ~/.ssh
- Clean up the existing configuration by deleting config, known_hosts and other ssh public and private key files
- 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
-
Generate ssh keys for enterprise github with work email address
ssh-keygen -t rsa -b 4096 -C "your_work_email@work_domain.com"
On prompt enter the key name for work account id_rsa_work
On the next prompt enter a passphrase for the ssh key
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
-
Generate ssh keys for personal github with personal email address
ssh-keygen -t rsa -b 4096 -C "your_personal_email@domain.com"
On prompt enter the key name for personal account id_rsa_personal
On the next prompt enter the passphrase for the ssh key
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
-
Start the ssh agent in your terminal
eval "$(ssh-agent -s)"
-
Create a new config file
touch ~/.ssh/config
-
Add keys to ssh agent
ssh-add -K id_rsa_work ssh-add -K id_rsa_personal
-
Verify keys are stored in ssh agent
ssh-add -l
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
-
Copy your work public ssh key using the following command
pbcopy < id_rsa_work.pub
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.
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
-
Copy your personal public ssh key using the following command
pbcopy < id_rsa_personal.pub
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.
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
-
Enter the below command in terminal
ssh -T work
-
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 -
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
-
Enter the below command in terminal
ssh -T personal
-
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 -
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
Top comments (1)
Thanks for the info on how to solve the github connection issue with the config settings!