[ACCEPTED]-Accessing the GPIO (of a raspberry pi) without ``sudo``-gpio

Accepted answer
Score: 21

Rakesh, I've just been trying to figure 28 out exactly the same thing, and I think 27 I've solved it.

You don't need to understand 26 much of the makefile at all. The important 25 lines are the following, which are executed 24 in bash when you run sudo make install

install: install-files
    groupadd -f --system gpio
    chgrp gpio $(DESTDIR)/bin/gpio-admin
    chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin

groupadd -f --system gpio creates a system group 23 called gpio. chgrp gpio $(DESTDIR)/bin/gpio-admin changes the group of the binary 22 (which the C file gpio-admin.c was compiled 21 to) to gpio. The owner of the binary is 20 still root (since you're running make as 19 root.) chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin does two important things. Firstly, it 18 lets a member of the gpio group run gpio-admin. Secondly, it 17 sets the setuid bit on gpio-admin.

When you 16 add yourself to the gpio group, you can 15 run gpio-admin, without using sudo, but 14 gpio admin will act like it is being run 13 under sudo. This allows it to write to the 12 /sys/class/gpio/export file. It also allows 11 it to change the owner of the files /sys/class/gpio/gpio[pin 10 number]/direction etc. that get created.

Even 9 if you change the group of /sys/class/gpio/export 8 to gpio, and set permissions to allow you 7 to write to it

sudo chgrp gpio /sys/class/gpio/export /sys/class/gpio/unexport
sudo chmod g+rwx /sys/class/gpio/export /sys/class/gpio/unexport

you can export a pin without 6 superuser powers

echo 22 > /sys/class/gpio/export

but the files /sys/class/gpio/gpio22/direction 5 etc. will still be create with root as the 4 owner and group, and you'll need to use 3 sudo to change them. Also, ownership of 2 the export and unexport files will revert 1 to root after each reboot.

More Related questions