Howto: Mounting without sudo

This had been bothering 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 problematic. 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 key at /dev/sda1 that I want to mount to a folder called /media/usbdisk. 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. ls -lha gives …

drwxr-xr-x 2 root root 16K 1969-12-31 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 /media/usbdisk 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 1969-12-31 17:00 usbdisk

and the color 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 kmandla:kmandla /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 kmandla:kmandla /media/usbdisk

ls -lha shows …

drwxr-xr-x 2 kmandla kmandla 6 2007-03-08 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 kmandla kmandla 6 2007-03-08 15:52 usbdisk

Excellent. And the word “usbdisk” is in reverse blue and green. Now we’re in business. Let’s remount.

sudo mount /dev/sda1 /media/usbdisk

And from ls -lha shows. ..

drwxr-xr-x 2 root root 16K 1969-12-31 17:00 usbdisk

What the … ? Mounting a drive changed it all back! And when it’s unmounted, the permissions and ownership are reverted to 777 and kmandla:kmandla! Argh! :evil:

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 unelegant. And the brute solution is not always the best one.

I tinkered a bit with changing the /etc/sudoers 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 fstab, of all places. Or to be more specific, the way the mount command interfaces with the fstab file.

Try this: Make a folder where you want to mount the device. In the example it’s /media/usbdisk. Then open your fstab 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 /media/usbdisk folder, using the vfat filesystem. In addition, noauto means Ubuntu (or Linux really, since my Arch system does the same thing) 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. user allows any user to mount and unmount the drive, and users allows anyone to unmount a drive mounted by another user (um, look at the man pages for mount if that doesn’t make sense :roll: ).

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

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 kmandla kmandla 0 2007-03-08 16:28 testfile

Perfect. Unmounting is the same, with only umount /dev/sda1 needed.

The beauty of this is that you can use the group 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 /dev/sda*) 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 filesystem has to match, too. So in short, it has to be exactly what fstab expects.

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 homemade script. (I pasted a copy into the Ubuntu-NL pastebin, as a backup.)

#!/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. Disgustingly simple, but some of my finest work. Copy, paste into a file and make executable. If you’re a fellow Openbox fan, you can now add it as a menu item or — even better — trigger it with a keybinding. Is Alt-M taken? :mrgreen: Have fun!

P.S.: This method should work for CDROM mounting or other drives too, so long as you can comfortably predict the device assignment and the filesystem it will encounter. And check man 8 mount if you want more details or a better, fuller explanation of the mount-fstab interrelationship.

About these ads

