Thursday, 30 August 2007

How to automount a usb stick and make it accessible to non root users

I will assume that you have autofs and udev installed.

First of all, go to /etc/udev and create a file called usbstick.rules
The contents of that file are as follows:

ACTION!="add", GOTO="usbstick_rules_end"
SUBSYSTEM=="usb_device", GOTO="usbstick_rules_real"
SUBSYSTEM=="scsi", GOTO="usbstick_rules_real"


SYSFS{idVendor}=="XXXX", \
SYSFS{idProduct}=="XXXX", \
KERNEL=="sd?1", \
NAME="%k", \
SYMLINK="usbstick", \
MODE="0660", \


This will create a symlink to /dev/sdX1 called "usbstick" with the group "usbstick" each time the usb device with the given product and vendor id is inserted. You can check the ids with "lsusb -v"

Remember to create a link from /etc/udev/rules.d to that file usbstick.rules

After restarting the udev daemon and inserting the stick you should see something like:

lrwxrwxrwx 1 root root 4 Aug 30 20:58 /dev/usbstick -> sdb1
brw-rw---- 1 root usbstick 8, 17 Aug 30 20:58 /dev/sdb1

So, we now have a link to the device that should be accessible to anyone on the usbstick group.

Only thing left is to mount the device. It can be done adding this line to /etc/auto.removable

usbstick -fstype=vfat,rw,gid=1004,umask=002 :/dev/usbstick

gid 1004 is the id for the usbstick group.

We will also need to create a link to the removable device, something like (from /)

ln -s /var/autofs/removable/usbtick usbstick

After inserting the stick and changing dir into /usbstick you should now see the contents of your stick.

No comments: