树莓派上的/ dev / mem访问被拒绝 [英] /dev/mem access denied on raspberry pi

查看:375
本文介绍了树莓派上的/ dev / mem访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Raspberry Pi,并且正在编写一个cgi python脚本,该脚本创建一个网页来控制我的gpio输出引脚。当我尝试将RPi.GPIO导入为GPIO时,我的脚本崩溃了。这是我得到的错误:

I am working with my Raspberry Pi and I am writing a cgi python script that creates a webpage to control my gpio out pins. My script crashes when I try to import RPi.GPIO as GPIO. This is the error that I am getting:

File "./coffee.py", line 7, in <module>
    import RPi.GPIO as GPIO
RuntimeError: No access to /dev/mem.  Try running as root!

当我使用sudo运行脚本时,但是从URL运行时,我的代码运行良好从我的apache2服务器上说我无权访问/ dev / mem。我已经尝试编辑visudo了,但是没有用。这是我的visudo文件的样子:

My code works perfectly when I use sudo to run my script, but when I am running from a URL from my apache2 server it says that I do not have access to /dev/mem. I have already tried editing visudo and that did not work. This is what my visudo file looks like:

#includedir /etc/sudoers.d
pi ALL=(ALL) NOPASSWD: ALL
www-data ALL=(root) NOPASSWD: /usr/bin/python3 /usr/lib/cgi-bin/coffee.py *
apache2 ALL = (root) NOPASSWD: /usr/lib/cgi-bin/coffee.py

我可以通过任何方式运行脚本从URL调用中获取根?有人可以告诉我我在做什么错吗?

There any way that I can run my script as root from a URL call? Can anyone tell me what I am doing wrong?

推荐答案

我发现将www-data添加到gpio用户组可以正常工作:

I found that adding www-data to the gpio user group worked fine:

sudo usermod -aG gpio www-data

还可以将www-data添加到内存用户组:

You can also add www-data to the memory user group:

sudo usermod -aG kmem www-data

如上所述,这是一个坏主意,但对我来说是有必要的

As mentioned, it is a bad idea, but for me it was necessary.

这篇关于树莓派上的/ dev / mem访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