How many of us now use mobile drives such as USB thumb drives or larger capacity external drives? With the cost of media going down it is much more accessible to many of us. Now, the way that Ubuntu and Linux handle these by default may cause your drive to mount in a dynamic location each time you plug it in. This tutorial will outline how to make sure your USB drive or external hard drive mount at your specified location each time.
Ubuntu uses a service called udev to handle management of dynamic drives. It is really simple to tell udev how you’d prefer things to be mounted. Just follow the few steps below:
- Edit (or create) the file: /etc/udev/rules.d/10-local.rules
- note: if you had to create this file you’ll also need to specify the correct permissions for security (see comments) using: sudo chmod 644 /etc/udev/rules.d/10-local.rules
We’ll get back to what to put in that file in just a second, but first we need to find out a couple of key pieces of information about the device. We need three things to list in the 10-rules.local file.
- The type of connection (ie; “usb” or “ieee1394” for firewire)
- The unique data point of the device itself (for USB it is in the format: SYSFS{serial}==”0000060412047593″ and for FireWire: SYSFS{guid}==”0x0030e0f4e020e229″)
- The destination where we would now like to mount the drive.
The first part is easy. You, hopefully, know the connection type of your device. Either “usb” or “ieee1394”. The third part is also easy. Where do you want it to mount? The second step, however, is a little bit trickier but with one of the following commands its a breeze!
udevinfo -a -p $(udevinfo -q path -n /dev/sda) | grep SYSFS{serial}
udevinfo -a -p $(udevinfo -q path -n /dev/sda) | grep SYSFS{guid}
Note: The only variable in the above command is the location. If your device has mounted using /dev/sdb, /dev/sdc, etc. You’ll need to make sure you’re using the right location. If you don’t get any values returned try the next. Also, remember the first example is used to find a USB device while the second is for FireWire.
This command will return a few lines and we need the one matching the format similar to:
SYSFS{serial}=="0000060412047593" (usb devices)
SYSFS{guid}== "0x0030e0f4e020e229" (firewire devices)
At this point we now have the three ingredients for our 10-local.rules file. Here is the format and an example of what to enter into the file:
BUS=="usb", SYSFS{serial}=="0000060412047593", NAME{all_partitions}=="temp"
Simply replace the proper values with your own and put it in the /etc/udev/rules.d/10-local.rules. Again, BUS is looking for the type of connection. SYSFS is looking for the unique data point and NAME is asking for the destination mount point.
After you’ve entered these three ingredients you’ll need to test things out. First you’ll want to restart the udev service:
sudo /etc/init.d/udev restart
You can then run a command to test the new udev settings. Fittingly enough the program is udevtest and you would use the following command:
sudo udevtest /sys/block/sda block
As mentioned above you would need to replace the block area with your device. It may be sdb, sdc, sdd, etc.
Assuming everything works you system will now automagically create the dev/ and media/ point for that specific piece of hardware each time you boot. Go ahead and duplicate the process for any device you like. USB drives, removable disks, digital cameras, music players, etc. The same steps apply to any mobile usb or firewire based device!
Note: if you run into trouble or have questions about this process check out more information at the Ubuntu Forums or at Writing udev Rules by Daniel Drake.
I’m wondering why you advocate mode 777 for the /etc/udev/rules.d/10-local.rules file? Since that directory is accessible to everyone, world write permissions means that anyone can change the file at any time. As a “10-” file will be picked up before the “50-” file (which holds the shipped-with-the-distro rules), any user can override any rules in later files. This means that any user can take control of any device, which could easily lead to rooting the box.
Or am I missing something?
Lamont you’ve got a good point. I guess I didn’t realize some of the implications of that. I’ve updated the post for 644 to match the permissions on the other files.
Thanks for keeping an eye out there.
Ubuntu since about 9.10 replaced the udevinfo command, and several others, with the udevadm command… which has a slightly trickier syntax. For a more modern howto, see here: http://j1.savesources.com/blogs/show/2