sudo echo“某事” >>> /etc/privilegedFile 不起作用
这是一个关于 Linux 中 sudo 权限的非常简单的问题,至少看起来应该如此。
很多时候,我只想将某些内容附加到 /etc/hosts
或类似文件,但最终无法做到,因为 >
和 即使拥有 root 权限,>>>
也是不允许的。
有没有办法让这项工作无需 su
或 sudo su
进入 root ?
This is a pretty simple question, at least it seems like it should be, about sudo permissions in Linux.
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.
Is there someway to make this work without having to su
or sudo su
into root?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
您还可以使用
moreutils
包中的sponge
,而不需要重定向输出(即,无需隐藏tee
噪音):You can also use
sponge
from themoreutils
package and not need to redirect the output (i.e., notee
noise to hide):通过使用 sed -i 和 $ a ,您可以附加文本,其中包含最后一行之后的变量和特殊字符。
例如,将 $NEW_HOST 和 $NEW_IP 添加到 /etc/hosts:
sed 选项解释:
By using sed -i with $ a , you can append text, containing both variables and special characters, after the last line.
For example, adding $NEW_HOST with $NEW_IP to /etc/hosts:
sed options explained:
回声“你好世界”| (sudo tee -a /etc/apt/sources.list)
echo 'Hello World' | (sudo tee -a /etc/apt/sources.list)
怎么样:
回显文本| sudo dd status=无=特权文件
我想更改/proc/sys/net/ipv4/tcp_rmem。
我做了:
sudo dd status=none of=/proc/sys/net/ipv4/tcp_rmem <<<"4096 131072 1024000"
消除单行文档的回声
How about:
echo text | sudo dd status=none of=privilegedfile
I want to change /proc/sys/net/ipv4/tcp_rmem.
I did:
sudo dd status=none of=/proc/sys/net/ipv4/tcp_rmem <<<"4096 131072 1024000"
eliminates the echo with a single line document
这对我有用:
原始命令
工作命令
This worked for me:
original command
Working command
您可以更改文件的所有权,然后在使用
cat >>
追加后将其更改回来吗?像这样的东西对你有用吗?
Can you change the ownership of the file then change it back after using
cat >>
to append?Something like this work for you?
使用 Yoo 的答案,将其放入您的
~/.bashrc
中:现在您可以运行
sudoe 'deb blah # blah' /etc/apt/sources.list
编辑:
一个更完整的版本,允许您通过管道输入文件或从文件重定向,并包含
-a
开关关闭附加(默认情况下处于打开状态):Using Yoo's answer, put this in your
~/.bashrc
:Now you can run
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):在 bash 中,您可以将
tee
与> 结合使用。 /dev/null
保持标准输出干净。In bash you can use
tee
in combination with> /dev/null
to keep stdout clean.一些用户在使用多条线路时不知道解决方案。
Some user not know solution when using multiples lines.
我想指出,出于好奇,您还可以引用heredoc(对于大块):
I would note, for the curious, that you can also quote a heredoc (for large blocks):
做事
应该有效果。 问题是> 和>> 由您的 shell 处理,而不是由“sudoed”命令处理,因此权限是您的权限,而不是您“sudoing”的用户的权限。
Doing
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 负责处理重定向。 它尝试使用您的权限打开文件,而不是您在 sudo 下运行的进程的权限。
也许使用这样的东西:
The issue is that it's your shell that handles redirection; it's trying to open the file with your permissions not those of the process you're running under sudo.
Use something like this, perhaps:
问题是 shell 确实输出重定向,而不是 sudo 或 echo,因此这是以普通用户身份完成的。
尝试以下代码片段:
The problem is that the shell does output redirection, not sudo or echo, so this is being done as your regular user.
Try the following code snippet:
使用
tee --append
或tee -a
。确保避免引号内引号。
为了避免将数据打印回控制台,请将输出重定向到 /dev/null。
请记住 (
-a
/--append
) 标志!只是
tee
的工作方式与>
类似,并且会覆盖您的文件。tee -a
的工作方式与>>
类似,并将写入文件末尾。Use
tee --append
ortee -a
.Make sure to avoid quotes inside quotes.
To avoid printing data back to the console, redirect the output to /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.