3

I have 2 wsl distros running on my pc, Ubuntu and Kali Linux. I have some programs installed and some files on my kali linux system that I want to access or edit through the Ubuntu distro. How can I do it? Are the files located somewhere in the file system that I can access through Ubuntu?

Thank You! Any help is appreciated.

InfoDaneMent
  • 131
  • 3
  • WSL controls where guest OS files are stored, and WSL is outside our support, AFAIK. However, welcome to AskUnbuntu! Please take the [tour] and read the [FAQ] to understand what we can and can't answer here. – Zeiss Ikon Mar 03 '22 at 13:46
  • 1
    Hi @ZeissIkon - Just a heads-up that WSL is on topic here as long as it relates to Ubuntu. See [this Meta answer](https://meta.askubuntu.com/a/17935/1165986) from Zanna (mod) for confirmation. Ubuntu on WSL *is* an official Canonical release, so it falls into our "on-topic" guidance. Thanks! – NotTheDr01ds Mar 03 '22 at 13:50
  • @NotTheDr01ds The core of the question, however, is where WSL stores the files for Kali, which is not supported -- right? – Zeiss Ikon Mar 03 '22 at 13:53
  • @ZeissIkon Not really - Read that meta answer closely, the last line is *"We should not close questions just because they smell Minty/Windowsy."* Also, the quote from another answer, *"unfortunately people often ask themselves "Is this (also) about something that isn't Ubuntu?" instead. That's the wrong question to ask."* – NotTheDr01ds Mar 03 '22 at 13:55
  • @NotTheDr01ds Okay. I give up. I can't read every meta post, so apparently I shouldn't have the ability to cast close votes, since what should and shouldn't be closed as unsupported has dozens of inconsistent (IMO) edge cases. Even referring new users to the tour and FAQ won't help, because they don't cover all those edge cases. – Zeiss Ikon Mar 03 '22 at 14:01
  • @ZeissIkon No worries - You did qualify your comment with "AFAIK", which is better than a lot of folks have done :-). And no, we can't all read every Meta post. But that's why we still require 5 close votes for it to take effect. Those that are truly off-topic do get closed quickly, but those that are corner cases usually get "caught" before enough close votes are cast. And if it comes to it, the Mods are really good about re-opening questions that were wrongly closed here. – NotTheDr01ds Mar 03 '22 at 14:04

2 Answers2

7

It's a bit odd:

Accessing WSL files from Windows is easy and built-in to WSL -- Just use the \\wsl$\<distro> drive share.

Accessing Windows files from WSL is also easy -- Just use the /mnt/<drive_letter> mount points.

Accessing files in one WSL instance from another isn't "built in", but can be accomplished through the use of bind mounts in each distro to the shared /mnt/wsl tmpfs mount.

Just execute the following command in both Ubuntu and Kali:

echo "/ /mnt/wsl/instances/$WSL_DISTRO_NAME none defaults,bind,X-mount.mkdir 0 0" | sudo tee -a /etc/fstab

Then exit each, issue a wsl --shutdown from PowerShell or CMD (a --terminate of each would suffice as well), and restart.

You'll find the files for each now in their respective /mnt/wsl/instances/<distroname> bind mount. 1

This works by creating an /etc/fstab entry that creates a bind mount using the distribution name in /mnt/wsl/instances/$WSL_DISTRO_NAME when the instance is started. the X-mount.mkdir allows mount to create the parent directories needed if they don't exist, similar to mkdir -p.

The /mnt/wsl directory is a tmpfs that is automatically:

  • Created by WSL when it first starts
  • Available to all WSL2 distributions

Additional notes:

  • 1 If /mnt/wsl/instances is empty or missing after doing this, you may be running a recent WSL release with a change in the mount order. Please see Option 1.5 in this Super User answer for a workaround.

  • Note that this does not work for WSL1 distributions

  • Also note that this method requires both distributions to be running. For some methods that don't require the second distro to be running, see my older methods in this Super User answer. Options 2 and 3 will both work even if the second distro isn't running.

