Bash 以其他用户身份运行脚本-为什么权限被拒绝?
我正在以root用户身份运行一个脚本,该脚本使用Bash 以其他用户身份运行脚本-为什么权限被拒绝?,bash,permissions,file-permissions,Bash,Permissions,File Permissions,我正在以root用户身份运行一个脚本,该脚本使用useradd和passwd创建一个用户。然后,在运行需要作为新创建的用户运行的其他命令之前,我会进行一些检查/安装 因此,我有创建用户的主脚本: main.sh: #!/bin/bash useradd testuser passwd testuser yum -y install git chmod 777 testuser.sh su testuser -c ./testuserscript.sh testuserscript.sh: #
useradd
和passwd
创建一个用户。然后,在运行需要作为新创建的用户运行的其他命令之前,我会进行一些检查/安装
因此,我有创建用户的主脚本:
main.sh:
#!/bin/bash
useradd testuser
passwd testuser
yum -y install git
chmod 777 testuser.sh
su testuser -c ./testuserscript.sh
testuserscript.sh:
#!/bin/bash
git clone git://github.com/schacon/grit.git
当我运行时。/main.sh:我得到以下错误:
bash: ./testuserscript.sh: Permission denied
当我这样做时,我会得到以下信息:
-rwxrwxrwx. 1 root root 728 Jun 24 11:02 testuserscript.sh
我是否运行错误的命令来运行
testscript
,因为testuser
?权限被拒绝可能发生在以下情况:
#!
)后的路径不正确或未指向可执行文件(或者用户没有执行此脚本的权限)testuser.sh
,但随后尝试执行testuserscript.sh
我不希望su
更改文件夹,但可以尝试绝对路径。还要检查新创建的已使用的默认shell。要避免所有这些问题(以及其他一些问题),请尝试:
您可以稍后使用变量来定义所有脚本都在哪个文件夹中。您可以尝试
sudo-u testuser命令
是否在所有指向testuserscript.sh
的目录上都有+x?@thatotherguy是的。我一直尝试使用chmod 777,直到/tmp/。也不走运。@otherguy这两个脚本都在/tmpIs/tmp
中,并用noexec挂载?
sudo -u testuser bash /tmp/testuserscript.sh