5

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?

6

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

5
  • I have few files with some kind of organized names in '/etc/udev/rules.d/': 40-scratch.rules 70-persistent-cd.rules 99-input.rules These names look like init.d script names. Names are just for ordering rules? I don't have to register that somewhere, like with init scripts?
    – Kamil
    Jul 30 '14 at 21:34
  • No. You just need to create the files and use the .rules suffix. The files are processed in lexicographical order (i.e., the same as you'd get with ls -1), which is why numbers are used at the beginning, but that's not required. I don't know what takes precedence (rules read first or rules read last) in the case of some kind of conflict -- but you don't need to worry about that in this case. Just make sure the file name is unique, since man udev says "files with identical filenames replace each other".
    – goldilocks
    Jul 30 '14 at 22:38
  • I like this. Very elegant method. Im gonna test this before I accept.
    – Kamil
    Jul 30 '14 at 22:42
  • I did test it, BTW, exactly as is -- I have a raspbian pi -- so if it doesn't work, leave another comment.
    – goldilocks
    Jul 31 '14 at 12:14
  • 1
    Works on Ubuntu as well. Oct 19 '16 at 1:44
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.