K.Mandla's blog of Linux experiences

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

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/sda1sudo 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.)

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.

Post navigation

13 thoughts on “Howto: Mounting without sudo”

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. :)

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)

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).

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.

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 :)

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.

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.