Linux 不使用 sudo 访问 GPIO(树莓派的)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12133860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:11:02  来源:igfitidea点击:

Accessing the GPIO (of a raspberry pi) without ``sudo``

linuxsudoraspberry-pigpio

提问by Rakesh Pai

This question might not be specific to the raspberry pi, of course. Also, I'm relatively new to Linux.

当然,这个问题可能不是针对树莓派的。另外,我对 Linux 比较陌生。

I want to write a little library (in node.js, if that matters) to access the GPIO of the raspberry pi using the sysfs. However, accessing the sysfs requires sudo access, and that's bad for obvious reasons.

我想写一个小库(在 node.js 中,如果这很重要)来使用 sysfs 访问 raspberry pi 的 GPIO。但是,访问 sysfs 需要 sudo 访问权限,这显然很糟糕。

Quick2Wireseems to have a solution, but I want to understand it better and not just blindly use it. They've used C of course, but from what I understand, the code isn't complex, and probably can be pulled off with just bash, even if less elegantly. However, more than anything, I'm not sure whyit works.

Quick2Wire似乎有一个解决方案,但我想更好地理解它,而不是盲目地使用它。他们当然使用过 C,但据我所知,代码并不复杂,可能只用 bash 就可以完成,即使不那么优雅。然而,最重要的是,我不确定它为什么有效。

Any help will be great.

任何帮助都会很棒。

Edit: Thanks for the comments. It's clear I need to rephrase the question. Here goes: How is it that once installed (as root), the app doesn't require any more root perms to use? How does adding someone to a group help in this case? /sys/devices/virtual/gpioisn't the location where the gpio sysfs is available, so what's the trickery with that? I'm really a n00b, so these questions might be n00b-ish, so please bear with me.

编辑:感谢您的评论。很明显我需要重新表述这个问题。这是:一旦安装(以 root 身份),应用程序如何不再需要使用任何 root 权限?在这种情况下,将某人添加到群组有何帮助?/sys/devices/virtual/gpio不是 gpio sysfs 可用的位置,那有什么技巧呢?我真的是一个 n00b,所以这些问题可能是 n00b-ish,所以请耐心等待。

采纳答案by Harry Braviner

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

Rakesh,我只是想弄清楚完全相同的事情,我想我已经解决了。

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

您根本不需要了解很多 makefile。重要的几行如下,运行时在bash中执行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 gpiocreates a system group called gpio. chgrp gpio $(DESTDIR)/bin/gpio-adminchanges the group of the binary (which the C file gpio-admin.c was compiled to) to gpio. The owner of the binary is still root (since you're running make as root.) chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admindoes two important things. Firstly, it lets a member of the gpio group run gpio-admin. Secondly, it sets the setuid bit on gpio-admin.

groupadd -f --system gpio创建一个名为 gpio 的系统组。chgrp gpio $(DESTDIR)/bin/gpio-admin将二进制文件的组(C 文件 gpio-admin.c 编译到该组)更改为 gpio。二进制文件的所有者仍然是 root(因为您以 root 身份运行 make。)chmod u=rwxs,g=rx,o= $(DESTDIR)/bin/gpio-admin做了两件重要的事情。首先,它让 gpio 组的成员运行 gpio-admin。其次,它在 gpio-admin 上设置 setuid 位。

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

当您将自己添加到 gpio 组时,您可以运行 gpio-admin,而无需使用 sudo,但 gpio admin 会表现得就像在 sudo 下运行一样。这允许它写入 /sys/class/gpio/export 文件。它还允许它更改创建的文件 /sys/class/gpio/gpio[pin number]/direction 等的所有者。

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

即使您将 /sys/class/gpio/export 的组更改为 gpio,并设置允许您写入的权限

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 superuser powers

您可以在没有超级用户权限的情况下导出引脚

echo 22 > /sys/class/gpio/export

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

但是文件 /sys/class/gpio/gpio22/direction 等仍将以 root 作为所有者和组创建,您需要使用 sudo 来更改它们。此外,每次重新启动后,导出和取消导出文件的所有权将恢复为 root。