如何使用 sudo 作为另一个用户在 bash 子 shell 中执行一系列命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3435312/
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
How can I execute a series of commands in a bash subshell as another user using sudo?
提问by qrest
I'm writing a bash script that needs to sudo multiple commands. I can do this:
我正在编写一个需要 sudo 多个命令的 bash 脚本。我可以做这个:
( whoami ; whoami )
but I can't do this:
但我不能这样做:
sudo ( whoami ; whoami )
How do I solve this?
我该如何解决这个问题?
回答by Gilles 'SO- stop being evil'
Run a shell inside sudo
:
sudo bash -c 'whoami; whoami'
在里面运行一个 shell sudo
:
sudo bash -c 'whoami; whoami'
You can use any character except '
itself inside the single quotes. If you really want to have a single quote in that command, use '\''
(which technically is: end single-quote literal, literal '
character, start single-quoted literal; but effectively this is a way to inject a single quote in a single-quoted literal string).
您可以'
在单引号内使用除自身之外的任何字符。如果您真的想在该命令中使用单引号,请使用'\''
(技术上是:结束单引号文字,文字'
字符,开始单引号文字;但实际上这是一种在单引号中注入单引号的方法文字串)。
回答by Maxim Egorushkin
You can pass the commands as standard input into sudo'ed bash with a here document:
您可以使用此处的文档将命令作为标准输入传递到 sudo'ed bash 中:
sudo bash <<"EOF"
whoami
id
EOF
This way there is no need to fiddle with correct quoting, especially if you have multiple levels, e.g.:
这样就不需要摆弄正确的引用,特别是如果你有多个级别,例如:
sudo bash <<"EOF"
whoami
echo $USER ~
sudo -u apache bash <<"DOF"
whoami
echo $USER ~
DOF
EOF
Produces:
产生:
root
root /root
apache
apache /usr/share/httpd
(Note that you can't indent the inner terminator —?it has to be alone on its line. If you want to use indentation in a here document, you can use <<-
instead of <<
, but then you must indent with tabs, not spaces.)
(请注意,您不能缩进内部终止符 —?它必须单独在一行中。如果您想在此处的文档中使用缩进,您可以使用<<-
代替<<
,但是您必须使用制表符而不是空格缩进。 )
回答by Znik
for example try this, I tested it:
例如试试这个,我测试了它:
sudo bash -c "cd /;ls;ls|grep o"
In this example you first change dir to /root, next list root directory and finally for root directory filter only directories having name with letter 'o'.
在本例中,您首先将 dir 更改为 /root,接下来列出根目录,最后为根目录过滤仅名称带有字母 'o' 的目录。
But i thing better way is writting script that do all you need and give exitcode for all complex action. Then you can sudo script instead group of single commands like example above.
但我更好的方法是编写脚本来完成您需要的所有操作并为所有复杂操作提供退出代码。然后你可以 sudo 脚本而不是像上面的例子那样的单个命令组。
回答by Chaitu Nookala
If you would like to get syntax highlighting from your editor, not use quotes around your code, and have proper indentation, you can write your commands in a function and send it to bash using the declare
command:
如果您想从编辑器中突出显示语法,而不是在代码周围使用引号,并有适当的缩进,您可以在函数中编写命令并使用以下declare
命令将其发送到 bash :
function run_as_root() {
whoami
id
echo $USER
}
sudo bash -c "$(declare -f run_as_root); run_as_root"
回答by kit.yang
The Brackets means that execute the command in a new bash.It execute the command with the interval of semicolon.Just use the code below instead.
括号表示在新的bash中执行命令。它以分号为间隔执行命令。只需使用下面的代码。
(sudo whoami;sudo whoami)
BYW:the space is not necessary when using '()'.
BYW:使用'()'时不需要空格。
回答by schemacs
sudo only asks for your passwd the first time.The passwd answered is valid for about 5 minutes by default.You can change this value as thistold.So just worry about the passwd prompt at the beginning of your script,then you can use sudothrough out. changing Defaults:user_name timestamp_timeout's value to -1 may be a security hole on your system.
仅执行sudo询问你的passwd第一time.The passwd的回答有效期为约5分钟default.You可以改变这个值作为这个在脚本的开头told.So只是在passwd提示烦恼,那么你可以使用sudo的始终。将 Defaults:user_name timestamp_timeout 的值更改为 -1 可能是您系统上的一个安全漏洞。