11 Responses to “Howto: Mounting without sudo”


  1. 1 Luke 2007/03/08 at 9:25 PM

    I think this is the correct way to do it. When I started reading I wanted to chime in with the fstab and users suggestion but I see you figured it out in the end.

    As a side note, I use Kubuntu on Dapper. I noticed that when I put something in fstab it automatically appears as an icon on KDE desktop and in the devices menu. If I need to mount it, I just need to click on one of the desktop shortcuts and it will be automatically mounted as Konqueror opens it.

    It will also work from within any KDE device. I find it to be really convenient – for example I often save attachments to the local Windows share at work. All I have to do is to choose “Save As” in KMail, then click on Devices, and then on the share icon and save. I don’t even need to think about mounting. :)

  2. 2 mtz 2007/03/08 at 11:32 PM

    to go around most of your “problems”, you might want to pass a couple or arguments to the mount command ..user=me, will give you default ownership of the files, rw, will give you read-write access to the drive, nouid will tell your system not to mind file/folder owhership. i also believe an option -n will allow you to mount without requiring root privileges.

    you should read mount options, it will solve a lot of your “problems”

    /etc/mtab “manages” removable devices that are plugged in when the system is already up(fstab is used when the system is booting up)

  3. 3 K.Mandla 2007/03/08 at 11:33 PM

    :mrgreen: Thanks, Luke. This was definitely one of those things I had to work out for myself. It seemed almost counterintuitive, but I got it in the end.

    I wasn’t aware KDE handled mountpoints so easily. I’ll have to get over my aversion to it and see what good stuff is on that side of the fence. :) Cheers!

  4. 4 Luke 2007/03/09 at 7:55 AM

    mtz – I don’t think you are supposed to edit mtab. I was under impression that it is automatically updated when you use the mount command.

    K.Mandla – I’m the other way – I live in a KDE world, and rarely peak out to the Gnome side. At the moment the KDE support in Kubuntu (Dapper) is decent, but a little less polished than the Ubuntu’s Gnome. But there was a big improvement from Hoary to Dapper. Hopefully Edgy and Feisty will continue this trend of getting better ant running KDE. :)

    Whenever I’m working in Gnome I instantly miss things like one click mounting or the fish:// protocol which lets me to access files on remote machines via scp, smb, sftp or whatever just as if they were local files (ie. I can open them with any KDE based application, modify, save and close while KDE will automatically cache my changes locally, and then upload them via network).

  5. 5 John 2007/03/11 at 9:13 AM

    Luke: true, but sometimes KDE expects applications to be able to understand its terms.

    For example, in Firefox, if I try to save a file to a removable drive using the KDE file selector, the file selector tries to pass media://sdb1/path to Firefox, which it rejects. If KDE used a normal path, this problem wouldn’t exist.

    But overall I agree with you. I’m just starting to look at DCOP – commandline control of KDE’s programs seems very interesting.

  6. 6 Luke 2007/03/11 at 12:04 PM

    John – I actually haven’t noticed that. I stopped using the default Kubuntu version of Firefox when it became outdated. Now I use the 2.0.0.1 binary from the firefox website with the generic gtk file chooser which of course doesn’t “see” some KDE specific paths.

    It is a little annoying since the file chooser is so different but you get used to it after a while.

    Still, I can mount a drive by clicking on it it and then point my FF to the mountpoint manually when I want to save to it :)

  7. 7 zariuq 2007/05/05 at 5:33 PM

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

    is what you say to change the fstab thing to… but that is annoying as I like the auto thing…

    I looked at my fstab file and noticed that I had something where the noauto was that was not noauto, umask=000 (do the 0′- represent groups, users and stuff?? because group 0 on my computer is root)

    so I just changed the noauto to that…

    /dev/sda1 /media/usbdisk vfat users,umask=000 0 0

    would this work to have it available to users and have it auto done?
    (it probably isn’t, because I am a lame newbie… but I am hopefull)

  8. 8 Aleksey Zapparov A.K.A. iXTi 2007/06/06 at 12:19 AM

    Well, if you want to mount concrete usb flash-disk, you can do it without fstab tricks.

    The probl;em in options, they are have to be (for vfat fs):
    rw,users,noatime,uid=$UID,shortname=lower,codepage=866,utf8

    so the sudo mount command should looks like:
    sudo mount /DEVICE_NODE /MOUNT_POINT -o rw,users,noatime,uid=$UID,shortname=lower,codepage=866,utf8

    I’m too lazy to type all this s*** all the time I insert my usb flashdisk, so here’s my solution:
    $ mkdir -p ~/bin && cd ~/bin
    $ touch mnt-myflashdisk && chmod 777 mnt-myflashdisk
    $ echo “sudo mount /dev/sdb /mnt -o rw,users,noatime,uid=$UID,shortname=lower,codepage=866,utf8″ > mnt-myflashdisk
    $ touch umnt-myflashdisk && chmod 777 umnt-myflashdisk
    $ echo “sudo umount /dev/sdb” > umnt-myflashdisk

    so now, when I’m inserting my bsdstyle-partitioned vfat usb flashdisk i just type mnt-myflashdisk and that’s all

  9. 9 Alexandru 2007/06/20 at 9:06 AM

    To accomplish the same purpose, you can restructure the command so it looks like:

    sudo mount -o uid=$USER,gid=$USER /dev/sda1 /mnt/point

    This will make user and group ownership of the mount point belong to your username and not root.

  10. 10 desvinchado 2008/12/11 at 7:34 AM

    YEAH!!! (snip) YEAH. sorry i’ve been trying to get this done over a week now. it’s my first openbox install (or no-gnome-ubuntu) and could not find the way to make pcman see my ntfs and ext3 music partition on start as my user. Thanks a lot!!!
    Dual boot xp and ubuntu and a ext3 for my files.
    ivman for automount.

    ad this lines to fstab:
    /dev/sda1 /media/disk-1 ntfs users 0 0
    /dev/sda2 /media/disk ext3 users 0 0

    just take away the noauto option so they automount.
    bye

  11. 11 Roky 2011/01/25 at 4:27 AM

    I was copying from pendrive to drive partition and suddenly system was hanged and i had to restart my system. when I click my partition: It has occurred an Error Message..
    It is shown below:
    *************************************************************
    Error mounting: mount exited with exit code 13: $MFTMirr does not match $MFT (record 0).
    Failed to mount ‘/dev/sda7′: Input/output error
    NTFS is either inconsistent, or there is a hardware fault, or it’s a
    SoftRAID/FakeRAID hardware. In the first case run chkdsk /f on Windows
    then reboot into Windows twice. The usage of the /f parameter is very
    important! If the device is a SoftRAID/FakeRAID then first activate
    it and mount a different device under the /dev/mapper/ directory, (e.g.
    /dev/mapper/nvidia_eahaabcc1). Please see the ‘dmraid’ documentation
    for more details.
    **************************************************************

    I’ve tried Mount Manager but It didn’t work. Plz some1 is here to help and trouble shoot this problem.

    Tell me
    WELCOME
    Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Welcome!



Visit the Wiki!

Some recent desktops


May 6, 2011
Musca 0.9.24 on Crux Linux
150Mhz Pentium 96Mb 8Gb CF
 


May 14, 2011
IceWM 1.2.37 and Arch Linux
L2300 core duo 3Gb 320Gb

Some recent games


Apr. 21, 2011
Oolite on Xubuntu 11.04
L2300 core duo 3Gb 320Gb

Enter your email address to subscribe to this blog and receive notifications of new posts.

Join 405 other followers

License

This work is licensed under the GNU Free Documentation License. Please see the About page for details.

Blog Stats

  • 3,961,025 hits

Archives


Follow

Get every new post delivered to your Inbox.

Join 405 other followers

%d bloggers like this: