Trying lots of different Linux distributions on all kinds of hardware, I find myself typing commands like this quite often:
sudo dd if=xubuntu-13.10-desktop-amd64.iso of=/dev/sdc bs=10240
Needless to say, sooner or later I will mistype the destination and wipe a harddrive instead of the intended USB drive. I would like not to use sudo
every time here.
On my system, a fairly modern Ubuntu, permissions on /dev/sdc
are like: (when a stick is present):
$ ls -al /dev/sdc*
brw-rw---- 1 root disk 8, 32 Apr 6 22:10 /dev/sdc
How do I grant my regular user write access to random USB sticks but not other disks present in my system?
Best Answer
I think you can use UDEV to do what you want. Creating a rules file such as
/etc/udev/rules.d/99-thumbdrives.rules
you'd simply add a rule that will allow either a Unix group or user access to arbitrary USB thumb drives.Would create the device using the user
<user>
and group<group>
.Example
After adding this line to my system.
And reloading my rules:
If I now insert a thumbdrive into my system, my
/var/log/messages
shows up as follows:Now checking out the device files under
/dev
shows the following:So it would seem to have worked.
Being more explicit
The above will work but will likely have these rules getting applied to every block device which isn't quite what we want. To narrow its focus a bit you can use
ATTRS{..}==...
attribute rules to restrict the application to specific hardware. In my case I only want it to be applied to a single USB thumbdrive.Step #1 - uniquely id device
So to start we can use this command once we've mounted the particular thumb drive so that we can use
udevadm
to scrutinize it, groping it for its particular attributes.Here I'm focusing on looking at the "manufacturer" and "product" attributes.
NOTE:
ATTRS{..}==..
attributes are attributes to parent devices in the hierarchy of where this device's device file is ultimately deriving from. So in our case the block device being added,/dev/sdb
is coming from a USB parent device, so we're looking for this parent's attributes,ATTRS{manufacturer}=...
, for example.So in this example I'm selecting the manufacturer "JMTek" and the product "USBDrive".
Step #2 - modify .rules flie
So with these additional bits in hand let's add them to our original
.rules
file.Step #3 - Trying it out
Now when we reload our rules and unmount/remove/reinsert our USB thumbdrive again we get this rule:
However if I insert a completely different device:
References