{"id":495,"date":"2016-02-05T09:01:39","date_gmt":"2016-02-05T03:31:39","guid":{"rendered":"http:\/\/www.cyberaka.com\/?p=495"},"modified":"2016-02-05T09:17:58","modified_gmt":"2016-02-05T03:47:58","slug":"using-ssh-to-clone-git-repository-with-multiple-private-keys","status":"publish","type":"post","link":"https:\/\/www.cyberaka.com\/?p=495","title":{"rendered":"Using SSH to clone Git repository with multiple private keys"},"content":{"rendered":"<p>I prefer to use the multiple private keys and avoid using the same private key with multiple services. Recently I decided to switch from HTTPS based Git clone of my bitbucket repositories to SSH based Git clone. So created a new private key added them into bitbucket.org and then I expected that my git clone would work. But it didn&#8217;t.<br \/>\n<code><br \/>\ngit clone git@bitbucket.org:mybitbucketid\/mygitrepository.git<br \/>\nCloning into 'mygitrepository'...<br \/>\nconq: repository access denied.<br \/>\nfatal: Could not read from remote repository.<\/p>\n<p>Please make sure you have the correct access rights<br \/>\nand the repository exists.<br \/>\n<\/code><\/p>\n<p>I knew I had to point the git client to my private key so I created the following entry in my ~\/.ssh\/config file.<br \/>\n<code><br \/>\nHost\t\tbitbucketrepo<br \/>\nHostName\tbitbucket.org<br \/>\nIdentityFile\t~\/.ssh\/bitbucket_private_key<br \/>\nUser\t\tgit<br \/>\n<\/code><\/p>\n<p>Now I felt it would work. I tried again and it still didn&#8217;t work.<br \/>\n<code><br \/>\ngit clone bitbucketrepo:mybitbucketid\/mygitrepository.git<br \/>\nCloning into 'mygitrepository'...<br \/>\nconq: repository access denied.<br \/>\nfatal: Could not read from remote repository.<\/p>\n<p>Please make sure you have the correct access rights<br \/>\nand the repository exists.<br \/>\n<\/code><\/p>\n<p>Now I was confused as to why it was not working. On further research I found <a href=\"http:\/\/stackoverflow.com\/questions\/4565700\/specify-private-ssh-key-to-use-when-executing-shell-command-with-or-without-ruby\">this link<\/a>. Now I updated the entry in my ~\/.ssh\/config file to this.<br \/>\n<code><br \/>\nHost\t\tbitbucketrepo<br \/>\nHostName\tbitbucket.org<br \/>\nIdentityFile\t~\/.ssh\/bitbucket_private_key<br \/>\nIdentitiesOnly\tyes<br \/>\nUser\t\tgit<br \/>\n<\/code><\/p>\n<p>Now I tried again. This time it worked perfectly.<br \/>\n<code><br \/>\ngit clone bitbucketrepo:mybitbucketid\/mygitrepository.git<br \/>\nCloning into 'mygitrepository'...<br \/>\nwarning: You appear to have cloned an empty repository.<br \/>\nChecking connectivity... done.<br \/>\n<\/code><\/p>\n<p>Well the addition of &#8220;IdentitiesOnly\tyes&#8221; line in my config file did the trick. It seems that when we do an SSH connection it&#8217;s default behavior is to send the identity file matching the default filename for each protocol. So if you have a file named ~\/.ssh\/id_rsa then that will get tried before your private key which in my case was ~\/.ssh\/bitbucket_private_key. So by using the &#8220;IdentitiesOnly    yes&#8221; line I explicitly asked my ssh client to use my identity file and nothing else and it worked like a charm. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I prefer to use the multiple private keys and avoid using the same private key with multiple services. Recently I decided to switch from HTTPS based Git clone of my bitbucket repositories to SSH based Git clone. So created a new private key added them into bitbucket.org and then I expected that my git clone [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,35,24],"tags":[52,50,51],"class_list":["post-495","post","type-post","status-publish","format-standard","hentry","category-linux-os","category-mac-os","category-tips-and-tricks","tag-conq-repository-access-denied","tag-git","tag-ssh"],"_links":{"self":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts\/495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=495"}],"version-history":[{"count":1,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":496,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=\/wp\/v2\/posts\/495\/revisions\/496"}],"wp:attachment":[{"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cyberaka.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}