首页 文章

sudo echo“something”>> / etc / privilegedFile不起作用......有替代方案吗?

提问于
浏览
483

对于Linux中的sudo权限,这是一个非常简单的问题,至少看起来应该是这样 .

很多时候,我只想将某些内容附加到 /etc/hosts 或类似的文件但最终无法进行,因为即使使用root也不允许使用 >>> .

有没有办法让这项工作没有 susudo su 进入root?

13 回答

  • 0

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

    请尝试以下代码段:

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

    我会注意到,对于好奇的人,你也可以引用一个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"
    
  • 8

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

  • 269

    这对我有用:原始命令

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

    工作指挥

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

    通过将 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 for append

  • 0

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

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

    这样的东西对你有用吗?

  • 9

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

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

    问题是's your shell that handles redirection; it'尝试使用您的权限打开文件,而不是在sudo下运行的进程 .

    使用这样的东西,也许:

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

    使用Yoo's answer,将其放入 ~/.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


    Edit:

    一个更完整的版本,允许您管道输入或从文件重定向,并包含一个 -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
    }
    
  • 701
    sudo sh -c "echo 127.0.0.1 localhost >> /etc/hosts"
    
  • 0

    使用 tee --appendtee -a .

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

    请务必避免引号内的引号 .

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

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

    sudo sh -c "echo >> somefile"
    

    应该管用 . 问题是>和>>由shell处理,而不是由“sudoed”命令处理,因此权限是你的权限,而不是你正在“sudoing”的用户权限 .

  • 13

    在bash中,您可以将 tee> /dev/null 结合使用以保持stdout清洁 .

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

相关问题