How to mount a USB stick as a non-root user with write permission

pendrive

So you want to use a USB stick or a USB hard drive, and you don’t want to mount it as root every time?

Why would you want to do this?

  • It’s a hassle to mount the USB stick using sudo every time – you have to type the root password, and you have to specify all the mount options each time you  mount it.
  • The permissions on a FAT32 USB stick or drive don’t allow write permissions as you, only root, so you have to sudo any write based file operation on the USB device. This is because the commonly found format of most USB disks is FAT32, as it has the best compatibility with Windows machines and is supported on Mac OS and Linux too, unfortunately FAT32 has no notion of file permissions, unlike EXT4).

Another point worthy of note, if you are someone who uses a shiny, full fat desktop like GNOME or KDE, then you’ll likely find that things like USB removable media are automatically mounted for you, so it is often fine to use it in that capacity. However, for Linux luddites and server admins like me, that just won’t cut it, I want an easy user-level mount at the command line, or bust!

Ok, you’ve made up your mind that you want to be able to mount the disk as a real user, not root. The good thing is, is that this is really quick to do.

Firstly, we need to create a directory where the drive is going to be mounted to. So if you haven’t done so already, create this directory:

$sudo mkdir -p /media/<username>/usb
$sudo chown <username> /media/<username>/usb
$sudo chmod 0777 /media/<username>/usb

Obviously, you can call the mount point whatever you want, I’ve just called it usb and stuck it in the /media/<username>/usb directory (note <username> is your own username). The last two commands change the ownership to be ‘you’, and sets the permissions to read and write for all users (change this if you want it to be less open).

Next up, we need to ascertain which drive you want to mount. You may already know this, but if you don’t know what the /dev/ entry for the USB stick is, then you can find this out by sticking it in the usb port on your machine and running dmesg:

$dmesg
[snip]
[95921.903363] usb-storage 1-2.2:1.0: USB Mass Storage device detected
[95921.903445] scsi host5: usb-storage 1-2.2:1.0
[95922.902013] scsi 5:0:0:0: Direct-Access USB DISK 2.0 PMAP PQ: 0 ANSI: 6
[95922.902347] sd 5:0:0:0: Attached scsi generic sg2 type 0
[95923.714434] sd 5:0:0:0: [sdc] 15133248 512-byte logical blocks: (7.74 GB/7.21 GiB)
[95923.714639] sd 5:0:0:0: [sdc] Write Protect is off
[95923.714642] sd 5:0:0:0: [sdc] Mode Sense: 23 00 00 00
[95923.714848] sd 5:0:0:0: [sdc] No Caching mode page found
[95923.714851] sd 5:0:0:0: [sdc] Assuming drive cache: write through
[95923.719842] sdc: sdc1
[95923.721331] sd 5:0:0:0: [sdc] Attached SCSI removable disk
[/snip]

You can see that it’s detected that a USB storage device on sdc, and that the partition it can see is called sdc1 (highlighted above in bold).

Now all you need to do is edit the /etc/fstab file and add this line at the bottom:

$sudo vim /etc/fstab

/dev/sdc1 /home/storage auto user,umask=000,utf8,noauto 0 0

Remove the ‘noauto’ bit if you want it to be mounted automatically on boot, however that’s probably a bad idea if its a removable device!

That’s all the hard work done, and you shouldn’t have to do that ever again, now all you need to do to mount it, whenever you like, is to issue a simple mount command:

$mount /dev/sdc1

or, alternatively you can mount it by its mount point. Either way, it doesn’t matter 🙂

$mount /media/<username>/usb

Hope this saves you some aggravation!

Leave a Reply