How to Set Device RW Permissions Permanently on Raspbian

devicespermissionsraspberry piraspbian

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?

Best Answer

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

Related Question