<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Valentin Berlin</title>
    <description>The latest articles on DEV Community by Valentin Berlin (@valenber).</description>
    <link>https://dev.to/valenber</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F124555%2F642ae21b-d715-49c6-afea-c881bd4c0ae8.jpeg</url>
      <title>DEV Community: Valentin Berlin</title>
      <link>https://dev.to/valenber</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/valenber"/>
    <language>en</language>
    <item>
      <title>Using multiple SSH-keys with git</title>
      <dc:creator>Valentin Berlin</dc:creator>
      <pubDate>Sun, 30 Jun 2019 20:24:33 +0000</pubDate>
      <link>https://dev.to/valenber/using-multiple-ssh-keys-with-git-15h1</link>
      <guid>https://dev.to/valenber/using-multiple-ssh-keys-with-git-15h1</guid>
      <description>&lt;p&gt;Recently I found myself having to use two SSH keys on my machine to access code repositories hosted on Bitbucket under two different accounts, personal and work. In the end it was not very complicated, but it took me a while to figure out, so I decided to write everything down. :)&lt;/p&gt;

&lt;p&gt;The first thing I need is to have both SSH keys on my machine. I already had the personal one, so I generated the second with same &lt;code&gt;ssh-keygen&lt;/code&gt; terminal command. I had to give it a different file name though, to make sure my personal SSH key isn't overwritten. Once the key is created it should be added to the account that hosts the repositories (in my case Bitbucket account). &lt;/p&gt;

&lt;p&gt;The next step is to create SSH configuration inside &lt;code&gt;.ssh/config&lt;/code&gt; file. Each record in this file represents specific host (or hosts) and details a set of parameters to be used with it.&lt;br&gt;
Every time the system is establishing SSH connection it will look through this config file and if the URI it is trying to connect to matches one of the Host records, it will use the parameters for this record. It supports many parameters, but I only needed two in this case.&lt;/p&gt;

&lt;p&gt;The config is very simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;when trying to connect to &lt;code&gt;work.bitbucket.org&lt;/code&gt; connect to &lt;code&gt;bitbucket.org&lt;/code&gt; instead and use credentials from &lt;code&gt;id_rsa_work&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;in all other cases just use credentials from &lt;code&gt;id_rsa&lt;/code&gt; file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Host work.bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa_work

Host *
  IdentityFile ~/.ssh/id_rsa

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the logical question here is where this &lt;code&gt;work&lt;/code&gt; subdomain of &lt;code&gt;bitbucket.org&lt;/code&gt; is coming from? When I try to &lt;code&gt;git pull&lt;/code&gt; from either personal or work repos it goes to bitbucket.org. And the answer is that we can just fake it. Rather than use the real URL of the remote repository we can modify it.&lt;/p&gt;

&lt;p&gt;All the parameters of a git repository are stored inside the &lt;code&gt;.git/config&lt;/code&gt; file. We can print the repo's remote URL by executing &lt;code&gt;git remote -v&lt;/code&gt; in the terminal. And to modify it we can either edit the config file directly or execute &lt;code&gt;git remote set-url origin [new_url]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So to make the request of a particular repo to match our SSH configuration, we just need to prefix &lt;code&gt;bitbucket.org&lt;/code&gt; in the repo's URL with &lt;code&gt;work.&lt;/code&gt;, e.g. &lt;code&gt;git@work.bitbucket.org:valenber/myrepo.git&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This way when updating the repo (making pull or push) the system will try to establish SSH connection with &lt;code&gt;work.bitbucket.org&lt;/code&gt;, it doesn't exist, but this request will never get further than our SSH config file, where it will get redirected to &lt;code&gt;bitbucket.org&lt;/code&gt; and use the correct SSH key.&lt;/p&gt;

</description>
      <category>notetoself</category>
      <category>ssh</category>
      <category>git</category>
    </item>
  </channel>
</rss>
