I am attempting to get a c++ program to access the gpio as non-root. I am on a raspi 2 running Jessie. I followed the instructions on issue: Access GPIO pins without root. No access to /dev/mem. Try running as root! For some reason, it still doesn't work after adding user to the gpio group and ensuring the correct permissions/owners. It says

can't open /dev/mem: : Permission denied

Am i supposed to change permissions to /dev/mem? I've read that it is a security concern so I would like to avoid it. The only think i didn't do was update the kernel to 4.4 (at 4.1.19-v7+) since it looks like it can cause issues with some drivers. Maybe i should risk that?

Any insight would be helpful.

1 Answer 1


You need root privileges to access /dev/mem.

To allow access to the GPIO a new device was created called /dev/gpiomem.

/dev/gpiomem can be accessed by members of the gpio group and grants access to the memory used by the GPIO.

I give example code at http://abyz.me.uk/rpi/pigpio/examples.html#Misc_tiny_gpio

  • I have been using the library here: github.com/hzeller/rpi-rgb-led-matrix . It turns out this library maps to the hardware directly, so this won't fix my issue. But i think it answers the question i posted. It turns out I need to use setresgid() and setresuid() to make my code work without root access.
    – Cole
    May 8, 2016 at 4:18
  • @Cole You can't make your program access /dev/mem without root privileges. That's just another way of granting your program root privileges. It may be simpler just to set the program suid. I.e. have the program owned by root and set the s bit, e.g. chown root.root prog; sudo chmod +s prog.
    – joan
    May 8, 2016 at 7:43
  • @Cole I have the same issue. How do you use setresgid() and setresuid()?
    – 0__
    Jun 18, 2021 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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