gpt4 book ai didi

linux - sudo "-u"选项不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 04:04:51 25 4
gpt4 key购买 nike

我有一个从 /dev/i2c-1 读取设备的程序。该软件名为 pa_access,下面是一个运行示例:

ubuntu@arm:~$ sudo ./pa_access

Input [0]: 0
Input [1]: 0
Input [2]: 0
Input [3]: 0
Input [4]: 0
Input [5]: 0
Input [6]: 0
Input [7]: 0
Input [8]: 0
Input [9]: 0
Input [10]: 0
Input [11]: 0
Input [12]: 0
Input [13]: 0
Input [14]: 0
Input [15]: 0
Input [16]: 0
Input [17]: 0
Input [18]: 0
Input [19]: 0
Input [20]: 0
Input [21]: 0
Input [22]: 0
Input [23]: 0
Input [24]: 0
Input [25]: 0
Input [26]: 1
Input [27]: 0
Input [28]: 0
Input [29]: 0

如您所见,该软件读取一组硬件输入,然后打印出输入状态。因此,必须使用 sudo 执行该程序。
当用户由“-u”选项指定时,就会出现问题,例如:

ubuntu@arm:~$ sudo -u www-data ./pa_access

Error setting comunication with PA board on /dev/i2c-1.
Input [0]: 0
Input [1]: 224
Input [2]: 252
Input [3]: 182
Input [4]: 169
Input [5]: 138
Input [6]: 0
Input [7]: 0
Input [8]: 79
Input [9]: 146
Input [10]: 0
Input [11]: 0
Input [12]: 188
Input [13]: 187
Input [14]: 246
Input [15]: 182
Input [16]: 33
Input [17]: 146
Input [18]: 0
Input [19]: 0
Input [20]: 0
Input [21]: 0
Input [22]: 0
Input [23]: 0
Input [24]: 0
Input [25]: 160
Input [26]: 246
Input [27]: 182
Input [28]: 0
Input [29]: 0

程序显示错误消息,因为它没有访问设备文件的权限(与没有 sudo 运行时发生的错误相同)。
如果尝试使用登录用户,则会出现相同的错误:

ubuntu@arm:~$ sudo -u ubuntu ./pa_access

Error setting comunication with PA board on /dev/i2c-1.
Input [0]: 0
Input [1]: 224
Input [2]: 252
...
...
Input [26]: 246
Input [27]: 182
Input [28]: 0
Input [29]: 0

但是将 root 作为 -u 的参数传递,工作正常
需要指定用户,因为我想从 PHP 脚本运行该程序。

怎么了?有任何想法吗?提前致谢。

最佳答案

当您使用 sudo 运行命令时,您实际上将用户更改为 root。您需要成为root才能访问硬件。

来自Wikipedia :

sudo...allows users to run programs with the security privileges of another user (normally the superuser, or root).

编辑:您应该考虑遵循solution for this question 。它建议构建一个可以在 PHP 中执行的二进制包装器,并使用 root 权限运行该程序。

关于linux - sudo "-u"选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21634745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com