sudo echo "something" >> /etc/privilegedFile doesn't work
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.
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, 2022Comments
-
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
orsudo su
into root? -
Vinko Vrsalovic over 15 yearsWhat 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 about 15 yearsI 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 over 13 yearsI agree. Seems neater than start a new sh too, especially with potentially to do things with environment etc.
-
Lri over 11 yearsDepending on your
sh
, echo can interpret escape sequences like\t
inside single quotes. You could useprintf %s 'something'
instead. -
mic_e about 11 yearsOne 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 about 9 yearsUnder OS X, this should be
tee -a
instead oftee --append
. -
N Jones over 8 yearsThis works great, except the embedded quotes might need to be escaped with a \
-
msanford over 8 yearsQuite 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 over 8 yearsFor 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. over 7 yearsUsing tee is more popular and more compatible to newer distros.
-
David over 7 yearsChown 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 over 7 yearsThis is the only one which works as-is as an SSH command which can be executed on a remote node.
-
James over 6 yearsFor the record, you want
-a
on Ubuntu Server 16.04,--append
is ignored as @totymedli illustrates. -
codeforester about 6 yearsShould be
tee -a
. Justtee
will overwrite the file! -
Ligemer almost 6 yearsI 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 over 5 years@GordonSun this is because
sudo
(for security reasons) doesn't propagate the environment to the subprocess. You may usesudo -E
to bypass this restriction. -
rubo77 over 5 yearsgreat, this way you can also repeat the last command as sudo:
sudo sh -c "!!"
-
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 over 4 yearsPlease take a moment to read through the editing help in the help center. Formatting on Stack Overflow is different than other sites.
-
Jeff over 3 yearsI 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-WWWXXXYYYZZZ.json"
<newline>> ' /Users/gcpclient/.bashrc
-
Akhil almost 2 years
-a
appends to file. -
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?