Score:0

Access root user directories from Windows under WSL?

cn flag

I'm running WSL2, Ubuntu 20.04, and Docker Desktop. I often need to access some directories that are root owned and not readable by my main user. It's getting pretty old to always have to:

  • Manually login to the Ubuntu console as root
  • Manually cd to the dirs I need to view files in
  • cat each file one by one

This is especially painful when having to search through a ton of directories within just a minute or two, as I'm having to do right now with a bunch of Portainer directories.

I'd like to be able to just click through each directory in Explorer or Directory Opus and look in the preview tab. However, these files/directories apparently aren't accessible when using Windows applications to examine \\wsl$\Ubuntu\path\to\root\owned\directory\.

Portainer creates its WSL folders as being owned by root. I can manually chown them as my WSL/Ubuntu user (same name: scott) and then have the ability to do the above, but that gets old fast. I'm also worried that it might other things on the Ubuntu/Portainer side down the road.

What I'd like is to add my Windows user to Ubuntu where Windows Explorer/Directory Opus/Notepad/etc. have the ability to access root user files/dirs, but with Linux as my second language ;-) I'm not real sure how that's done.

Also, when correctly entering the password into the Windows prompt, it still fails:

Network Credentials Prompt

P.S. I do know that I can login to the Ubuntu console and do explorer.exe ., but even then I get an error when accessing root dirs.

J. Scott Elblein avatar
cn flag
@Nmath "It's really not clear from your question what your end goal is". "What I'd like is to add my Windows user to Ubuntu where Windows Explorer/Directory Opus/Notepad/etc. have the ability to access root user files/dirs."
NotTheDr01ds avatar
vn flag
@Nmath The problem is that Explorer (and all Windows executables) can only access the files as the *default* WSL/Ubuntu user. This means that root-owned/protected files are *not accessible* (as mentioned by the OP in the question) in Windows Explorer, Dopus, or any other Windows app. I *may* have a workaround, but I need to test it further before posting, as I've *thought* I'd solved this in the past and been wrong (I had to delete my answer to [this Super User question](https://superuser.com/q/1743898/1210833)).
NotTheDr01ds avatar
vn flag
@Nmath I have to disagree - First of all, the OP simply wants to be able to *view* these files using the File Preview capabilities of Explorer or Dopus (although Notepad is later mentioned). That's a perfectly good reason for wanting to access root-protected files through Windows apps, IMHO. Second, though, the 9P network filesystem that WSL uses for sharing files between Ubuntu and Windows should be completely safe with POSIX filesystems -- It's included in the Linux kernel, so I would hope it's okay in this respect!
J. Scott Elblein avatar
cn flag
@NotTheDr01ds Thank you for fully understanding my question. =) @ Nmath, I do of course understand all of the security implications (Linux makes permissions/security *abundantly clear* dozens of times throughout a single day's use), but in my case for the purposes of this question, I have no intention of using any Windows apps to edit system config files. While I *may* (or may not) use them to edit non-system-related files, such as the dockerfiles for a container saved by Portainer.
Score:1
vn flag

Orthogonal answer (but much simpler than my other answer): Use a Linux application that can preview the root-owned files instead of using a Windows application. Downside -- There's just no comparison to Directory Opus, of course (IMHO)!

sudo apt install ranger
sudo ranger . # or the path you want to inspect

Note that preview of files when running as sudo is disabled by default for security reasons. You can enable it via zi. Then simply select the file you want to preview, and it will be displayed in the right-column.

I'm assuming that what you are inspecting will primarily be text-files. If the file is longer than the terminal height, you can scroll (see key-bindings) or preview the file in a full-screen pager using i.

J. Scott Elblein avatar
cn flag
While not directly answering my question, big TY for the ranger recommendation! Never heard of it before, but it definitely *is* nice and useful. If I just keep arrow-right'ing it'll load the file in nano, which is just about as good as using the `i` to preview it. It'd be extra nice if I could just arrow-left to back out of it, instead of having to use `esc`, but that's a nitpicky, minor gripe. Also, +1 just for using the word "orthogonal"; NOICE! lol
J. Scott Elblein avatar
cn flag
I haven't had a chance to go through your 1st answer yet, but will later today ;)
NotTheDr01ds avatar
vn flag
@J.ScottElblein Interestingly, I had never heard of it either! Just came across it when searching for something like "linux preview file manager" :-). And +1 back for the mention of Directory Opus ;-)
Score:0
vn flag

