简体   繁体   English

如何使用 sudo 作为另一个用户在 bash 子 shell 中执行一系列命令?

[英]How can I execute a series of commands in a bash subshell as another user using sudo?

I'm writing a bash script that needs to sudo multiple commands.我正在编写一个需要 sudo 多个命令的 bash 脚本。 I can do this:我可以做这个:

( whoami ; whoami )

but I can't do this:但我不能这样做:

sudo ( whoami ; whoami )

How do I solve this?我该如何解决这个问题?

Run a shell inside sudo : sudo bash -c 'whoami; whoami'sudo运行一个 shell: sudo bash -c 'whoami; whoami' 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).如果您真的想在该命令中使用单引号,请使用'\\'' (技术上是:结束单引号文字,文字'字符,开始单引号文字;但实际上这是一种注入单引号的方法在单引号文字字符串中)。

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, eg:这样就不需要摆弄正确的引用,特别是如果你有多个级别,例如:

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.) (请注意,您不能缩进内部终止符 - 它必须单独在一行中。如果您想在此处的文档中使用缩进,您可以使用<<-而不是<< ,但您必须使用制表符缩进, 不是空格。)

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 脚本而不是像上面的例子那样的单个命令组。

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"

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:使用'()'时不需要空格。

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 this told.So just worry about the passwd prompt at the beginning of your script,then you can use sudo through out.仅执行sudo询问你的passwd第一time.The passwd的回答有效期为约5分钟default.You可以改变这个值作为这个在脚本的开头told.So只是在passwd提示烦恼,那么你可以使用sudo始终。 changing Defaults:user_name timestamp_timeout's value to -1 may be a security hole on your system.将 Defaults:user_name timestamp_timeout 的值更改为 -1 可能是您系统上的安全漏洞。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM