5

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.

Dadá
  • 151

2 Answers2

9

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
  • 22,082
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
  • 22,082
Jose Sa
  • 21