2014-02-07 32 views
0

我有一個程序從/dev/i2c-1中讀取設備。這個軟件叫pa_access這裏有一個例子來看:sudo「-u」選項不起作用

[email protected]:~$ 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」選項指定的用戶,例如發生
問題:

[email protected]:~$ 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的情況下運行時發生)。

[email protected]:~$ 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腳本運行程序。

怎麼了?有任何想法嗎?提前致謝。

回答

0

當您使用sudo運行命令時,您正在有效地將用戶更改爲root。您需要成爲root才能訪問硬件。

Wikipedia

須藤...允許用戶運行與另一用戶(通常是超級用戶或根)的安全權限的程序。

編輯:您應該考慮以下的solution for this question。它建議構建一個可以在PHP中執行的二進制包裝器,並使用root特權運行該程序。

0

顯然,程序只能以root身份運行。 WWW-數據非常多不是根,這是很重要的一點。你可能想要讀的是setuid