bash sudo 给不同的用户并在 sudo 后立即运行命令,被动地(非交互式)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22107454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sudo to different user and run commands immediately post sudo, passively (non interactively )
提问by user1874594
Consider this sudo - I am logged in as jonhnd and run this through a script run1st.ksh that has the below command
考虑这个 sudo - 我以 jonhnd 身份登录并通过具有以下命令的脚本 run1st.ksh 运行它
sudo -iu bigadmin
This elevates my credentials to bigadmin a generic user but that user is a 'shared' user meaning - I really dont have any leeway to customize his personal profile After he is logged in ,I see his PATH variable is very skinny ,with just few paths added
这将我的凭据提升为 bigadmin 一个通用用户,但该用户是一个“共享”用户的意思 - 我真的没有任何余地来自定义他的个人资料 在他登录后,我看到他的 PATH 变量非常瘦,只有几条路径添加
echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/opt/dell/srvadmin/bin
so the pwd is not in the path and neither would be executable . No of those dir's in the $PATH is writable and like I said modifying any personal files of bigadmin isn't an option. I have a start up script I execute as that user for just my sessions and I have to force execute it
所以密码不在路径中,也不会是可执行的。$PATH 中的那些目录都不是可写的,就像我说的那样,修改 bigadmin 的任何个人文件都不是一种选择。我有一个启动脚本,我只在我的会话中以该用户身份执行,我必须强制执行它
. ./myscript
will work
将工作
myscript
. myscript
gives myscript not found error understandably because the path restrictions mentioned above. What I want to do is this run the sudo and myscript in run1st.ksh so that when I enter that bigadmin prompt myscript has already run @barmar thanks for helping . here is the o/p from running that ( put verbose on set -x )
由于上面提到的路径限制,可以理解为 myscript not found 错误。我想要做的是在 run1st.ksh 中运行 sudo 和 myscript,这样当我输入 bigadmin 提示时,myscript 已经运行 @barmar 感谢您的帮助。这是运行的 o/p(将详细放在 set -x 上)
++ sudo -iu bigadmin ksh -c '. ./run1st.ksh'
Usage: . [ options ] name [arg ...]
@Barmar
@巴马尔
sudo -iu bigadmin ksh -c '/home/bigadmin/run1st.ksh'
ksh: line 1: /home/bigadmin/run1st.ksh: not found
sudo -iu bigadmin ksh -c '. /home/biadmin/run1st.ksh'
age: . [ options ] name [arg ...]
$ sudo -iu bigadmin ksh -c '. /home/bigadmin; run1st.ksh'
+ sudo -iu bigadmin ksh -c '. /home/bigadmin; run1st.ksh'
Usage: . [ options ] name [arg ...]
$ sudo -iu bigadmin ksh -c '. /home/bigadmin/run1st.ksh ; run1st.ksh'
+ sudo -iu bigadmin ksh -c '. /home/bigadmin/run1st.ksh ; run1st.ksh'
Usage: . [ options ] name [arg ...]
does not work
不起作用
$ sudo -iu bigadmin /home/bigadmin/run1st.ksh
+ sudo -iu bigadmin /home/bigadmin/run1st.ksh
-nologin: line 1: /home/bigadmin/run1st.ksh: not found
It actually look for the run1st.ksh in johnd 's directory
它实际上在 johnd 的目录中寻找 run1st.ksh
$ sudo -iu bigadmin echo `pwd`
++ pwd
+ sudo -iu bigadmin echo /home/johnd
and even after copying it to johnd 's home- I get the SAME File not found command . So its looking for the file in specific PATH and not in the directory you put forth . Maybe tweak PATH in that sudo command ?
甚至在将它复制到 johnd 的家之后 - 我得到了 SAME File not found 命令。所以它在特定的 PATH 中寻找文件,而不是在你提出的目录中。也许在那个 sudo 命令中调整 PATH ?
there was a space in the filename xxx. ksh
that I did'nt notice. Fixed that and now it does not give file not found error BUT it executes it and returns back to the johnd prompt.
文件名xxx. ksh
中有一个我没有注意到的空格。修复了这个问题,现在它不会给出找不到文件的错误,但它会执行它并返回到 johnd 提示。
$ sudo -iu bigadmin /home/bigadmin/run1st.ksh
+ sudo -iu bigadmin /home/bigadmin/run1st.ksh
++ printf '3]0;%s@%s:%ssudo -iu bigadmin /path/to/myscript
7' johnd server400 '~'
$
回答by Barmar
You can do:
你可以做:
##代码##