NotTheDr01ds
  • 15,380
  • 6
  • 51
  • 79
  • It doesn't seem to be working for me, even after `wsl --shutdown` - in each instance I only see that instance. Is there any WSL option to share mounts across instances? Doesn't seem intuitive that it should happen. – Zeppe May 27 '22 at 15:11
  • @Zeppe A couple of possibilities there. First, are you sure the instances are WSL2? Double-check with `wsl.exe -l -v`. As noted in the answer, it won't work for WSL1. If they are WSL2, then something's definitely going wrong with the `/mnt/wsl` mount. Check `mount | grep wsl`. One of the results should be `none on /mnt/wsl type tmpfs ...`. If it doesn't exist (on WSL2), then you might want to post a separate question so that we can determine why. Hope that helps! – NotTheDr01ds May 27 '22 at 18:53
  • yes it's wsl2 - but the tmpfs looks like: `tmpfs on /mnt/wsl type tmpfs (rw,relatime)` - this is on both distros (ubuntu 18.04 and 22.04). Then I also got, in each, the corresponding entry: `/dev/sdc on /mnt/wsl/instances/Ubuntu-22.04 type ext4 (rw,relatime,discard,errors=remount-ro,data=ordered)` and same for 18.04 – Zeppe May 30 '22 at 09:10
  • 1
    @Zeppe Hmm - Any chance you are running a Systemd script (Genie? WSL2-Hacks? Distrod?) Or is there anything else that might be remounting that directory such as a shell startup script or `/etc/fstab`? Also, is there anything in `/etc/wsl.conf` that might change the behavior? – NotTheDr01ds May 30 '22 at 16:05
  • problem solved. The issue was not the type of the mount, but the location: in an (old, migrated from wsl1) ubuntu-18.04 I had a `wsl.conf` with `root=/`, which meant mountpoints were on `/` and this meant that: 1. the bind mount defined in 18.04 was not seen in the ubuntu-22.04 image, and that the ubuntu-22.04 image was mounted on `/` in ubuntu.18-04. Pretty trivial to see once I ran `findmnt` on the older image :) an curiously, somewhat related to @jose-sa answer. Thanks for your help! – Zeppe May 31 '22 at 09:15
2

Adding to @NotTheDr01d's answer, I need to take into consideration that a different automount root might be defined in /etc/wsl.conf. In my case, I set it to /, so /mnt/c is actually /c, and /mnt/wsl is /wsl.

To make the method @NotTheDr01d's mentions compatible with any custom automount point, you can use the following:

# determine mountpoint
MNT_WSL=$(awk '/\/wsl tmpfs/{print $2}' /proc/mounts)

# config fstab
grep "${MNT_WSL}/${WSL_DISTRO_NAME}" /etc/fstab || echo "/ ${MNT_WSL}/${WSL_DISTRO_NAME} none defaults,bind,X-mount.mkdir 0 0" | sudo tee -a /etc/fstab

# mount for current instance
sudo mount ${MNT_WSL}/${WSL_DISTRO_NAME}
NotTheDr01ds
  • 15,380
  • 6
  • 51
  • 79
Jose Sa
  • 21
  • 2
  • Agreed - My solution assumes the default `/mnt` configuration, so it's nice to have another option for those who need a different automount point. Thanks! – NotTheDr01ds May 13 '22 at 12:55
  • Both answers do not work when mounting Ubuntu2204 from Ubuntu2004 :( It mounts itself (2004) under the name/mount point of 2204 – Anton Nov 15 '22 at 13:42
  • Maybe your distro is not WSL version 2 ? Checking locally I see: $ grep VERSION_ID /wsl/*/etc/os-release /wsl/u20.04/etc/os-release:VERSION_ID="20.04" /wsl/u21.04/etc/os-release:VERSION_ID="21.04" /wsl/u22.04/etc/os-release:VERSION_ID="22.04" – Jose Sa Nov 15 '22 at 17:27