【发布时间】:2019-11-08 20:16:45
【问题描述】:
我在 RHeL 服务器上有一个 root 用户帐户。在该服务器上,我有一个名为 user.sh 的简单脚本,它只返回当前用户:
#!/bin/bash
echo $USER
当从我的 root 帐户运行时,输出是
bash user.sh
>>>root
通过另一个脚本,我希望能够在不输入密码的情况下临时切换用户storing the password in the script or a file 或modifying /etc/sudoers 并执行user.sh,然后返回到我的初始root 帐户。这有可能吗?
这是我迄今为止尝试过的:
-
Using delimeters to execute a block of code
#!/bin/bash bash /user.sh su other_user <<EOF echo Current user: $USER EOF输出:
root Current user: root -
在 bash 中切换到用户,执行命令然后退出
#!/bin/bash bash /user.sh su other_user bash /user.sh exit输出:脚本暂停执行并将我返回到以
other_user登录的终端,但我仍将位于包含user.sh的根帐户目录中root [other_user@my_server]$如果我随后输入
exit,我将返回到我的根帐户并且脚本完成执行 -
#!/bin/bash bash /user.sh su - other_user -c /path/user.sh输出:
root -bash: /path/user.sh: Permission denied 使用
sudo -i -u other_user以用户身份登录并执行脚本,这会产生与尝试#2 相同的问题,但我被重定向到other_user的主目录。
值得注意的是,如果我使用方法 2,当我以 other_user 登录时,我可以运行 bash user.sh 并产生所需的输出:other_user
【问题讨论】: