bashraspberry-pichmodchownraspberry-pi2

Access GPIO (/sys/class/gpio) as non-root


The /sys/class/gpio can only be accessed as root by default. So I like that a new group gpio can use the files and directories under /sys/class/gpio. To achieve that I added the following lines to /etc/rc.local (I'm on Debian):

sudo chown root:gpio /sys/class/gpio/unexport /sys/class/gpio/export
sudo chmod 220 /sys/class/gpio/unexport /sys/class/gpio/export

So this gives write permissions to all the gpio group members. So they can now export and unexport pins fine.

The problem is they can't read/write the specific pin files after export (e.x. /sys/class/gpio/gpio17) beacause those are owned by root:root again.

How can I change that they are created by default as root:gpio too? I mean I can do that manually each time I export a pin. But that's a bit uncomfy.

UPDATE

According to larsks' answer I created the missing rule file. Now it partially works:

-rwxrwx---  1 root gpio 4096 Jun 19 16:48 export
lrwxrwxrwx  1 root gpio    0 Jun 19 16:51 gpio17 -> ../../devices/soc/3f200000.gpio/gpio/gpio17
lrwxrwxrwx  1 root gpio    0 Jun 19 16:45 gpiochip0 -> ../../devices/soc/3f200000.gpio/gpio/gpiochip0
-rwxrwx---  1 root gpio 4096 Jun 19 16:45 unexport

But for the ./gpio17/ I still get root:root:

-rw-r--r-- 1 root root 4096 Jun 19 16:52 active_low
lrwxrwxrwx 1 root root    0 Jun 19 16:52 device -> ../../../3f200000.gpio
-rw-r--r-- 1 root root 4096 Jun 19 16:52 direction
-rw-r--r-- 1 root root 4096 Jun 19 16:52 edge
drwxr-xr-x 2 root root    0 Jun 19 16:52 power
lrwxrwxrwx 1 root root    0 Jun 19 16:52 subsystem -> ../../../../../class/gpio
-rw-r--r-- 1 root root 4096 Jun 19 16:52 uevent
-rw-r--r-- 1 root root 4096 Jun 19 16:52 value

UPDATE 2

Okay I solved the problem. Because I installed Raspbian over the RaspbianInstaller I never went through the raspi-config tool. This seems to be a problem. Because I was also missing the /sys/device/virtual/gpio/ folder.

I followed this guide here: https://community.element14.com/products/raspberry-pi/f/forum/26425/piface-digital-2---setup-and-use#139528

And afterwards the permissions were correct (even for the pin-folders and their files value, direction, ...).


Solution

  • You can do this using udev rules, which can define actions to execute when the kernel instantiates new devices. Current versions of the Raspbian distribution for Raspberry Pi devices contain the following in /etc/udev/rules.d/99-com.rules:

    SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 770 /sys/devices/virtual/gpio'"
    

    This ensures that entries under /sys/class/gpio are always available to members of the gpio group:

    # ls -lL /sys/class/gpio/
    total 0
    -rwxrwx--- 1 root gpio 4096 May  6 23:36 export
    drwxrwx--- 2 root gpio    0 Jan  1  1970 gpiochip0
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 unexport
    # echo 11 > /sys/class/gpio/export 
    # ls -lL /sys/class/gpio/
    total 0
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 export
    drwxrwx--- 2 root gpio    0 May  6 23:37 gpio11
    drwxrwx--- 2 root gpio    0 Jan  1  1970 gpiochip0
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 unexport
    

    Update

    Permissions are correct for individual pins as well:

    # ls -Ll /sys/class/gpio/gpio11/
    total 0
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 active_low
    drwxr-xr-x 3 root root    0 May  6 23:36 device
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 direction
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 edge
    drwxrwx--- 2 root gpio    0 May  6 23:37 subsystem
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 uevent
    -rwxrwx--- 1 root gpio 4096 May  6 23:37 value