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$\Ubuntu
2 (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.
Start by going to the Microsoft Store, search for "Alpine WSL" and install it.
After installation, select "Open" from the Store (or just run Alpine from the Start menu).
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.
su -
and enter the password you used above.
echo -e "[user]\ndefault=root" > /etc/wsl.conf
exit
twice to exit the distribution (first to exit the su
session, then to exit Alpine)
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.
Exit Ubuntu
Start PowerShell as a regular (non-Admin) user.
wsl --shutdown
wsl ~
to restart Ubuntu
Open another terminal with PowerShell (still non-Admin) and run wsl ~ -d Alpine
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 ...
ls /mnt/wsl/instances
and confirm that there's an Ubuntu
(or Ubuntu-20.04
, etc.) there.
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.
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
.
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.
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.