【问题标题】:Running a script as a different user- why is permission denied?以其他用户身份运行脚本 - 为什么权限被拒绝?
【发布时间】:2014-08-14 22:59:57
【问题描述】:

我正在以 root 身份运行一个脚本,该脚本使用 useraddpasswd 创建一个用户。然后在运行一些需要以新创建的用户身份运行的其他命令之前进行一些检查/安装。

所以我有创建用户的主脚本:

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

【问题讨论】:

  • 你可以试试sudo -u testuser command
  • 你在testuserscript.sh之前的所有目录上都有+x吗?
  • @thatotherguy 是的。我尝试使用 chmod 777 一直到 /tmp/。也没有运气。
  • @thatotherguy 两个脚本都在 /tmp
  • /tmp 是否安装了 noexec?

标签: bash permissions file-permissions


【解决方案1】:

权限被拒绝可能发生在以下情况:

  1. 脚本对调用它的用户没有执行权限
  2. hash-bang (#!) 后的路径不正确或未指向可执行文件(或用户无权执行此脚本)。
  3. 该用户没有所有指向该脚本的文件夹的执行权限。

在您的代码中,您将 chmod 777 设置为 testuser.sh,但随后您尝试执行 testuserscript.sh

我不希望 su 更改文件夹,但可能会尝试绝对路径。还要检查新创建的使用的默认外壳。为了规避所有这些(以及其他一些问题),请尝试:

sudo -u testuser bash /tmp/testuserscript.sh

您可以稍后使用变量来定义所有脚本所在的文件夹。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多