2011-01-07 54 views
0

我想編寫一個設置mercurial存儲庫的shell腳本,並允許組「 」開發人員「的所有用戶執行此腳本。在使用sudo運行時找到shell腳本

該腳本歸用戶「hg」所有,並且在運行時工作正常。當我嘗試運行它與其他用戶 ,使用sudo時,執行暫停與「權限被拒絕」錯誤,當它試圖源另一個文件時出現問題。

有問題的腳本文件:

create_repo.sh create_repo.sh的

#!/bin/bash 

source colors.sh 

REPOROOT="/srv/repository/mercurial/" 
... rest of the script .... 

權限和colors.sh:

-rwxr--r-- 1 hg hg 551 2011-01-07 10:20 colors.sh 
-rwxr--r-- 1 hg hg 1137 2011-01-07 11:08 create_repo.sh 

sudoers的設置:

%developer ALL = (hg) NOPASSWD: /home/hg/scripts/create_repo.sh 

我的「 m試圖運行:

[email protected]:~$ id 
uid=1000(user) gid=1000(user) groups=4(adm),20(dialout),24(cdrom),46(plugdev),105(lpadmin),113(sambashare),116(admin),1000(user),1001(developer) 

[email protected]:~$ sudo -l 
Matching Defaults entries for user on this host: 
    env_reset 

User user may run the following commands on this host: 
    (ALL) ALL 
    (hg) NOPASSWD: /home/hg/scripts/create_repo.sh 

[email protected]:~$ sudo -u hg /home/hg/scripts/create_repo.sh 
/home/hg/scripts/create_repo.sh: line 3: colors.sh: Permission denied 

所以腳本被執行,但在試圖包含其他腳本時暫停。

我也嘗試使用:

用戶@ NEBU:〜$ sudo的-u HG /斌/慶典/home/hg/scripts/create_repo.sh

可以得到相同的結果。

如果可以通過sudo使用其他用戶運行腳本,那麼包含另一個shell腳本的正確方法是什麼?

+0

您的權限看起來不對。每個有權讀取的人都應該可執行腳本。如果一個腳本只是來源並且從不執​​行,它不應該是可執行的:`chmod a + x create_repo.sh; chmod a-x colors.sh` – 2011-01-12 16:23:09

+0

原因''create_repo.sh''沒有''a + x'',因爲它打算使用''sudo -u hg''運行。 colors.sh沒有爲其他人設置的執行位,不需要刪除它。 – 2011-01-15 21:55:16

回答

1

可能發生的情況是,該腳本嘗試在當前目錄中嘗試sourcecolors.sh文件並失敗,因爲它沒有權限讀取當前目錄,原因是sudo

嘗試使用source /home/hg/scripts/colors.sh