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

标签 linux sudo sudoers

我有一个从 /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/

相关文章:

linux - 在 Linux 中运行永久进程时,我们需要 sleep() 吗?

bash - Linux - Bash 登录到机器然后成为 root

bash - 以受限用户身份运行 monit 并使其监视需要 root 权限的进程

ubuntu - 写入 sudoers 文件以允许 USER1 以 USER2 身份执行特定命令而无需密码

Linux: 'Username' 不在 sudoers 文件中。此事件将被报告

linux - 如何处理共享文件/区域的页面错误

linux - 如何将基于 OS X 的闭源应用程序移植到 Linux?

php - 添加到 sudoers.d 的脚本未以 root 身份执行

c - rpm 减小共享库的大小

linux - vim 和 sudo vim 在 openSUSE 上使用不同的设置