sudo echo“某事” >>> /etc/privilegedFile 不起作用

发布于 2024-07-04 08:51:42 字数 255 浏览 5 评论 0原文

这是一个关于 Linux 中 sudo 权限的非常简单的问题,至少看起来应该如此。

很多时候,我只想将某些内容附加到 /etc/hosts 或类似文件,但最终无法做到,因为 >即使拥有 root 权限,>>> 也是不允许的。

有没有办法让这项工作无需 susudo 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

晌融 2024-07-11 08:51:42

您还可以使用 moreutils 包中的 sponge,而不需要重定向输出(即,无需隐藏 tee 噪音):

echo 'Add this line' | sudo sponge -a privfile

You can also use sponge from the moreutils package and not need to redirect the output (i.e., no tee noise to hide):

echo 'Add this line' | sudo sponge -a privfile
甚是思念 2024-07-11 08:51:42

通过使用 sed -i $ a ,您可以附加文本,其中包含最后一行之后的变量和特殊字符。

例如,将 $NEW_HOST 和 $NEW_IP 添加到 /etc/hosts:

sudo sed -i "\$ a $NEW_IP\t\t$NEW_HOST.domain.local\t$NEW_HOST" /etc/hosts

sed 选项解释:

  • -i 表示就地
  • $ 最后一行
  • a 用于追加

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:

sudo sed -i "\$ a $NEW_IP\t\t$NEW_HOST.domain.local\t$NEW_HOST" /etc/hosts

sed options explained:

  • -i for in-place
  • $ for last line
  • a for append
悸初 2024-07-11 08:51:42

回声“你好世界”| (sudo tee -a /etc/apt/sources.list)

echo 'Hello World' | (sudo tee -a /etc/apt/sources.list)

往昔成烟 2024-07-11 08:51:42

怎么样:
回显文本| 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

茶色山野 2024-07-11 08:51:42

这对我有用:
原始命令

echo "export CATALINA_HOME="/opt/tomcat9"" >> /etc/environment

工作命令

echo "export CATALINA_HOME="/opt/tomcat9"" |sudo tee /etc/environment

This worked for me:
original command

echo "export CATALINA_HOME="/opt/tomcat9"" >> /etc/environment

Working command

echo "export CATALINA_HOME="/opt/tomcat9"" |sudo tee /etc/environment
空城之時有危險 2024-07-11 08:51:42

您可以更改文件的所有权,然后在使用 cat >> 追加后将其更改回来吗?

sudo chown youruser /etc/hosts  
sudo cat /downloaded/hostsadditions >> /etc/hosts  
sudo chown root /etc/hosts  

像这样的东西对你有用吗?

Can you change the ownership of the file then change it back after using cat >> to append?

sudo chown youruser /etc/hosts  
sudo cat /downloaded/hostsadditions >> /etc/hosts  
sudo chown root /etc/hosts  

Something like this work for you?

南笙 2024-07-11 08:51:42

使用 Yoo 的答案,将其放入您的 ~/.bashrc 中:

sudoe() {
    [[ "$#" -ne 2 ]] && echo "Usage: sudoe <text> <file>" && return 1
    echo "$1" | sudo tee --append "$2" > /dev/null
}

现在您可以运行 sudoe 'deb blah # blah' /etc/apt/sources.list


编辑:

一个更完整的版本,允许您通过管道输入文件或从文件重定向,并包含 -a 开关关闭附加(默认情况下处于打开状态):

sudoe() {
  if ([[ "$1" == "-a" ]] || [[ "$1" == "--no-append" ]]); then
    shift &>/dev/null || local failed=1
  else
    local append="--append"
  fi

  while [[ $failed -ne 1 ]]; do
    if [[ -t 0 ]]; then
      text="$1"; shift &>/dev/null || break
    else
      text="$(cat <&0)"
    fi

    [[ -z "$1" ]] && break
    echo "$text" | sudo tee $append "$1" >/dev/null; return $?
  done

  echo "Usage: $0 [-a|--no-append] [text] <file>"; return 1
}

Using Yoo's answer, put this in your ~/.bashrc:

sudoe() {
    [[ "$#" -ne 2 ]] && echo "Usage: sudoe <text> <file>" && return 1
    echo "$1" | sudo tee --append "$2" > /dev/null
}

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):

sudoe() {
  if ([[ "$1" == "-a" ]] || [[ "$1" == "--no-append" ]]); then
    shift &>/dev/null || local failed=1
  else
    local append="--append"
  fi

  while [[ $failed -ne 1 ]]; do
    if [[ -t 0 ]]; then
      text="$1"; shift &>/dev/null || break
    else
      text="$(cat <&0)"
    fi

    [[ -z "$1" ]] && break
    echo "$text" | sudo tee $append "$1" >/dev/null; return $?
  done

  echo "Usage: $0 [-a|--no-append] [text] <file>"; return 1
}
前事休说 2024-07-11 08:51:42

在 bash 中,您可以将 tee> 结合使用。 /dev/null 保持标准输出干净。

 echo "# comment" |  sudo tee -a /etc/hosts > /dev/null

In bash you can use tee in combination with > /dev/null to keep stdout clean.

 echo "# comment" |  sudo tee -a /etc/hosts > /dev/null
笑看君怀她人 2024-07-11 08:51:42

一些用户在使用多条线路时不知道解决方案。

sudo tee -a  /path/file/to/create_with_text > /dev/null <<EOT 
line 1
line 2
line 3
EOT

Some user not know solution when using multiples lines.

sudo tee -a  /path/file/to/create_with_text > /dev/null <<EOT 
line 1
line 2
line 3
EOT
节枝 2024-07-11 08:51:42

我想指出,出于好奇,您还可以引用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"

I would note, for the curious, that you can also quote a heredoc (for large blocks):

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"
不可一世的女人 2024-07-11 08:51:42
sudo sh -c "echo 127.0.0.1 localhost >> /etc/hosts"
sudo sh -c "echo 127.0.0.1 localhost >> /etc/hosts"
花之痕靓丽 2024-07-11 08:51:42

做事

sudo sh -c "echo >> somefile"

应该有效果。 问题是> 和>> 由您的 shell 处理,而不是由“sudoed”命令处理,因此权限是您的权限,而不是您“sudoing”的用户的权限。

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.

似最初 2024-07-11 08:51:42

问题是你的 shell 负责处理重定向。 它尝试使用您的权限打开文件,而不是您在 sudo 下运行的进程的权限。

也许使用这样的东西:

sudo sh -c "echo 'something' >> /etc/privilegedFile"

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:

sudo sh -c "echo 'something' >> /etc/privilegedFile"
洋洋洒洒 2024-07-11 08:51:42

问题是 shell 确实输出重定向,而不是 sudo 或 echo,因此这是以普通用户身份完成的。

尝试以下代码片段:

sudo sh -c "echo 'something' >> /etc/privilegedfile"

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:

sudo sh -c "echo 'something' >> /etc/privilegedfile"
要走干脆点 2024-07-11 08:51:42

使用tee --appendtee -a

echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list

确保避免引号内引号。

为了避免将数据打印回控制台,请将输出重定向到 /dev/null。

echo 'deb blah ... blah' | sudo tee -a /etc/apt/sources.list > /dev/null

请记住 (-a/--append) 标志!
只是 tee 的工作方式与 > 类似,并且会覆盖您的文件。 tee -a 的工作方式与 >> 类似,并将写入文件末尾。

Use tee --append or 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文