2

Problem:

How can I run a script owned by root with root permission as normal user?

Let's take for example a folder /path/to/folder. First I make sure that the folder and all content is owned by root:

sudo chown -R root:root /path/to/folder

Then I tried different approaches of giving permissions to other users:

sudo chmod -R +x /path/to/folder
sudo chmod -R u+s /path/to/folder
sudo chmod -R 4755 /path/to/folder

But the script I want to run, still gives me:

Can't open /dev/mem: Permission denied

Detailed

I have a Flask application which changes colours of an led-strip according to incoming requests.

Regardless of which library is used, accessing GPIO always needs root permission.

I would like to change this for two reasons:

  • I am using venv and running the app with sudo causes the globally installed python packaged to be used rather than the ones in venv.

  • In its finished state the application should run in apache. So far I didn't get apache to run the app with sudo.


I would like to focus on the main problem, rather than Flask or the led-strip library.

Thanks in advance!

Edit

I looked into the suggested question and my user is in the gpio group, the dev/gpiomem folder exists and has the exact same permissions and still I get the /dev/mem permission denied error.

Now I found out, that the package I am using apparently uses /dev/mem exclusively and doesn't support /dev/gpiomem. (https://github.com/jgarff/rpi_ws281x)

I'm now looking for an alternative and I am open for suggestions :-)

7
  • 1
    Instead, maybe check this. Nothing says you have to have root rights. // Using the setuid bit still makes the script run as root (except scripts cannot use setuid). I suggest you read up a little on Linux and chmod and whatnot.
    – Daniel B
    Commented Mar 23, 2022 at 18:45
  • You should never ever set the SUID bit on a script As Daniel pointed out you can allow your normal user to access GPIO just fine. Commented Mar 23, 2022 at 18:49
  • Thanks, to both of you - actually I already found the question but I didn't get it to work. I will take a closer look. And even though setting the suid bit is not recommended, I am interested what I missed. The owner is root, the executable and suid bit are set and still when I run my python gpio script I get the dev/mem permission denied error. Also tried this with a bash script and whoami -> echoes pi. Its a bit frustrating, every solution I found is either not working or not recommended...
    – ningelsohn
    Commented Mar 24, 2022 at 20:35
  • I updated my question, seems like the package I am using doesn't supports /dev/gpiomem
    – ningelsohn
    Commented Mar 24, 2022 at 21:13
  • Relevant? Commented Mar 24, 2022 at 21:27

0

You must log in to answer this question.

Browse other questions tagged .