To summarize the root issue 1, your Ubuntu distribution is accessible via a 9P network share in Windows. The Windows 9P client, which automatically creates \\wsl$\Ubuntu2 (Windows 10) or \\wsl.localhost\Ubuntu (Windows 11) shares has to select a particular Ubuntu user in order to provide access/permissions/ownership/etc.

1 No pun intended, really, I just noticed it!

2 Or other distribution name such as, e.g. "Ubuntu-22.04"

WSL uses the default user for the distribution as the "owner" of the share. This is why you typically don't have access to root-restricted files when a Windows application is attempting to view or edit files in (e.g.) \\wsl.localhost\Ubuntu.

With that in mind (after typing the above info), a crazy, hairbrained idea came to mind. Can we use another distribution, where the default user is root, to launch a Windows executable and yet still have it access the files (still as root) in Ubuntu? The answer appears to be, "Yes", but I will say that this is a problem that I've thought I had "solved" (or at least worked-around) before, only to find I was wrong.

So I've tested this on Windows 10 with an older WSL version and Windows 11 with the latest (as of yesterday) WSL 0.70.8 and it seems to work under both. The process is, unfortunately, a bit convoluted, but if it works for you, it should be something that can be scripted or aliased. Don't be too scared by the number of steps below. There are several reasons why they are longer than they need to be:

  • I'm just pretty detailed (perhaps overly so) in how I explain things.
  • I hope that, at the end of this, you'll understand why this works (or doesn't) so that you'll be able to adjust the process (if needed) for your use-case in the future.
  • Also, most of the steps below are one-time setup and won't need to be repeated.

