Harry
Harry

Reputation: 3937

How to read and write the raw device file of a usb flash drive as non-root

My USB flash drive gets automatically mounted at /media/ME without requiring root intervention.

Now, I can read and write sectors on the drive mounted at /dev/sdX device using open, lseek, read, and write. But I can do this only as root.

Question: Just as the device can get mounted without root intervention, is it also possible to read and write from/to this device without being root?

If it's not possible, I'm willing to show a GUI password dialog for root. But how do I do this from my console program?

Upvotes: 2

Views: 3184

Answers (1)

abasterfield
abasterfield

Reputation: 2282

Yes

The block device /dev/sdX has standard UNIX permissions - so if it is owned by your user or a group your user is in and the permissions are set correctly then you will be able to write to it.

$ ls -l /dev/sda
brw-rw---T 1 root disk 8, 0 Dec  3 18:27 /dev/sda

So only root and members of the group disk can read or write to my /dev/sda. If I put myself in the disk group I would be able to write to the block device.

You can set-up udev to set the permission of the block devices automagically when they are plugged in.

Upvotes: 3

Related Questions