178

This question answers how to access Windows files from Ubuntu Bash, but how can I do the opposite?

Specifically, I need to access my SSH key from Windows which is located at /home/mark/.ssh/id_rsa under Bash.

mpen
  • 11,884
  • 16
  • 55
  • 68
  • 4
    Possible duplicate of [Where is the Windows bash directory in Windows?](http://superuser.com/questions/1110433/where-is-the-windows-bash-directory-in-windows) – phuclv Aug 08 '16 at 06:38
  • (I have suggested to close phuclv's linked question as dupe in reverse-chronological order as this question is both more general and has far better and more up-to-date answers.) – Bob Aug 28 '19 at 05:00
  • Perhaps it's obvious to some, but in reality you really don't need to have Win access to the linux files. As folks clearly state below, doing so is **Dangerous**. Instead, you can do most everything you need from the bash shell. For example, to copy your id_rsa file to you windows section, you can do that from bash via /mnt/c/user/ yadda yadda. I made a symbolic link to my Win Desktop in my Bash home and I can move, delete, etc from within bash. – mikemtnbikes Feb 17 '20 at 23:22
  • 4
    @mikemtnbikes Copying it won't keep it in sync. A symbolic link between windows and linux? Does that even work? If it does, that sounds equally dangerous. I only intended to read the files, but regardless, we have `\\wsl$` now which is perfectly safe. – mpen Feb 18 '20 at 03:34
  • @mpen my limited understanding is that you can mess things up using Windows Explorer (or any non-ubuntu shell program) to access WSL, but not the other way around (That is, I **believe** using ubuntu to access WSL files or std win files shoudl be fine since the shell should be WSL 'aware'). Thus, I **expect** you should be able to make a symbolic link within the shell from the WSL to non-WSL directories and vice versa. (Please note the caveats and let me know if you try making links and, if so, what the outcome is.) – mikemtnbikes Mar 03 '20 at 09:34
  • 1
    posting it here for visibility as I was misled by nearly all examples below. To read WSL ssh config or other useful file from your VS Code settings, you must use forward slashes: `//wsl$//home//.ssh/config`. Also, the distro name needs a version number, not just "Ubuntu" or "Debian". Type `wsl -l` in `cmd` to confirm what you've got. – Fons MA Jun 03 '21 at 01:58
  • Only `wsl.localhost` works for me (i.e. `\\wsl.localhost\Debian12`), just `$wsl` doesn't. (Forward or backward slashes are both fine.) – Sz. Aug 23 '23 at 19:50

14 Answers14

96

PM for Windows Command-Line here:

Updated October 2019: Updating the response below to reflect the newly added ability to directly access distros' Linux files via the newly integrated P9 server in Win10 1903 (and later).

IMPORTANT: Spelunking through the Windows filesystem to access Linux files has and will continue to be unsupported and STRONGLY recommended against! To understand why, please read this post

So how does one access Linux files using Windows tools (e.g. notepad, VS/VScode, etc.)? Previously, you couldn't, but starting in Windows 10 1903 we (finally!) expose your distros' filesystems to Windows via a P9 fileserver. We've also published an in-depth video discussing how this works! You can also read a summary of this new feature in this blog post

enter image description here

Look forward to hearing how you get on with this feature. If you find any problems, please file issues on the WSL GitHub repo here: https://github.com/Microsoft/wsl.

AJM
  • 222
  • 1
  • 11
Richard Turner
  • 1,919
  • 15
  • 15
  • 2
    Sir, Is this 9P file server feature stable for normal users? If not then it may be better to add a warning about using unstable insider builds. Many users may not familiar with it. One interesting fact though, your answer contains blogs from 2016 and 2019, ~3 yr ;) – Biswapriyo Mar 01 '19 at 18:28
  • Ooh, that is exciting news!! Thank you for sharing! – mpen Mar 04 '19 at 07:05
  • 1
    @biswaprio.it is made VERY clear in the manual steps one has to complete to join the Insider program that these releases are literally weekly drops of the next version of Windows as it's being built. And yes, the 9P server will be stable for general users in the mainstream OS release it arrives within. And yes, the fact that it's taken us this long to build & start delivering a decent solution here should give you some idea about how tricky it wss for our small team to prioritize and to engineer. – Richard Turner Mar 07 '19 at 21:24
  • 3
    I have been trying out this command from Ubuntu with every update of my system since I first heard this was released. My explorer always goes to my Documents folder. And I have not yet found out any *steps* to "enable" this feature. – Axeman Mar 16 '19 at 02:52
  • 7
    Similar situation to Axeman, when I run `explorer.exe .` it opens the System32 folder. @RichardTurner Where are the manual steps? – Chris Mar 20 '19 at 19:33
  • 1
    It's been working great for me. I didn't change any settings. Goes into some `\\wsl$` folder, just like the article says. I think I'm on slow-track Insider edition though. – mpen Mar 29 '19 at 00:22
  • I'm getting same as @Chris opens system32 folder on 18.04.2 LTS. – Beebee Apr 14 '19 at 16:31
  • system32 not sure whats up – Muhammad Umer May 15 '19 at 01:11
  • Why is this nonsense an accepted answer? You have to dig through links to even find the `explorer.exe .` command which, as others have mentioned here in the comments, DOESN'T WORK. It opens a random folder in C:/Windows. – Andrew Koster Aug 10 '19 at 20:23
  • 1
    @AndrewKoster - please note that I pointed out above that you'll need **'Win10 1903 (and later)'** to get the newest Explorer integration with WSL distros. – Richard Turner Aug 22 '19 at 21:29
  • I have automatic Windows updates turned on. – Andrew Koster Aug 22 '19 at 21:44
  • 1
    What do you see when you execute `ver` from Cmd? – Richard Turner Aug 30 '19 at 00:05
  • 1
    This page was very helpful: https://devblogs.microsoft.com/commandline/whats-new-for-wsl-in-windows-10-version-1903/ I had to cd \\wsl$\Ubuntu and then I could use powershell to navigate my WSL files – Daniel S. Sterling Oct 16 '19 at 20:35
  • Can we use Windows Text Editors to edit the text files in this way? Or should all text files be edited in the Vim inside the Bash shell? – CMCDragonkai Jan 09 '20 at 13:21
  • I have been using WSL for a long time and basicall the only thing that was required when changing a file directly was to refresh the explorer with F5 in Windows and start a new shell in Linux, no big deal. – runlevel0 Jan 21 '20 at 12:10
  • There're problem with WSL2 for now. https://github.com/microsoft/WSL/issues/5718#issuecomment-671035212 I run into it and can't use it freely on my computer – Benyamin Limanto Feb 19 '21 at 14:09
  • Benjamin - please follow the issue in GitHub - it's not easy to track issues via comments to SO questions/answers – Richard Turner Feb 26 '21 at 19:15
87

The location was actually moved in the latest release to :

C:\Users\%USERNAME%\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\

See this GitHub issue from Microsoft/WSL #2578

As mentionned in the Issue above and the comments below, don't mess with these files from the windows os.

https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/

drew7721
  • 1,102
  • 1
  • 9
  • 9
  • 1
    Thanks! Thought I was going crazy trying to follow the old instructions, I didn't even have a lxss folder. – Alex S Feb 20 '18 at 02:13
  • Glad it helped! Vote it up so that it can help others and keep an eye on the repo for updates. – drew7721 Feb 20 '18 at 05:38
  • 1
    Is this information still true? I cannot find such folder on my Windows 10. – Bruno Finger May 14 '18 at 07:13
  • @BrunoFinger This still works on version 1803 (Build 17134.285) for me. Do you by any chance have a specific version of Ubuntu (16.04, 18.04) installed? – Claus Conrad Sep 22 '18 at 10:18
  • 13
    WE STRONGLY RECOMMEND THAT YOU DO NOT SPELUNK INTO THE DISTRO ROOT FOLDERS FROM WINDOWS. IF YOU DO, DATA LOSS AND/OR CORRUPTION IS HIGHLY LIKELY: Please read this post for more details: https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/ – Richard Turner Mar 01 '19 at 18:15
  • 1
    @RichardTurner stuck in caps lock? the blog post is mentioned multiple times in the GitHub issue that is the source of the answer and linked above. thx for the info, but I doubt anyone would try to manipulate or change files that way. – drew7721 Mar 07 '19 at 17:45
  • 2
    "I doubt anyone would try to manipulate or change files that way". Why do you think I shouted the advice above? We hear from people multiple times a week cool have ignored or not read this advice and end up corrupting the files in their root folders. Heck, there are tools that replace your (beta)distros files that often cause this too. – Richard Turner Mar 07 '19 at 21:17
  • 2
    I browsed to a folder I had created via Ubuntu WSL in Explorer and as a result it irreversibly broke permissions...so yeah, I would not recommend doing this! – SamAndrew81 Mar 29 '19 at 17:55
  • For me the folder was c:\Users\%USERNAME\AppData\Local\Packages\\*openSUSELeap15*\ where * stands for some (seemingly meaningless) alphanumeric sequence. – jciloa Dec 02 '20 at 15:07
78
\\wsl$\Ubuntu\home\user\whatever 

In the explorer or the Run widget (Cmd+R). Works like a normal network share and it's safe to manipulate files.

You can also map it to a drive or folder, just as any other network share.

Note: This is a new feature implemented in Windows 10 build 18342

runlevel0
  • 942
  • 6
  • 10
  • 16
    also woth noting, Running `\\wsl$` will allow you to navigate to the correct installation & path using explorer (If you have multiple distros – Tricky Dec 19 '19 at 11:49
  • 3
    \\wsl$\Ubuntu-20.04 – jrsalunga Jul 03 '20 at 10:17
  • 3
    when I entered `\\wsl$` in explorer, it did not show anything. In cmd I used use `wsl -l` to see a list of distros and use the exact name e.g. `\\wsl$\Ubuntu-18.04`. Once I have opened this location, also `\\wsl$` starts to show it... – TmTron Jul 17 '20 at 13:38
  • 9
    This answer should be marked as the 'official' answer as using the \\wsl$ share is the only route to safely edit WSL Linux files from Windows – cyberspy Oct 27 '20 at 20:44
  • https://github.com/microsoft/WSL/issues/5718#issuecomment-671035212 there're error now. Please upvote this problem so M$ people could notice it – Benyamin Limanto Feb 19 '21 at 14:11
  • 1
    In VSCode I needed to enter path in ProjectManager extension, I had to use forward slashes like this: `//wsl$/Ubuntu-20.04/home//` – Daniel Katz May 17 '21 at 22:11
11

Yes but is not recommended to manipulate that folder from the windows explorer. If you want to copy, move, edit or erase files from the lxss folder you need to do it inside bash with command line tools. Only files that are on /mnt/* are really manipulable from the windows explorer.

onoma
  • 251
  • 1
  • 5
  • 1
    Even simple text files? What are the pitfalls? – mpen Aug 08 '16 at 17:01
  • 3
    Every file that resides on a Volfs folder (like /home) has Extended Attributes that store the Linux permissions of that file. If you edit that file on a windows editor then those attributes are lost and file will disappear from bash. You can read more here: https://blogs.msdn.microsoft.com/wsl/2016/06/15/wsl-file-system-support/ – onoma Aug 08 '16 at 21:56
  • 4
    Sounds like that ought to be a bug. Windows either shouldn't give us access to those files, or give us read-only access, or they should intercept the write calls to the linux files and just not modify the attributes. Thanks for the tip. I only wanted to read the files, so hopefully that's not a problem. – mpen Aug 08 '16 at 22:00
8

Found it by searching my entire C drive. Files are here:

C:\Users\<username>\AppData\Local\lxss

e.g., my SSH key is here:

C:\Users\Mark\AppData\Local\lxss\home\mark\.ssh\id_rsa
mpen
  • 11,884
  • 16
  • 55
  • 68
  • 2
    It seems the path changed since the official FCU update. – Briefkasten Oct 21 '17 at 08:08
  • 1
    @Briefkasten I just updated to FCU and my files are still there. Created a new file under Bash just to make sure. Did you upgrade WSL or something? – mpen Oct 24 '17 at 06:02
  • 3
    @John D The WSL is located in the Package Folder where the App will be installed. For me it is: C:/Users/{username}/AppData/Local/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/rootfs/ Cont. to https://github.com/Microsoft/WSL/issues/402#issuecomment-321853125 – Briefkasten Oct 28 '17 at 08:15
7

In powershell, use

cd ${env:appdata}\..\local\packages\canonical*\localstate\rootfs

then

ls

returns the same list of folders as

ls / 

within bash on WSL.

masterxilo
  • 498
  • 7
  • 7
  • `cd ${env:appdata}\..\local\packages\canonical*\localstate\rootfs` does not work for the `debian app`. How can I access debian files in powershell under the "wsl roof"? – Timo Dec 06 '21 at 19:35
6

If you open Windows Explorer e type the path \\wsl$ it will list all your installed Linux distributions.

neves
  • 469
  • 2
  • 6
  • 12
  • 2
    This information was already suggested in October 2019 – Ramhound Jun 18 '20 at 03:27
  • @Ramhound, the other post takes you to a specific point in an specific filesystem. My explanation is simpler, less confusing and will make the user choose the correct docker container. – neves Mar 13 '21 at 21:42
  • does not matter when this info was mentioned but it is the most quickest zero configuration method to access the files in WSL, no third party tools needed, the location comes online when bash starts, – Zain Ul Abidin Mar 24 '21 at 12:06
4

As has been mentioned above by [onoma] files in WSL directories have attributes that would be gone if one manipulates it using explorer or text editor run under Windows system. The solution could be to start ssh-server in WSL (there might be a reinstall necessary) listening on localhost and then mounting WSL filesystem as a drive using, for example, win-sshfs or you can just use Bitvise SSH client to connect over ssh and manipulate files by sftp window. The topic has been partially discussed here: How can I SSH into “Bash on Ubuntu on Windows 10”?

Shamar
  • 149
  • 1
4

I finally made a way to open explorer (and vscode) with actual correct folder context from inside WSL: https://github.com/andymule/wslwin

For example, after you install this, in linux (WSL) you would just cd /home/mark/.ssh/ and type explorer, and it would open windows explorer at that location, whatever it is.

EDIT: this feature is being officially supported in WSL now, and you should no longer use my scripts

andymule
  • 141
  • 4
3

subst L: $env:LOCALAPPDATA\lxss (for powershell)

or subst L: %LocalAppData%\lxss (from cmd)

This puts your Linux-subsystem filesystem-root / on your L: drive.

You could also map just your home directory, or just whack %LocalAppData%\lxss in an explorer window. Just don't try browsing L:\mnt\c or your brain might explode.

Cheezmeister
  • 877
  • 1
  • 8
  • 12
  • 3
    Nice solution! For current Windows 10 it's subst L: $env:LOCALAPPDATA\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs – Andreas M. Oberheim Jan 22 '18 at 18:05
3

This worked for me using Powershell

cd  Microsoft.PowerShell.Core\FileSystem::\\wsl$\Ubuntu-20.04\home\username
adammendoza
  • 153
  • 4
3

I just discovered you can run powershell.exe from linux and your linux console becomes a PowerShell console at the current directory. This is the best of both worlds as you never have to leave your linux console... which is where I spend most of my time. You can then type exit to get back into linux world.

Ryan Wheale
  • 161
  • 4
2

Im on Windows 10 Creators Update. I use SFTP NetDrive to mount the WSL filesystem into windows as as a network drive.

There are some Window sshFS ports that'll achieve the same thing.

You'll need to start the ssh daemon via "sudo service ssh start"

mliang2
  • 21
  • 1
  • What is the advantage of this over going to the files directly? – mpen Aug 01 '17 at 22:21
  • 1
    The files have metadata attached which may be lost when accessing the directly with applications that do not understand the metadata. – mlk Jun 18 '18 at 09:12
1

From RUN (WindowsKey + R) you can type \\wsl$ and it will give you all your wsl installations.
This applies for WSL2

Philipos D.
  • 111
  • 4