Score:0

Mount folder from WSL to Ubuntu over SSH with SSHFS

my flag

I am trying to mount a folder located inside WSL to my Ubuntu 20.4 machine using SSHFS but I always get the message that there is "No such file or directory." How can I do it?

The obvious way I tried to mount the directory was the following:

sshfs user@remotehost:"\\wsl$\Ubuntu-20.04" /home/user/Remote/
Will avatar
id flag
Can you edit your post to say how you’re trying to mount it please?
Adam avatar
my flag
@Will I edited my question. Thank you for the suggestion.
Score:0
vn flag

Short Answer:

Use the Windows host as an ssh jumphost to access WSL.

More Detail:

There are a few pitfalls when trying to access WSL (especially WSL2) through ssh:

First, you may have run into the fact that WSL2 runs in a virtualized environment, with a NAT'd vNIC. This means that services running in WSL2 are not directly accessible from other machines on the network.

WSL does provide localhost forwarding, which allows access from the Windows host itself to services running in WSL2 (via localhost:portnumber). But this does not extend to other machines on the network.

You can see a full Github thread on this here with the link being directly to the comment that it typically recommended to resolve this.

It involves:

  • Setting firewall rules
  • Port-forwarding from Windows to the WSL2 virtual networking interface
  • The biggest challenge here is that the address of the vNIC changes each time WSL restarts. So the script has to delete the old rules and recreate them each reboot.

But, with your updated edit, you seem to be trying something slightly different, where it appears you may be trying to access the WSL pseudo-share through Windows, perhaps via ssh on the Windows host itself.

Unfortunately, this also won't work because \\wsl$\... is itself a network filesystem (using the 9P protocol). This means that it really doesn't exist on the Windows host itself when accessed via ssh.

My preferred solution with ssh (and thus sshfs) is to make use of a ssh jumphost. This gets around the "changing vNIC address" issues and doesn't require you to run a script at every Windows boot.

To set this up:

  • Install Windows OpenSSH server on the Windows host. I use the default port 22 for the "host" ssh, but you don't necessarily have to.
  • Install and configure SSH in your WSL instance. You will need to use a different port number here than for the Windows host. For the sake of this example, we'll use port 8022. Edit your /etc/ssh/sshd_config to change the port.
  • Optional, but recommended, set up both the Windows OpenSSH and the WSL instances with your public key in ../ssh/authorized_keys (on Windows OpenSSH, this is under your %userprofile%/.ssh directory. In Linux/WSL, of course, it's in ~/.ssh.

Now, since (as mentioned earlier) the Windows host can access the WSL2 instances on localhost, you can use the Windows SSH as a jumphost to the WSL2 SSH sessions.

Assuming that:

  • Your Windows computer is named windowshost (substitute whatever it really is called)
  • You have your Windows OpenSSH on port 22
  • You have your WSL SSH server on port 8022

Then you can mount your WSL system using:

sshfs -o ssh_command='ssh -J windowshost' -p 8022 localhost:/ <mountpoint>

The WSL instance is "localhost" because this is the address from the perspective of the jumphost (the Windows host).

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.