Rob Sears bio photo

Rob Sears

       

Rocket scientist. Computer hacker. Geek before it was cool.

BTC Donations:
1AU9qGkSubhR24r8Y4WEoV8bccZjeT2dKg

I’ve been booting multiple operating systems for a long time. So long, in fact, that I’ve found it necessary to install each OS on to its own small partition and share files between them. For example, I created a “Media” partition on my 1TB hard drive where I keep my music, ebooks, etc. Windows and my various Linux distros all share this partition, such that if I download a new album on iTunes, those files become available to my media players when I boot into Linux. I think this is actually pretty common among dual-booters and Linux-only people alike.

However, Linux can be very finnicky when it comes to mounting other file systems and doesn’t always act like I want. For instance, it will mount “Media” as a root-owned device by default, making me “sudo” everything little thing I want to do on that drive. Fortunately, there’s /etc/fstab. This file loads when the machine boots and mounts the various drives however its instructed. So I’ve collected some tips and tricks to make using this file easy for the use case I’ve described.

The /etc/fstab file is composed of lines like this:

<file system> <mount point> <type> <options> <dump> <pass>

You should see at least 3 lines: one each for proc, swap and the root filesystem ( / ). If you have any other hard drives or partitions, you may see them in here too. Generally, these entries will look like:

/dev/<some device> /local/mount/point vfat auto,nouser,noexec,fmask=0777,dmask=0773 0 0

I’m not going to cover the plethora of options and filesystems. These lists are easily found elsewhere online (I particularly like the Arch wiki article). I do want to spend a moment discussion one set of options: fmask and dmask.

fmask and dmask are masks for files and directories, respectively. A mask is basically a general rule to apply to a file, unless that rule is specifically overridden. In other words, it tells the system “Give the file/directory these permissions, unless you are told otherwise by an authorized user.” With fstab, you can tell Linux what to do with the files that are mounted on a given partition using the fmask and dmask options.

fmask and dmask are set with a set of 4 digits, the first of which is always a 0, to indicate an octal. The remaining three are octal permissions for owner, group and user (world), respectively. See below:

0 1 2 3 4 5 6 7
R Y Y Y Y N N N N
W Y Y N N Y Y N N
X Y N Y N Y N Y N

For example, an fmask of 0000 would translate to:

O G U
R Y Y Y
W Y Y Y
X Y Y Y

So, the owner, group and world can all read, write and execute files on this partition. This probably isn’t the behavior we want, though. We would probably want the owner to have full rwx permissions over the files, and members of the group to have at least r permissions. Maybe we don’t want anyone outside the group to have any access whatsoever to the files. So referring to the chart our mask would have owner=0, group=3 and user=7. This gives a mask of 0037.

This is actually really cool because it gives us complete flexibility over who can access and modify our files.

But what if we have multiple users on the same machine? Easy! We can simply create a group that will have access to the partition, then add any users who need the partition to this group. For example, if you have three users, mom, dad and admin, and all three need access to the music on a media partition, you could add them to a group called “family” and tell fstab to mount the media partition with admin as the owner and family as the group. Then, you could set fmask and dmask as 0017. This gives admin, mom and dad the ability to read and write files, but mom and dad don’t get execute permissions (because that would be a terrible idea). Anyone else gets denied.

So a good fstab entry to this affect would be something like:

/dev/<device> /mount/point ntfs-3g rw,auto,user,exec,uid=1000,gid=35,fmask=0007,dmask=0003 0 0

This mounts the partition to a local mount point as an NTFS (Windows) filesystem, giving ownership to the admin, and group 35. The admin and group all get rwx permissions on everything. Everyone else gets read access on directories, and no read access on files.

Hopefully that clarifies the fstab file a bit. It’s a deceptively flexible system, but not terribly complex. Happy mounting!