1

Within a small project I opened a wireless connection using the NRF24L01 transceiver and a Raspberry Pi 3 Model B+ with Raspbian 10. To communicate over the transceiver I make use of the RF24 library of tmrh20 (https://github.com/tmrh20/RF24.git).

In the RF24 folder is an example application called gettingstarted that has to be created using make. Also, SPI has to be enabled in Raspbian. When running the application gettingstarted I have to use sudo and for some reason it will not let me run the application with the local user.

Example of it working:

sudo ./gettingstarted

Example of it not working:

./gettingstarted

Error message:

pi@raspberrypi:~/Desktop/RF24/examples_linux $ ./gettingstarted
RF24/examples/GettingStarted/
terminate called after throwing an instance of 'std::runtime_error'
  what():  Process should run as root

What must be done to allow the local user (user "pi" in my case) to run gettingstarted?

  • This is not a Pi issue, but incompetence in the code - which doesn't even have any documentation – Milliways Jul 14 at 11:43
  • 1
    @Milliways Where would you recommend to get the answer from? Is this a C++ issue? Do you mean the missing documentation in the RF24 Git repository of tmrh20? – Socrates Jul 14 at 12:20
  • 1
    Ordinarily a user has to be a member of the 'spi' group to use SPI devices, but the pi user is a member of that group in Raspberry Pi OS (it's a default ... unless the group membership was edited. Do 'grep spi /etc/group' and confirm you see the pi user listed.) Alternatively, you run it as root, then use 'lsof -p <process ID>' and see what files and devices are being accessed by it ... then check group ownerships and permissions on those and add the pi user to any needed groups. – Tim Campbell Jul 14 at 14:39
  • @TimCampbell Thanks for the very accurate answer, Tim! The approach searching /etc/group showed user pi in the group spi. The second approach with lsof looks promising. The ./gettingstarted application though only runs for a millionth second. Is there a way to run it with a monitor? i.e. `lsof --run-command 'sudo ./gettingstarted' and then view what users / groups had to run to execute the command? – Socrates Jul 14 at 21:30
1

Nope you have to run as root if you don't want to use sudo you could just do sudo bash and then run your commands. This will put you into the root user in the terminal.

Alternatively, you could create a shell script with just sudo ./gettingstarted, I don't think you will need to run sudo in front of that, but could be wrong.

There might be a way to upgrade the pi account to sudo, however, it's probably not recommended.

| improve this answer | |
  • Upgrading the pi-user to sudo certainly works, but don't want to give that user all the rights possible. What part exactly requires the root user? Usually it is a group that can be simply added to the user with sudo usermod -a-G mygroup myuser. – Socrates Jul 14 at 11:11
  • It's because something you're accessing in the program requires root. Tbh why can't you just use sudo? – dinomite59 Jul 14 at 12:45
  • In a playground scenario it is ok to use root for just about anything. But in a more professional approach user management is very important for security reasons. Hence, only allow what is necessary to run the application, and forbid everything else. There has to be a way to execute the code without being root. I am though not sure whether this has to be solved within Raspbian or within C++. – Socrates Jul 14 at 13:04
  • @dinomite59 running sudo bash won't put your root user in terminal, but run the script with elevated privileges. running sudo -i will elevate to the root user. – Swedgin Jul 14 at 13:14
1

You used make to create gettingstarted so I assume it is compiled to an executable file and not only a script for an interpreter (bash, python, etc.). Having this you are able to use the setuid or setgid flags on the file. This flags on file permission are used to execute the file with the rights of its owner or group and not with the permission of the executing user (user pi) by default.

Have a look at Wikipedia - setuid and find what setting is appropriate for you.

But be aware that using setuid/setgid have important security risks if the executable isn't proper designed. You will find many warnings about this on the web.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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