19

I'm trying to migrate from Cygwin to WSL. In bash on Cygwin, I could do things like:

$ ls //servername/sharename

to access a Windows shared folder. I didn't need to mount it or anything first, it just worked. If I try the same thing in Ubuntu, I get a 'No such file or directory' error.

I work with a lot of network shares. What's the best/easiest way to handle this in Ubuntu under WSL?

4 Answers 4

18
$ sudo mkdir /mnt/share
$ sudo mount -t drvfs '\\server\share' /mnt/share

Use single quotes otherwise you will need to escape the backslashes.

WSL2 also supports CIFS.

$ sudo apt install cifs-utils
$ sudo mount -t cifs -o user={user},pass={password},vers=1.0 //server/share /mnt/share
1
  • 4
    Why vers=1.0?
    – studog
    Commented Mar 15, 2022 at 14:42
6

Updated based on @BowlOfRed's autofs answer. There are a few things that need to be solved with autofs for it to likely work for your use case.

The first answer, while perfectly correct, seems to think that you are asking how to mount CIFS/SMB drive shares, but I'm not reading your question quite that way. You seem to already know that you need to mount the shares. Your implied question is whether or not there's a way to "access the share without first mounting" (mount-on-demand) like you can in Cygwin. I understand that with a large number of different shares, as you say, this can be far more convenient.

While my original answer was essentially, "No", @BowlOfRed points out that autofs can "mount-on-demand". However, there are a few problems with autofs in my testing:

  • First, and easily solvable, under WSL you will need some way to start the autofs service. Normally under Ubuntu, this would be a Systemd unit that would start with boot. However, WSL doesn't support Systemd. Fortunately autofs comes with a SysVInit style script as well, so sudo service autofs start can be used to start it. See my Stack Overflow answer on how to do this at boot, depending on whether you are running Win 10 or 11.

  • Second, the auto.smb script that comes with autofs may require you to store your credentials for each host in a separate /etc/creds/<hostname> file. However, it sounds like you might be a in a corporate environment with a domain account. In that case, you might be able to use Kerberos or winbind to log in only once and have autofs use the cached credentials/ticket. However, I haven't been able to test this on my network, and I can't provide any guidance the configuration you'll need.

    Another option, I believe, would be to write an autofs script to use DrvFs instead of CIFS. Under WSL, DrvFs mounts act (to some degree) like Cygwin, where it uses your Windows user's credentials and doesn't require additional authentication before the mount. There's a challenge there, though, in that the automount script runs as root rather than your normal user.

    And yet another option might be modifying the current auto.smb script to use one common credentials file rather than one for each host.

  • While WSL is, by its nature, a single-user system, I'm also not a fan of storing plain-text Windows passwords on the filesystem, regardless of permissions. The DrvFs, Kerberos, or winbind options above would solve this, of course.


Side note/alternative:

Of course, if it's some finite number of shares that you want to have available at all times in WSL, then you can add them to your /etc/fstab so that they are mounted by default.

none /mnt/smb tmpfs rw,relatime 0 0
drvfs /mnt/smb/servername/sharename 9p rw,dirsync,relatime,X-mount.mkdir,aname=drvfs;path=UNC\servername/sharename;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio 0 0

That creates a tmpfs location for the SMB/CIFS mounts, then mounts the share in that directory.

You can copy the format out of your /etc/mtab after manually mounting, but I recommend adding the X-mount option so that the parent directories are automatically created in the mount point.

5

In windows (and therefore cygwin) you may have some windows/domain credentials from logging in. The UNC path mounts can use these user-level credentials to mount. WSL won't be able to use them.

But if you have a CIFS server that doesn't require a password, (or if you are comfortable with populating /etc/creds and letting any logged in user be able to use them for access), then you can use the automounter.

  • Install autofs.
  • add /cifs /etc/auto.smb --timeout=300 to /etc/auto.master
  • start/restart autofs

With that, you should be able to ls /cifs/<hostname>/<share> and see the data.

2
  • Thanks! Both my Ubuntu and Googling skills failed me on this one. That does look like what the OP is asking for. Commented Mar 15, 2022 at 13:02
  • Unfortunately after some testing this isn't quite working as (I, at least) hoped. If the OP is dealing with a number of different shares, then a different credential file has to be created for each host. That might not quite the "on-demand" need (or perhaps it will). The good news is that Windows credentials can also be used under WSL to connect to DrvFs shares without a password. So I think it's possible to set up an auto.drvfs that will work with automount. Haven't quite figured out how just yet, but I think it's possible ... Commented Mar 17, 2022 at 0:02
0

This may or may not help, but it's worth adding the following to your %USERPROFILE%\wsl.config:

[wsl2]
localhostForwarding=true

and to /etc/wsl.conf

[network]
generateHosts = false
[automount]
enabled = true
options = "metadata"
mountFsTab = false
1
  • 3
    Not really related to this question, but I'd recommend against most of those options. You typically do want WSL to generate your host file, and to mount fstab. And the localhostForward and [automount] enabled are the defaults anyway, so no need to specify them. The only one that I'd recommend is the metadata option, but be aware it's likely to slightly impair performance. It's definitely the most useful of those, though, as it allows you to "fake" Linux permissions on Windows drives. Commented Mar 15, 2022 at 14:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .