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?

share|improve this question

10 Answers 10

up vote 567 down vote accepted

Using tee:

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

Make sure to avoid quotes inside quotes.

To avoid printing data back to the console:

echo 'deb blah ... blah' | sudo tee --append /etc/apt/sources.list > /dev/null
share|improve this answer
3  
I absolutely prefer this one. It's just the simplest (and it tought me about tee, which comes in handy in other scenarios as well). – Joachim Sauer Apr 8 '09 at 19:00
5  
I agree. Seems neater than start a new sh too, especially with potentially to do things with environment etc. – Sam Brightman Oct 16 '10 at 5:59
28  
One important thing to note: NEVER forget the -a! Just imagine what a echo 'tmpfs /tmp tmpfs defaults 0 0' | sudo tee /etc/fstab would do – mic_e Feb 17 '13 at 8:00
18  
Under OS X, this should be tee -a instead of tee --append. – knite Feb 17 '15 at 8:25
11  
For those who don't understand what @mic_e said: without the -a (--append) flag the command would overwrite the whole file with the given string instead of appending it to the end. – totymedli Oct 22 '15 at 0:15

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"
share|improve this answer
    
What are "sudo permission boundaries"? It's just the shell which parses the redirection operator with higher precedence than a command for obvious reasons – Vinko Vrsalovic Sep 17 '08 at 16:22
    
Depending on your sh, echo can interpret escape sequences like \t inside single quotes. You could use printf %s 'something' instead. – user495470 Oct 2 '12 at 0:43
1  
Will not work for echo $EVN_VARIBLE – Gordon Sun Jan 28 '16 at 19:39
2  
This should be an accepted answer! – Slava Fomin II Aug 29 '16 at 19:39
    
Using tee is more popular and more compatible to newer distros. – Eduardo B. Aug 29 '16 at 22:29

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"
share|improve this answer
2  
Will not work for echo $EVN_VARIBLE – Gordon Sun Jan 28 '16 at 19:39
sudo sh -c "echo 127.0.0.1 localhost >> /etc/hosts"
share|improve this answer
1  
Will not work for echo $EVN_VARIBLE – Gordon Sun Jan 28 '16 at 19:39

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.

share|improve this answer
1  
Will not work for echo $EVN_VARIBLE – Gordon Sun Jan 28 '16 at 19:39

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"
share|improve this answer
1  
This works great, except the embedded quotes might need to be escaped with a \ – N Jones Aug 16 '15 at 3:48
    
Quite right @NJones! I shall edit my answer. (Note, however, that not doing this strips the internal " but does not cause the command to fail.) – msanford Aug 18 '15 at 14:52

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

 echo "# comment" |  sudo tee -a /etc/hosts > /dev/null
share|improve this answer
    
That should work in any POSIX shell, not just Bash. – Toby Speight Mar 15 '17 at 14:41

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
}
share|improve this answer

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
share|improve this answer

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?

share|improve this answer
2  
Chown is a destructive command to use. If a config manager used that to update /etc/hosts, suddenly /etc/hosts belongs to config user and not root. which potentially leads to other processes not having access to /etc/hosts. The whole point of sudo is to avoid having something logged into root and via sudoers more restrictions can be piled on as well. – David Jan 22 '17 at 0:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.