{"id":5164,"date":"2019-10-22T10:17:47","date_gmt":"2019-10-22T17:17:47","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/commandline\/?p=5164"},"modified":"2019-10-22T10:17:47","modified_gmt":"2019-10-22T17:17:47","slug":"sharing-ssh-keys-between-windows-and-wsl-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/commandline\/sharing-ssh-keys-between-windows-and-wsl-2\/","title":{"rendered":"Sharing SSH keys between Windows and WSL 2"},"content":{"rendered":"<p>I recently wrote an article for <a href=\"https:\/\/www.smashingmagazine.com\/2019\/09\/moving-javascript-development-bash-windows\/\">Smashing Magazine<\/a> that covers how I set up my personal development environment to use the <a href=\"https:\/\/docs.microsoft.com\/windows\/wsl\/wsl2-install?WT.mc_id=personal-docs-buhollan\" rel=\"noopener noreferrer\" target=\"_blank\">Windows Subsystem for Linux version 2<\/a> (WSL 2). One of the things that I cover in that article is how to get SSH setup in WSL with Github. I mention in that section that you can share SSH keys between Windows and WSL, but I never showed exactly how to do it. There\u2019s a good reason for that.<\/p>\n<p>I couldn\u2019t figure it out.<\/p>\n<p>An SSH key is specific to a machine. Your machine. The problem is that the Linux distro you install in WSL see\u2019s itself as it\u2019s own operating system, and it wants its own set of SSH keys. The primary value propositions of WSL is that you can have both Linux AND Windows. As a developer, this is quite compelling as you can develop <strong>for<\/strong> both platforms <strong>on<\/strong> both platforms but with only one machine and without having to constantly switch OS&#8217;s. This means that you would need a set of SSH keys for Linux and one for Windows, ultimately treating your machine as 2 machines instead of one.<\/p>\n<p>A better solution would be to share the same set of SSH keys between Windows and WSL so that you have one set of keys for one machine.<\/p>\n<h2>Setup SSH on Windows first<\/h2>\n<p>My recommendation is that you set up SSH on the Windows side first. Follow the instructions over on <a href=\"https:\/\/help.github.com\/en\/articles\/adding-a-new-ssh-key-to-your-github-account\">Github&#8217;s documentation<\/a> to do this. It will walk you through generating the key, starting the agent and then adding your key to Github.<\/p>\n<h2>Copy keys to WSL<\/h2>\n<p>To use this same set of keys in WSL, you first need to copy them over. The keys are almost always located at <code>c:\\Users\\&lt;username&gt;\\.ssh<\/code>. That\u2019s a folder. You will need to copy that entire folder from Windows, to WSL.<\/p>\n<p>Open a terminal instance attached to WSL. Might I recommend the <a href=\"https:\/\/www.microsoft.com\/en-us\/p\/windows-terminal-preview\/9n0dx20hk701?WT.mc_id=personal-docs-buhollan\" rel=\"noopener noreferrer\" target=\"_blank\">free Windows Terminal<\/a>? You\u2019ll love it. I promise. If you\u2019re not 100% satisfied, you can get your money back &#8211; no questions asked.<\/p>\n<p>Execute the following command to copy the keys from Windows to WSL.<\/p>\n<p><code>cp -r \/mnt\/c\/Users\/&lt;username&gt;\/.ssh ~\/.ssh<\/code><\/p>\n<blockquote>\n<p>Note that if you installed Github desktop, you do not need to setup SSH keys on the Windows side, and the .ssh folder will not be present. Make sure you follow the Github instructions above to generate the SSH keys on Windows.<\/p>\n<\/blockquote>\n<h2>Fix permissions<\/h2>\n<p>If you were to try and push something to Github from WSL, it will warn you that it doesn\u2019t recognize the remote host and it will ask you if you want to connect. Type \u201cyes\u201d. Then you will get an error that looks something like this\u2026<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569766796018_image.png\" alt=\"terminal showing unprotected private key\" width=\"1012\" height=\"453\" class=\"alignnone size-full wp-image-5165\" srcset=\"https:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569766796018_image.png 1012w, https:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569766796018_image-300x134.png 300w, https:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569766796018_image-768x344.png 768w\" sizes=\"(max-width: 1012px) 100vw, 1012px\" \/><\/p>\n<p>Like nearly everything that goes wrong on Linux, this is a permissions issue. You need to adjust the permissions on the key file to get this working. To do that, run the following command from WSL.<\/p>\n<p><code>chmod 600 ~\/.ssh\/id_rsa<\/code><\/p>\n<p>What this does is set Read\/Write access for the owner, and no access for anyone else. That means that nobody but you can see this key. The way god intended.<\/p>\n<p>Now try and push to Github\u2026<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569767047098_image.png\" alt=\"terminal showing RSA host key message\" width=\"1009\" height=\"292\" class=\"alignnone size-full wp-image-5166\" srcset=\"https:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569767047098_image.png 1009w, https:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569767047098_image-300x87.png 300w, https:\/\/devblogs.microsoft.com\/commandline\/wp-content\/uploads\/sites\/33\/2019\/10\/s_D3B2B20B8FE7B8446120837B2DF158A420079A1E10169DB4E803F409BC26C097_1569767047098_image-768x222.png 768w\" sizes=\"(max-width: 1009px) 100vw, 1009px\" \/><\/p>\n<p>Success!<\/p>\n<p>Now, you will be asked to enter your passphrase every single time you try and push to Github. That\u2019s going to get old in a hurry. This is because the ssh agent isn\u2019t running on the Linux side. To get the agent running when WSL starts, first install <a href=\"https:\/\/www.funtoo.org\/Keychain\" rel=\"noopener noreferrer\" target=\"_blank\">keychain<\/a>.<\/p>\n<p><code>sudo apt install keychain<\/code><\/p>\n<p>Then add the following line to your <code>~\/.bashrc<\/code> file\u2026<\/p>\n<p><code>eval ``keychain --eval --agents ssh id_rsa<\/code><\/p>\n<p>Each time you reboot, you\u2019ll have to enter your passphrase. But you only have to do it one time until you reboot or terminate WSL.<\/p>\n<blockquote>\n<p>There are other ways to auto-start the ssh-agent in WSL. There are instructions in <a href=\"https:\/\/www.smashingmagazine.com\/2019\/09\/moving-javascript-development-bash-windows\/\" rel=\"noopener noreferrer\" target=\"_blank\">this article<\/a> that show how to do with with Zsh.<\/p>\n<\/blockquote>\n<h2>Sharing is caring<\/h2>\n<p>While you can set up SSH keys on both the Linux and the Windows side, it feels a bit redundant. It also feels a bit like I\u2019m not utilizing the full potential of WSL, which is that it lets me move between the two operating systems as if they were one. Sharing the same set of keys feels a bit more like I\u2019m on one system instead of two.<\/p>\n<p>I care because I\u2019m always looking for the one \u201cright way\u201d to do things. The reality is that there is no one \u201cright way\u201d to do anything. You can setup SSH keys on both WSL and Windows, or you can set them up just in Windows and share them with WSL. It\u2019s your world. The ssh keys just live in it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently wrote an article for Smashing Magazine that covers how I set up my personal development environment to use the Windows Subsystem for Linux version 2 (WSL 2). One of the things that I cover in that article is how to get SSH setup in WSL with Github. I mention in that section that [&hellip;]<\/p>\n","protected":false},"author":8550,"featured_media":5176,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-5164","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-on-ubuntu-on-windows"],"acf":[],"blog_post_summary":"<p>I recently wrote an article for Smashing Magazine that covers how I set up my personal development environment to use the Windows Subsystem for Linux version 2 (WSL 2). One of the things that I cover in that article is how to get SSH setup in WSL with Github. I mention in that section that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts\/5164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/users\/8550"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/comments?post=5164"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/posts\/5164\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/media\/5176"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/media?parent=5164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/categories?post=5164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/commandline\/wp-json\/wp\/v2\/tags?post=5164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}