bash sudo echo "something" >> /etc/privilegedFile 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/84882/
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 echo "something" >> /etc/privilegedFile doesn't work
提问by David
This is a pretty simple question, at least it seems like it should be, about sudo permissions in Linux.
这是一个非常简单的问题,至少看起来应该是,关于 Linux 中的 sudo 权限。
There are a lot of times when I just want to append something to /etc/hosts
or a similar file but end up not being able to because both >
and >>
are not allowed, even with root.
有很多的时候,我只是想将一些东西附加到/etc/hosts
或类似的文件,但最终没有能够因为两者>
并>>
不准,甚至有根。
Is there someway to make this work without having to su
or sudo su
into root?
有没有办法使这项工作无需su
或sudo su
进入根?
回答by Yoo
Use tee --append
or tee -a
.
使用tee --append
或tee -a
。
echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list
Make sure to avoid quotes inside quotes.
确保避免引号内的引号。
To avoid printing data back to the console, redirect the output to /dev/null.
为避免将数据打印回控制台,请将输出重定向到 /dev/null。
echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list > /dev/null
Remember about the (-a
/--append
) flag!
Just tee
works like >
and will overwrite your file. tee -a
works like >>
and will write at the end of the file.
记住 ( -a
/ --append
) 标志!就像tee
工作一样>
,会覆盖你的文件。tee -a
像>>
并且会写在文件的末尾。
回答by Matt P
The problem is that the shell does output redirection, not sudo or echo, so this is being done as your regular user.
问题是 shell 执行输出重定向,而不是 sudo 或 echo,所以这是作为您的普通用户完成的。
Try the following code snippet:
尝试以下代码片段:
sudo sh -c "echo 'something' >> /etc/privilegedfile"
回答by Incident
The issue is that it's your shell that handles redirection; it's trying to open the file with yourpermissions not those of the process you're running under sudo.
问题是你的 shell 处理重定向;它试图用您的权限打开文件,而不是您在 sudo 下运行的进程的权限。
Use something like this, perhaps:
使用这样的东西,也许:
sudo sh -c "echo 'something' >> /etc/privilegedFile"
回答by Vinko Vrsalovic
sudo sh -c "echo 127.0.0.1 localhost >> /etc/hosts"
回答by agnul
Doing
正在做
sudo sh -c "echo >> somefile"
should work. The problem is that > and >> are handled by your shell, not by the "sudoed" command, so the permissions are your ones, not the ones of the user you are "sudoing" into.
应该管用。问题在于 > 和 >> 由您的 shell 处理,而不是由“sudoed”命令处理,因此权限是您的,而不是您“sudo”进入的用户的权限。
回答by msanford
I would note, for the curious, that you can also quote a heredoc (for large blocks):
我会注意到,出于好奇,您还可以引用一个 heredoc(对于大块):
sudo bash -c "cat <<EOIPFW >> /etc/ipfw.conf
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<plist version=\"1.0\">
<dict>
<key>Label</key>
<string>com.company.ipfw</string>
<key>Program</key>
<string>/sbin/ipfw</string>
<key>ProgramArguments</key>
<array>
<string>/sbin/ipfw</string>
<string>-q</string>
<string>/etc/ipfw.conf</string>
</array>
<key>RunAtLoad</key>
<true></true>
</dict>
</plist>
EOIPFW"
回答by Vytenis Bivainis
In bash you can use tee
in combination with > /dev/null
to keep stdout clean.
在 bash 中,您可以tee
与 结合使用> /dev/null
以保持标准输出干净。
echo "# comment" | sudo tee -a /etc/hosts > /dev/null
回答by hololeap
Using Yoo's answer, put this in your ~/.bashrc
:
使用Yoo 的回答,将其放入您的~/.bashrc
:
sudoe() {
[[ "$#" -ne 2 ]] && echo "Usage: sudoe <text> <file>" && return 1
echo "" | sudo tee --append "" > /dev/null
}
Now you can run sudoe 'deb blah # blah' /etc/apt/sources.list
现在你可以运行 sudoe 'deb blah # blah' /etc/apt/sources.list
Edit:
编辑:
A more complete version which allows you to pipe input in or redirect from a file and includes a -a
switch to turn off appending (which is on by default):
一个更完整的版本,它允许您通过管道输入文件或从文件重定向,并包含一个-a
关闭附加的开关(默认情况下打开):
sudoe() {
if ([[ "" == "-a" ]] || [[ "" == "--no-append" ]]); then
shift &>/dev/null || local failed=1
else
local append="--append"
fi
while [[ $failed -ne 1 ]]; do
if [[ -t 0 ]]; then
text=""; shift &>/dev/null || break
else
text="$(cat <&0)"
fi
[[ -z "" ]] && break
echo "$text" | sudo tee $append "" >/dev/null; return $?
done
echo "Usage: echo 'Add this line' | sudo sponge -a privfile
[-a|--no-append] [text] <file>"; return 1
}
回答by Michael Goldshteyn
You can also use sponge
from the moreutils
package and not need to redirect the output (i.e., no tee
noise to hide):
您还可以sponge
从moreutils
包中使用而不需要重定向输出(即,没有tee
隐藏的噪音):
回答by Sudev Shetty
echo 'Hello World' | (sudo tee -a /etc/apt/sources.list)
echo '你好世界' | (sudo tee -a /etc/apt/sources.list)