If you want to access a user space GPIO chip in Linux with out root (or sudo) privileges then the device needs to not be created with root access controls. One way to reduce the access level required for a device is to write a custom udev rule.

Here we are going to try and take a GPIO device at /dev/gpiochip0 and mount it with the group gpio.

Creating the Group

Assuming the gpio group does not already exist on your system it is simple to do so.

$ sudo groupadd --system gpio
$ cat /etc/group | grep gpio

Note your gpio group may be different than 998. This is of no actual consequence.

The last thing you might want to do is add a user to this new gpio group. For instance to add the current user to the new gpio group you could do this.

sudo usermod -a -G gpio $USER

The user will need to log out and log back in order to make use of being part of this new group.

Query udev database for device information

To find out information about the GPIO chip the system is detecting use the udevadm command. In this example the GPIO device is located at /dev/gpiochip0.

$ udevadm info -q all -n /dev/gpiochip0
P: /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/gpiochip0
N: gpiochip0
L: 0
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/gpiochip0
E: DEVNAME=/dev/gpiochip0
E: MAJOR=254

Once you know some information about the GPIO device that information can be used to write a udev rule to match that device.

Writing a udev matcher and rule


This is a simple matcher that matches anything that is the gpio SUBSYSTEM. It names it gpiochip0, gpiochip1, and so on. Assigns the owner as root. The group as the new gpio group. Finally makes it so that the owner and group both have read/write privileges.

Save rule and apply

After writing the matcher it is time to write the new rule. This command for example will create a new file at /etc/udev/rules.d/99-gpio.rules containing the matcher designed previously.

sudo tee "/etc/udev/rules.d/99-gpio.rules" > /dev/null <<'EOF'

Finally it is time to reload the udev rules udevadm control --reload.

$ ls -alh /dev/gpiochip0
crw-rw---- 1 root gpio 254, 0 Mar 27 21:23 /dev/gpiochip0