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

what does sudo echo do
centos sudo echo
sudo echo permission denied ubuntu
bash sudo echo redirect
sudo echo etc fstab
echo password> | sudo
sudo tee
sudo bash echo

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?

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.

How to insert text into a root-owned file using sudo?, This doesn't work because the redirection is executed by the shell, not by the command it applies to. But your shell is not running as root, only echo 'text' is. "sudo echo" does not work together in Ubuntu (another "waste of time issue") \^-- echo has raised priv \^-- shell is actually handling this HTH, Spity.

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"

"sudo echo" does not work together in Ubuntu (another "waste of , A common trick when you need to have root permissions to write to a file, but not to generate the data, is to use tee : echo 'text' | sudo tee -a /file. sudo allows you to run any command with root privileges, but not as root user. The reason this is useful is that with this setup multiple people can have root rights yet all the logging and so on still indicates who did the changes. This setup is better than sharing root passwords.

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"

command line - Cannot echo "hello" > x.txt even with sudo?, sudo sh -c 'echo "Text I want to write" > /path/to/file'. If you just want to append some text, you do it this way: echo "Text I want to write" | sudo tee  A great cover version of the classic theme of Lipps Inc, "Funky Town". For me, the best version of the song is in this video.

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

Echo lines into a root owned file with sudo, The redirection is done by the shell before sudo is even started. So either make sure the redirection happens in a shell with the right  The redirection is done by the shell before sudo is even started. So either make sure the redirection happens in a shell with the right permissions. sudo bash -c 'echo "hello" > f.txt' or use tee. echo "hello" | sudo tee f.txt # add -a for append (>>)

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.

How to append text to a file when using sudo command on Linux or , Just echoing some lines into root owned file using using sudo. You figure something like "sudo echo blah >> /tmp/test" would work fine. 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 fil

Adding a line into the hosts file, getting permission denied when , I am trying to add entry to /etc/hosts file using ' sudo echo '192.168.1.254 router' >​> /etc/hosts ' but getting an error that read as, -bash:  sudo strace -r -o trace.log sudo echo hi Each line will start with the time taken since entering the previous syscall. (The initial sudo seems to be necessary; I don't know how much that will perturb the results.)

How to append to a file as sudo?, That's because echo is being run as root, but the shell is the one actually performing the redirection. You need to spawn a new shell for this to work: sudo -- sh -c  Pseudo Echo are an Australian new wave band that formed in 1982 in Melbourne, Victoria. The original line-up consisted of Brian Canham (vocals, guitars and keyboards), Pierre Gigliotti (as Pierre Pierre) (bass guitar, keyboards), Tony Lugton (guitars and keyboards) and Anthony Argiro (drums).

`sudo echo "bla" >> /etc/sysctl.conf` permission denied, -a, --append append to the given FILEs, do not overwrite [] So your command becomes echo "something" | sudo tee -a /etc/config_file. The advantages of tee 

Comments
  • 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).
  • I agree. Seems neater than start a new sh too, especially with potentially to do things with environment etc.
  • 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
  • Under OS X, this should be tee -a instead of tee --append.
  • 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.
  • What are "sudo permission boundaries"? It's just the shell which parses the redirection operator with higher precedence than a command for obvious reasons
  • Depending on your sh, echo can interpret escape sequences like \t inside single quotes. You could use printf %s 'something' instead.
  • Will not work for echo $EVN_VARIBLE
  • Using tee is more popular and more compatible to newer distros.
  • This is the only one which works as-is as an SSH command which can be executed on a remote node.
  • Will not work for echo $EVN_VARIBLE
  • @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.