So to do this, you are going to need a separate distribution installed. Since this distribution only needs to be able to launch the Windows executables as root, we're looking for the smallest thing available -- That's Alpine Linux. We could make things smaller, but it would be more trouble than it's worth. You can install Alpine on WSL directly from the Microsoft Store just as you do Ubuntu.

  1. Start by going to the Microsoft Store, search for "Alpine WSL" and install it.

  2. After installation, select "Open" from the Store (or just run Alpine from the Start menu).

  3. Add a username and password when asked. For the sake of verifying a future step here, just use the same username that you have in Ubuntu.

  4. su - and enter the password you used above.

  5. echo -e "[user]\ndefault=root" > /etc/wsl.conf

  6. exit twice to exit the distribution (first to exit the su session, then to exit Alpine)

  7. Back in Ubuntu, run the command in Option 1 of this answer. If you are on Windows 11 with a recent WSL release, you may need to also use Option 1.5.

  8. Exit Ubuntu

  9. Start PowerShell as a regular (non-Admin) user.

  10. wsl --shutdown

  11. wsl ~ to restart Ubuntu

  12. Open another terminal with PowerShell (still non-Admin) and run wsl ~ -d Alpine

  13. whoami to confirm that you are running as root in Alpine by default. If not, something in the echo ... > wsl.conf step above likely went wrong and we'll need to troubleshoot. If it is running as root, continue ...

  14. ls /mnt/wsl/instances and confirm that there's an Ubuntu (or Ubuntu-20.04, etc.) there.

  15. Make sure it contains the files in your Ubuntu distribution. For instance (assuming Ubuntu is the distribution name), ls /mnt/wsl/instances/Ubuntu/home/$USER/ should show the same files that are in your Ubuntu home directory. Note that this will only work if you used the same username in both Ubuntu and Alpine.

    This works because /mnt/wsl is a mount that is created by WSL itself and shared among all of the WSL2 instances/distributions that you run. Anything you place in /mnt/wsl is:

    • available from any distribution
    • ephemeral, meaning it disappears once WSL restarts. That's not a problem because what we are placing here is actually a mount-within-a-mount back to /. And because of the /etc/fstab line, it's actually mounted every time Ubuntu starts.

    I know you are fairly new to Linux, so hopefully that make sense! It's a lot of Linux concepts (fstab, tmpfs, mounts, etc.) put together, to be sure.

  16. With this in place, exit Alpine and return to Ubuntu:

    echo "Hello" > ~/testfile.txt`
    sudo chown root:root ~/testfile.txt
    sudo chmod 600 ~/testfile.txt
    

    ... to create a file we can use to test this. Of course, I know you already have files like this from Portainer, but this gives me a path we can use reliably.

    The file should no longer be previewable in, e.g. Directory Opus. Nor can you edit it via notepad.exe testfile.txt.

  17. Let's see if we can use Alpine, which we've confirmed runs as root by default, to open and edit the file in Notepad, for starters:

    wsl.exe \~ -d Alpine notepad.exe /mnt/wsl/instances/$WSL_DISTRO_NAME/home/$USER/testfile.txt
    

    If everything's working correctly (and it is for me, on two systems), you should be able to edit and save the file in Windows Notepad, even though it is owned by root and set for 600 (read-write, owner-only) permissions.

  18. If that works, then we have a general solution that we can use, and then tweak for other use-cases. For example, try:

    # Pass the name of your Ubuntu distro into Alpine.
    # Not absolutely necessary, just a convenience.
    export WSL_USER_DISTRO=$WSL_DISTRO_NAME
    export WSLENV=$WSLENV:WSL_USER_DISTRO
    
    wsl.exe \~ -d Alpine
    # You are now in Alpine, running inside Ubuntu
    
    /path/to/dopus.exe /Go $(wslpath -w /mnt/wsl/instances/$WSL_USER_DISTRO/)
    

    You should be able to preview root-owned/protected files from Ubuntu in Directory Opus (tested and works on my end, at least).

This is fairly nested, so let me unwind a bit:

  • We ran Ubuntu in PowerShell
  • We ran wsl.exe (a Windows command) in Ubuntu, to launch ...
  • Alpine inside Ubuntu
  • In Alpine, we have access to all of Ubuntu's files (as root) via /mnt/wsl/Ubuntu (or similar). And Alpine's files are shared to Windows (via 9P) as root.
  • In Alpine, we launched Directory Opus (a Windows binary), which can now access the Ubuntu files as root because we are accessing them through Alpine.

Make sense? If not, don't worry, it barely makes sense to me! ;-).

While you could, in theory, script this or alias it, you are (very/extremely/almost certainly/99.99999%) likely to get into some quoting/escaping issues as you pass paths from WSL, to Windows, to WSL, and back to Windows.

So, if the workflow works for you, I'd just launch Alpine and use it to launch any Windows executables that you want to access as root.

NotTheDr01ds avatar
vn flag
Of course, if you run into any issues, leave me a comment. Hopefully I can unwind where you (or I) went wrong. I do hope I got everything right in the instructions on the first pass, but there's a good chance (as long and complicated as it is), that I made a mistake of some sort. Apologies in advance if so!
J. Scott Elblein avatar
cn flag
So far, so good, but I did run into a couple small hiccups; in step 16 it looks like you might've accidentally added an extra tick after `txt`? Also, if I try using `\~` I get the error "/bin/bash: ~: command not found", but removing the backslash runs it fine. Last thing, the env vars in step 17; `$WSL_DISTRO_NAME` was empty, so I had to manually replace it with `Ubuntu`, and `$USER` was `root` rather than the one I entered in step 3, I think due to step 5? So I replaced it with my actual username, and viola! The txt file opened. I'm at the point now where I'm going to try the dopus stuff. =)
J. Scott Elblein avatar
cn flag
Ahh, it was going so well, and then tried opening dopus to the path `/mnt/wsl/instances/Ubuntu/opt/docker/configs/`, it opened find, navigated in dopus to to a root portainer folder, and ... it still asked me to enter in my network credentials just like before. Then tried to use explorer like this: `wsl.exe ~ -d Alpine explorer.exe wslpath -w /mnt/wsl/instances/Ubuntu/opt/docker/configs/` and explorer just opens to the Windows Documents folder. Not sure if I'm screwing somethin up here or not, lol.
NotTheDr01ds avatar
vn flag
@J.ScottElblein Yeah, I got the same thing -- Slight tweak on that command-line, though - You would need something like `wsl.exe ~ -d Alpine explorer.exe $(wslpath -w /mnt/wsl/instances/Ubuntu/opt/docker/configs/)`. Even then, I run into the problem, though - That's where the *"quoting/escaping issues as you pass paths from WSL, to Windows, to WSL, and back to Windows"* start to bite us and why I recommended running Dopus in two steps (launch Alpine, then launch Dopus from inside Alpine). That should work more smoothly.
NotTheDr01ds avatar
vn flag
You can see the problem if you replace it with `wsl.exe ~ -d Alpine echo $(wslpath -w /mnt/wsl/instances/Ubuntu/opt/docker/configs/)`. It loses backslashes.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.