【问题标题】:sudo "-u" option not worksudo "-u" 选项不起作用
【发布时间】:2014-02-07 18:05:21
【问题描述】:

我有一个从/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 脚本运行程序。

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

【问题讨论】:

    标签: linux sudo sudoers


    【解决方案1】:

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

    来自Wikipedia

    sudo...允许用户以其他用户(通常是超级用户或 root)的安全权限运行程序。

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

    【讨论】:

      【解决方案2】:

      显然,该程序只能以 root 身份运行。 www-data 非常不是根,这就是重点。您可能想阅读的是setuid

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-02
        • 2015-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多