January 31, 2014 · bash debian hacks linux productivity raspberrypi server shell tricks

Mounting without sudo

sudo

This had been bothering me for some time. On a system without an automounting solution, which I don't usually like because they involve daemons and message subsystems and whatnot, mounting an external drive, such as a USB key, can be a nuisance. Mounting requires sudo, but sudo inflicts root ownership on the target, which makes it inaccessible to a non-root user, short of using sudo over and over again.

Here's what I mean. Let's suppose I have a USB drive at <strong>/dev/sda1</strong> that I want to mount to a folder called <strong>/media/usbdisk</strong>. First, I make the folder.

sudo mkdir /media/usbdisk

Simple enough. Now, mount /dev/sda1 to that folder.

sudo mount /dev/sda1 /media/usbdisk

Also pretty simple. But look at who owns the folder. <strong>ls -lha</strong> gives

drwxr-xr-x 2 root root 16K 2014-01-21 17:00 usbdisk

If I try to

touch /media/usbdisk/file

I get

touch: cannot touch `/media/usbdisk/file': Permission denied

Okay, so let's change the permission on <strong>/media/usbdisk</strong> and try again. Gotta use sudo.

sudo chmod 777 /media/usbdisk

No errors. 777 should make it read-write-execute across the board. But

ls -lha /media

still says

drwxr-xr-x 2 root root 16K 2014-01-21 17:00 usbdisk

and the colour hasn't changed. And again,

touch /media/usbdisk/file

gives a permission denied error. Maybe recursively?

sudo chmod -R 777 /media/usbdisk

Nope.

Creating a non-root file on the USB disk is still prohibited. Perhaps the ownership needs to be set?

sudo chown bubbl:bubbl /media/usbdisk

Aw, nuts:

chown: changing ownership of `/media/usbdisk': Operation not permitted

Well, perhaps the ownership and permissions can't be set while the drive is mounted. Let's unmount, then set the ownership and permissions.

sudo umount /dev/sda1 sudo chown bubbl:bubbl /media/usbdisk

Now, <strong>ls -lha</strong> shows:

drwxr-xr-x 2 bubbl bubbl 6 2014-01-22 15:52 usbdisk

That's promising. Just to be safe, let's make sure the permissions are set.

sudo chmod -R 777 /media/usbdisk

And ls -lha shows:

drwxrwxrwx 2 bubbl bubbl 6 2014-01-22 15:52 usbdisk

Excellent. And the word <strong>usbdisk</strong> is in reverse blue and green.

Now we're in business. Let's remount.

sudo mount /dev/sda1 /media/usbdisk

And from ls -lha we get:

drwxr-xr-x 2 root root 16K 2014-01-22 15:52 usbdisk

What the frickity frack?

Mounting a drive changed it all back! And when it's unmounted, the permissions and ownership are reverted to 777 and bubbl:bubbl! Argh!

So it seems that when the drive is mounted with sudo, the folder reverts to root:root. So what, right? Just copy everything or write everything or start applications with sudo, and you're fine. Maybe, but it's terribly un-elegant. And the brute solution is not always the best one.

I tinkered a bit with changing the <strong>/etc/sudoers</strong> file, but that's not really where the problem lies. And really, if I could solve the mounting issue without sudo, that would be an even better solution.

So, instead, I turned to the <strong>/etc/fstab</strong>, of all places. Or to be more specific, the way the <strong>mount</strong> command interfaces with the <strong>fstab</strong> file.

Try this: Make a folder where you want to mount the device. In the example it's <strong>/media/usbdisk</strong>. Then, open your <strong>fstab</strong> file and add this line to it:

/dev/sda1 /media/usbdisk vfat users,noauto 0 0

In this case, we're adding the /dev/sda1 device to the <strong>/media/usbdisk</strong> folder, using the vfat filesystem. In addition, <strong>noauto</strong> means Linux won't automatically mount it at startup or with the mount -a command, which is good, since there isn't necessarily going to be anything there. <strong>user</strong> allows any user to mount and unmount the drive, and <strong>users</strong> allows anyone to unmount a drive mounted by another user (um, look at the man pages for mount if that doesn't make sense).

Now, from the terminal, a sudo mount -a command, to refresh the system mount array (or a reboot, if you want), and"¦

mount /dev/sda1

gives no errors. And if I

touch /media/usbdisk/testfile

I get no permissions error and

ls -lha /media/usbdisk

shows

-rwxr-xr-x 1 bubbl bubbl 6 2014-01-23 16:32 testfile

Perfect. Unmounting is the same, with only

umount /dev/sda1

needed.

The beauty of this is that you can use the <strong>group</strong> option to align this command against a specific user group and write symbolic links in home directories straight to the target folder. So each user in the USB-stick-accessible group could mount and unmount the stick, and get to the guts within one folder level.

Of course, there are a few downsides. First, you have to be certain that your device assignment is correct. As far as I know, and please correct me if I'm wrong, there's no way to mask particular devices (like <strong>/dev/sda*</strong>) in fstab - you have to use the exact device assignment. So if it doesn't pop up as sda1, it's not going to mount (or at least it wouldn't for me).

Furthermore, a USB drive with more than one partition might show up as sda1, sda2, sda3"¦ If you want a particular partition that's not the one listed in fstab, it won't work (or again, it wouldn't for me). And the file system has to match, too. So in short, it has to be exactly what fstab expects it to be.

Maybe there are ways around those things, but for now I'm content with this.

One last bonus: If you get this set up, and you want to be able to mount without constantly reaching for the terminal button, try this home-brewed script:

#!/bin/sh 
mtab_sda1='$(cat /etc/mtab | grep sda1 )' 
if [ -z '$mtab_sda1' ]; then 
  mount /dev/sda1 
else 
  umount /dev/sda1 
fi

Basically, it just checks the /etc/mtab file for anything referring to sda1, and mounts it if it finds nothing, or, dismounts it if it finds it. Copy, paste into a file and make executable.

This method should work for CD/DVDROM mounting or other drives too, so long as you can comfortably predict the device assignment and the file system it will encounter. And check

man 8 mount

if you want more details or a better, fuller explanation of the mount-fstab interrelationship.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket

Contact