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.