60

Is there a way to access removable media (e.g. thumb drives or SD cards) from within Bash on Windows?

Bash on Windows uses DriveFs to mount fixed drives in the /mnt folder, but it doesn't automatically mount removable media. And it doesn't look like it puts them in /dev either:

Aaron@MYPC:/$ ls /dev
block  fd  kmsg  lxss  null  ptmx  pts  random  shm  stderr  stdin  stdout  tty  tty0  tty1  urandom  zero

Is there a way to mount removable drives? Is there a way to access the underlying block device?

magicandre1981
  • 97,301
  • 30
  • 179
  • 245
Aaron Campbell
  • 943
  • 2
  • 8
  • 17
  • 5
    See this https://wpdev.uservoice.com/forums/266908-command-prompt-console-bash-on-ubuntu-on-windo/suggestions/13355724-unable-to-access-usb-devices-from-bash. – CodeIt Aug 05 '16 at 03:17
  • type `mount` and see if that gives you clues. If using `cygwin` bash, the path is /cygdrive/. – strobelight Oct 19 '16 at 16:58
  • Nope: `Aaron@MYPC:~$ mount rootfs on / type rootfs (rw,relatime) sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=204320k,mode=755)` [etc] – Aaron Campbell Oct 19 '16 at 17:26
  • 3
    Possible duplicate of [Windows 10 Ubuntu Bash Shell: How Do I Mount Other Windows Drives?](http://superuser.com/questions/1114341/windows-10-ubuntu-bash-shell-how-do-i-mount-other-windows-drives) – DavidPostill Dec 04 '16 at 16:22
  • Similar, although this question specifically mentions removable media and that one refers to fixed media. The highest rated answer on that post is only partially applicable to this question. – Aaron Campbell Dec 05 '16 at 17:04
  • @AaronCampbell And yet you accepted it as an answer...? – Nic Mar 17 '19 at 23:29
  • I was referring to the highest rated answer to the question that @DavidPostill marked as a possible duplicate, not on this thread. – Aaron Campbell Mar 18 '19 at 17:28

6 Answers6

73

Good news, it is now possible to mount USB media (including formatted as FAT) and network shares with drvfs on Windows 10:

Mount removable media: (e.g. D:)

$ sudo mkdir /mnt/d
$ sudo mount -t drvfs D: /mnt/d

To safely unmount

$ sudo umount /mnt/d

You can also mount network shares without smbfs:

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

You need at least Build 16176 so you might have to opt-in to the Windows Insider programm and then update Windows. Source: https://blogs.msdn.microsoft.com/wsl/2017/04/18/file-system-improvements-to-the-windows-subsystem-for-linux/

user643011
  • 2,498
  • 1
  • 16
  • 15
  • 5
    Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by [voting to close it as a duplicate](https://superuser.com/help/privileges/close-questions) or, if you don't have enough reputation for that, [raise a flag](https://superuser.com/help/privileges/flag-posts) to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill May 16 '17 at 15:52
  • 11
    Dear David. Feel free to raise the flag for duplication yourself. This answer is tailored to the question and you are more than welcome to make suggestions for improvement. – user643011 May 16 '17 at 19:37
  • 2
    Dear "user", I upvoted your comment here assuming that what you said was, well, true... Now I realize I was wrong, but can't seem to undo my comment upvote, so I have to ask explicitly: how exactly is this "tailored to the question", while still being the exact "bitwise" copy of [your other answer](https://superuser.com/a/1209698/150263)? – Sz. Aug 08 '18 at 18:50
  • 1
    Hey Sz! Because it's true? It answers exactly: Is there a way to access removable media from within Bash on Windows. If you have a better answer then feel free to answer the question better or suggest an edit to this one. Thanks! – user643011 Aug 10 '18 at 14:54
  • `wrong fs type, bad option, bad superblock on G:, missing codepage or helper program, or other error` -- also, please read [this comment](https://superuser.com/questions/1109993/accessing-removable-media-in-bash-on-windows#comment1764702_1209701); don't answer duplicates. – Nic Mar 17 '19 at 23:55
  • What is "drvfs" supposed to represent in this case? If I do `which drvfs` there is no such program. Documentation is a bit sparse on this on the Googles. – netpoetica Apr 13 '19 at 16:37
  • Is it possible without sudo? – TNT Aug 29 '19 at 18:53
  • @netpoetica `drvfs` is the `vfstype` (type of the filesystem). See `man mount`, e.g. https://linux.die.net/man/8/mount – user643011 Aug 29 '19 at 23:32
  • @TNT: `mount` is a powerful (privileged) command that needs root permissions. You should have no problem to execute `sudo`, because as a developer you should have sudo access on your Windows Subsystem for Linux. If not, try resetting your password or reinstalling it. https://winaero.com/blog/reset-password-wsl-linux-distro-windows-10/ Google it or open a new question if you still have problems. – user643011 Aug 29 '19 at 23:36
  • @user643011: I've got no problems with lost sudo passoword. I'm just wondering, because Linux GUI and Windows auto mount/dismount without asking for elevated permissions. I'm trying to make a "user-proof" backup script, it would be better if not ask for a password. – TNT Aug 30 '19 at 12:51
  • can we dd this mounted device? – ihavenokia Feb 25 '20 at 17:18
10

Is there a way to access removable media from within Bash on Windows?

Update:

Apparently it is now possible starting from Windows 10 Build 16176.

See https://superuser.com/a/1209701/337631.


No.

At the moment there are limitations on what drives are mounted:

In order for a drive to show up under /mnt/ it must meet the following criteria:

  1. The drive must be a fixed drive
  2. The drive must be formatted to NTFS.

This has been raised as an issue: Drives other than C: are not mounted in /mnt #1079. It is still marked as "Open".

To facilitate interoperability with Windows, WSL uses the DrvFs file system. WSL automatically mounts all fixed drives with supported file systems under /mnt, such as /mnt/c, /mnt/d, etc. Currently, only NTFS and ReFS volumes are supported.

Source WSL File System Support


Further Reading

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
2

To add on to the existing answers: if you install an ext driver for windows (e.g. Ext2Fsd) you can mount and ext flesystem like you would an NTFS filesystem. This can be useful if you want to mount Raspberry Pi SDCards.

2

I am finally able to configure WSL2 to automount my SDcard as non-root user and preserve owner:group and rwx permissions on SDCard. My SDcard is NTFS in format.

Step1. enable automount. Create /etc/wsl.conf with this content

$cat /etc/wsl.conf
[automount]
enable = true
options = "defaults,user"
mountFsTab = true

Step2: create mnt/d where we want to mount our drive

mkdir /mnt/d

Step3: Enable fstab entry for SDCard to be mounted at /mtn/d. In my case, D: drive (on surface pro 7)

$ cat /etc/fstab
LABEL=cloudimg-rootfs   /        ext4   defaults        0 1
D:      /mnt/d  drvfs   defaults,user,metadata,exec  0       0

Note:

  1. there are two lines, first one was already there. I added the second line.
  2. options user is requires, else it mounts as root user. and option metadata is required, else it wont be able preserve user, group etc permissions.
  3. exec is needed, else you may not be able to execute files in mounted file system. (I was not able to execute file in windows 11)

Step 4: restart wsl

#exit bash or linux, go to dos/windows prompt
wsl --shutdown
wsl # or bash 
0

If the device is not in /dev it's likely that your setup isn't even detecting the drive.

The command lsblk should list all connected block devices (drives). If it doesn't appear in this list your best bet is to try plugging in the device before starting bash.

I don't see from your question any mention of what method of using bash on windows you've found, but for most it shouldn't be too hard to restart bash.

If lsblk does show you your drive then find the path it lists and type mount <path shown by lsblk> <path you want the drive to be mounted to>

timotree
  • 1,078
  • 1
  • 9
  • 22
  • `mount`ing removable drives is not yet supported. "WSL automatically mounts all **fixed** drives with supported file systems under /mnt, such as /mnt/c, /mnt/d, etc. Currently, only NTFS and ReFS volumes are supported." See https://blogs.msdn.microsoft.com/wsl/2016/06/15/wsl-file-system-support/ – DavidPostill Dec 04 '16 at 16:21
  • lol. I already did it some time ago :) See my answer here [Manipulate windows files with linux bash shell in windows 10](//superuser.com/a/1130707) – DavidPostill Dec 04 '16 at 16:30
0

Mounting block devices in WSL was NOT possible before, but as of recently (Sept 2020), it is possible when you install preview 20211 (and possibly included in the mainstream Oct 2020 update).

command to mount: wsl --mount <DiskPath>

where you get the <DiskPath> via powershell command line: wmic diskdrive list brief

see here for details: https://devblogs.microsoft.com/commandline/access-linux-filesystems-in-windows-and-wsl-2/

Nepaluz
  • 119
  • 1
  • 2