-1

Before start I should say I have read a lot of topics (listed at the end of this post) about this issue but none is working for me or maybe I am missing something trivial in here.

At first I did clone the repository using HTTPS but then I switched to SSH following the docs from here. I did generate each of the SSH key and add them to the ssh-agent following the docs from here.

In a few lines (fake data used, ~ means my home directory) this is what I've done:

$ ssh-keygen -t rsa -b 4096 -C "first@gmail.com"
Enter a file in which to save the key (~/.ssh/id_rsa): [Press enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

$ ssh-keygen -t rsa -b 4096 -C "second@gmail.com"
Enter a file in which to save the key (~/.ssh/id_rsa_second): [Press enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

$ eval "$(ssh-agent -s)"
Agent pid 12697

$ ssh-add ~/.ssh/id_rsa
Identity added: ~/.ssh/id_rsa (~/.ssh/id_rsa)

$ ssh-add ~/.ssh/id_rsa_second
Identity added: ~/.ssh/id_rsa_second (~/.ssh/id_rsa_second)

$ ssh-add -l
4096 SHA256:gb+Gn4SqiyAP5ABUsmX6Xz11RHTSvDsWgEE5P2R2VTE ~/.ssh/id_rsa (RSA)
4096 SHA256:yxWMompayDNtYjv5y+FfJl7OpQ5Qu90kPgdXXvx6DRA ~/.ssh/id_rsa_second (RSA)

The next step was create the ~/.ssh/config file with the following content:

#first account
Host github.com-first
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa

#second account
Host github.com-second
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_second

I stopped here and give this a try by adding the SSH pub key for id_rsa_second to the repository where I want to use it (this doesn't need explanation). Next git pull:

$ git pull
Bad owner or permissions on /home/rperez/.ssh/config
fatal: Could not read from remote repository.

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

Then I tried the following:

  • Change the .git/config file to the following:

    [remote "origin"]
        url = git@github.com-second:repo/repo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    

But it didn't work and by this I mean I got exactly the same error as before.

What I am doing wrong? What I am missing?

What I have read:

Note: The title may seem confusing but it is correct because it is what I want to achieve I am just talking about one example but at the end I should be able to setup more than one account pointing to different repositories.

Ikke
  • 99,403
  • 23
  • 97
  • 120
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Possible duplicate of [ssh config for multiple bitbucket accounts - Simple example, but getting 'remote end hung up unexpectedly'](https://stackoverflow.com/questions/14409761/ssh-config-for-multiple-bitbucket-accounts-simple-example-but-getting-remote) – Ikke Jul 04 '17 at 03:10

1 Answers1

1

The reason it's not working is because of this error:

Bad owner or permissions on /home/rperez/.ssh/config

Ssh is picky (for security reasons) about the permissions of sensitive files.

man ssh says:

~/.ssh/config
        This is the per-user configuration file. The file format and configuration 
        options are described in ssh_config(5). Because of the potential for abuse,
        this file must have strict permissions: read/write for the user, and not
        writable by others.

So check the file permissions of /home/rperez/.ssh/config. They should be 0644 (-rw-r--r-).

Ikke
  • 99,403
  • 23
  • 97
  • 120