How can I set device rw permissions permanently on Raspbian?

I’m using the Raspbian (a distribution made for Raspberry Pi, which is based on Debian).

I have some scripts that use i2c.

Normally only root has read and write permissions for i2c.

I’m using this command to add i2c r/w permissions for normal user:

# chmod a+rw /dev/i2c-*

However after reboot, these devices have their default permissions.

What is the best way to make my i2c available for r/w for a normal user permanently?

Is there a more “elegant” way than adding my script to init.d that runs the command above after my Raspberry Pi boots?

Asked By: Kamil

||

You can do this using udev. Create a file in /etc/udev/rules.d with the suffix .rules, e.g. local.rules, and add a line like this to it:

ACTION=="add", KERNEL=="i2c-[0-1]*", MODE="0666"

MODE=0666 is rw for owner, group, world. Something you can do instead of, or together with that, is to specify a GID for the node, e.g:

GROUP="pi"

If you use this instead of the MODE setting, the default, 0660 (rw for owner and group) will apply, but the group will be pi, so user pi will have rw permissions. You can also specify the OWNER the same way.

Pay attention to the difference between == and = above. The former is to test if something is true, the latter sets it. Don’t mix those up by forgetting a = in ==.

You have to reboot for this to take effect.


“Writing udev rules” Reference

Answered By: goldilocks

There are some groups of devices that have group owners other than root. These include disk, input, sound, tty and serial devices. For these, you can provide access to the entire group of devices by adding the user to the group.

E.g., to provide access to /dev/ttyUSB0 to user pi. You ls -l the device and get,

crw-rw---- 1 root dialout 4, 64 Jan 19 01:36 /dev/ttyUSB0

Since the group is dialout, you add pi to the dialout group in /etc/group,

dialout:x:20:pi

The pi user can then log out and back in to activate the new group membership.

Answered By: Roger Dahl