[英]Sourcing a shell script, while running with sudo
我想编写一个设置脚本库的shell脚本,并允许“开发人员”组中的所有用户执行此脚本。
该脚本归用户“ hg”所有,运行后运行良好。 当我尝试使用sudo与另一个用户运行它时,问题就来了,当它尝试获取另一个文件时,执行由于“权限被拒绝”错误而暂停。
有问题的脚本文件:
create_repo.sh
#!/bin/bash
source colors.sh
REPOROOT="/srv/repository/mercurial/"
... rest of the script ....
create_repo.sh和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
我要运行的是:
user@nebu:~$ 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)
user@nebu:~$ 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
user@nebu:~$ sudo -u hg /home/hg/scripts/create_repo.sh
/home/hg/scripts/create_repo.sh: line 3: colors.sh: Permission denied
因此该脚本已执行,但是在尝试包含其他脚本时暂停。
我也尝试过使用:
user @ nebu:〜$ sudo -u hg / bin / bash /home/hg/scripts/create_repo.sh
给出相同的结果。
如果脚本可以通过sudo与其他用户一起运行,则包含另一个Shell脚本的正确方法是什么?
脚本可能会尝试在当前目录中尝试获取source
文件colors.sh
并失败,因为该脚本由于sudo
而没有读取当前目录的权限。
尝试使用source /home/hg/scripts/colors.sh
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.