sudo echo "something" >> /etc/privilegedFile doesn't work

242,497

Solution 1

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.

Solution 2

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"

Solution 3

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"

Solution 4

sudo sh -c "echo 127.0.0.1 localhost >> /etc/hosts"

Solution 5

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:
242,497
David
Author by

David

Computer hobbist since I was a kid, living my dream as a programmer now. Also, no English isn't a second language; I sacrificed grammar and coherent sentences to try and learn x86 ASM.

Updated on August 04, 2022

Comments

  • David
    David almost 2 years

    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?

  • Vinko Vrsalovic
    Vinko Vrsalovic over 15 years
    What are "sudo permission boundaries"? It's just the shell which parses the redirection operator with higher precedence than a command for obvious reasons
  • Joachim Sauer
    Joachim Sauer about 15 years
    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).
  • Sam Brightman
    Sam Brightman over 13 years
    I agree. Seems neater than start a new sh too, especially with potentially to do things with environment etc.
  • Lri
    Lri over 11 years
    Depending on your sh, echo can interpret escape sequences like \t inside single quotes. You could use printf %s 'something' instead.
  • mic_e
    mic_e about 11 years
    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
  • knite
    knite about 9 years
    Under OS X, this should be tee -a instead of tee --append.
  • N Jones
    N Jones over 8 years
    This works great, except the embedded quotes might need to be escaped with a \
  • msanford
    msanford over 8 years
    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.)
  • totymedli
    totymedli over 8 years
    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.
  • Eduardo B.
    Eduardo B. over 7 years
    Using tee is more popular and more compatible to newer distros.
  • David
    David over 7 years
    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.
  • Duncan Lock
    Duncan Lock over 7 years
    This is the only one which works as-is as an SSH command which can be executed on a remote node.
  • James
    James over 6 years
    For the record, you want -a on Ubuntu Server 16.04, --append is ignored as @totymedli illustrates.
  • codeforester
    codeforester about 6 years
    Should be tee -a. Just tee will overwrite the file!
  • Ligemer
    Ligemer almost 6 years
    I agree with other posts here. This uses a shell and only shell so another program like tee is not required here. Yes I know tee is installed already, but maybe not depending on what micro distro you're using like bare bones alpine. I like that you have a shell option too: sudo /bin/bash -c "echo 'fs.inotify.max_user_watches = 524288' > /etc/sysctl.d/60-golang-inotify.conf" for example.
  • arielf
    arielf over 5 years
    @GordonSun this is because sudo (for security reasons) doesn't propagate the environment to the subprocess. You may use sudo -E to bypass this restriction.
  • rubo77
    rubo77 over 5 years
    great, this way you can also repeat the last command as sudo: sudo sh -c "!!"
  • Nadav Har'El
    Nadav Har'El over 5 years
    @GordonSun yes it will, I don't understand why you keep repeating this statement! If you do sudo sh -c "echo 'something' >> $FILENAME", with double quotes, this will work - the variable substitution is done by the outer shell, not the sudoed shell.
  • Dharman
    Dharman over 4 years
    Please take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites.
  • Jeff
    Jeff over 3 years
    I think you've been downvoted because only GNU sed lets you put text immediately after the command. On OSX I had to actually use line breaks AND the -e option: sudo sed -ie '$a\ <newline> > export GOOGLE_APPLICATION_CREDENTIALS="adapter-gcp-compute-test-WWW‌​XXXYYYZZZ.json" <newline> > ' /Users/gcpclient/.bashrc
  • Akhil
    Akhil almost 2 years
    -a appends to file.
  • Michael Goldshteyn
    Michael Goldshteyn almost 2 years
    @Akhil, if you have a question, please leave a meaningful comment. If you have a statement or concern, please leave a meaningful comment. If you see an error or omission, please leave a meaningful comment. Do you see a pattern developing in my statements?