user5914341
user5914341

Reputation:

Multiple SSH Keys on the same device

I registered a personal SSH key months ago and everything worked fine. Now I'm working for a company. They created their own GitHub account and I have started a new repository.

I know I have to add another SSH key, which I did.

This is the content of the ~/.ssh/config file.

Host github.com
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

Host github-companyname
    HostName github.com
    User git
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_companyname

I also did ssh <keyname> and I am authenticated.

After that I executed the following commands.

git init
git add 
git remote add origin <repo>

It works all fine, until I run git push -u origin master.

I get this error.

ERROR: Repository not found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.

I don't understand. Everything seems to be set up correctly.

Why am I getting that error message?

If that makes any difference, I am using OSX Sierra 10.12.4.

Upvotes: 4

Views: 5757

Answers (1)

Indrek Ots
Indrek Ots

Reputation: 3911

Since my comment resolved OPs issue, I'm writing this as an answer.

The problem seems to be in the fact that you have multiple ssh keys for the same host. In your .ssh/config you have configured 2 hosts - github.com and github-companyname. In your company repository, you need to change the remote url in .git/config from [email protected]:... to git@github-companyname:.... Then ssh will use the correct key and you should have no problems with authentication.

For further reading:

When you need to clone an existing repository with your company key, you can apply the same approach.

git clone git@github-companyname:companyname/repositoryname.git

Notice that instead of github.com, the command uses github-companyname.

Upvotes: 6

Related Questions