How to access linux/Ubuntu files from Windows 10 WSL?

80

34

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

Posted 2016-08-08T03:35:19.640

Reputation: 9 109

4

Possible duplicate of Where is the Windows bash directory in Windows?

– phuclv – 2016-08-08T06:38:27.543

(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 – 2019-08-28T05:00:31.190

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/<USERNAME> 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 – 2020-02-17T23:22:23.270

@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 – 2020-02-18T03:34:22.540

Answers

43

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.

Richard Turner

Posted 2016-08-08T03:35:19.640

Reputation: 1 233

2Sir, 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 – 2019-03-01T18:28:31.410

Ooh, that is exciting news!! Thank you for sharing! – mpen – 2019-03-04T07:05:14.277

@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 – 2019-03-07T21:24:36.840

1I 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 – 2019-03-16T02:52:41.820

5Similar situation to Axeman, when I run explorer.exe . it opens the System32 folder. @RichardTurner Where are the manual steps? – Chris – 2019-03-20T19:33:14.187

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 – 2019-03-29T00:22:04.427

I'm getting same as @Chris opens system32 folder on 18.04.2 LTS. – Beebee – 2019-04-14T16:31:12.747

system32 not sure whats up – Muhammad Umer – 2019-05-15T01:11:41.297

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 – 2019-08-10T20:23:28.170

@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 – 2019-08-22T21:29:10.637

I have automatic Windows updates turned on. – Andrew Koster – 2019-08-22T21:44:33.793

What do you see when you execute ver from Cmd? – Richard Turner – 2019-08-30T00:05:12.413

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 – 2019-10-16T20:35:57.853

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 – 2020-01-09T13:21:16.413

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 – 2020-01-21T12:10:58.753

64

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

Posted 2016-08-08T03:35:19.640

Reputation: 824

1Thanks! Thought I was going crazy trying to follow the old instructions, I didn't even have a lxss folder. – Alex S – 2018-02-20T02:13:27.857

Glad it helped! Vote it up so that it can help others and keep an eye on the repo for updates. – drew7721 – 2018-02-20T05:38:07.490

1Is this information still true? I cannot find such folder on my Windows 10. – Bruno Finger – 2018-05-14T07:13:20.963

@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 – 2018-09-22T10:18:41.810

8

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 – 2019-03-01T18:15:07.687

@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 – 2019-03-07T17:45:55.233

1"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 – 2019-03-07T21:17:31.520

1I 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 – 2019-03-29T17:55:39.703

10

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

Posted 2016-08-08T03:35:19.640

Reputation: 201

1Even simple text files? What are the pitfalls? – mpen – 2016-08-08T17:01:54.030

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 – 2016-08-08T21:56:37.830

4Sounds 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 – 2016-08-08T22:00:59.663

9

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

Posted 2016-08-08T03:35:19.640

Reputation: 9 109

2It seems the path changed since the official FCU update. – Briefkasten – 2017-10-21T08:08:02.253

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 – 2017-10-24T06:02:59.407

2

@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 – 2017-10-28T08:15:31.367

6

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

Posted 2016-08-08T03:35:19.640

Reputation: 333

5

\\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

Posted 2016-08-08T03:35:19.640

Reputation: 202

1also woth noting, Running \\wsl$ will allow you to navigate to the correct installation & path using explorer (If you have multiple distros – Tricky – 2019-12-19T11:49:50.730

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

Posted 2016-08-08T03:35:19.640

Reputation: 49

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

Posted 2016-08-08T03:35:19.640

Reputation: 345

3Nice solution!

For current Windows 10 it's subst L: $env:LOCALAPPDATA\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs – Andreas M. Oberheim – 2018-01-22T18:05:51.817

3

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

Posted 2016-08-08T03:35:19.640

Reputation: 131

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

Posted 2016-08-08T03:35:19.640

Reputation: 21

What is the advantage of this over going to the files directly? – mpen – 2017-08-01T22:21:49.313

The files have metadata attached which may be lost when accessing the directly with applications that do not understand the metadata. – mlk – 2018-06-18T09:12:12.183