Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 616
David Avatar asked Sep 17 '08 16:09

David


People also ask

How do I fix sudo command not found?

Step 1: Install the 'sudo' command To achieve this, log in or switch to root user and use the APT package manager to update the system package list. Then install sudo as shown. When prompted to continue. hit 'Y' to proceed.

Is sudo a bash command?

sudo su lauches su directly with super user privileges, while sudo bash lauches the shell first and then executes the command with bash -c . The main difference would be that your . bashrc script will be run before executing the su - root command.

How do you echo a new line?

There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine. Using the backslash character for newline “\n” is the conventional way.


1 Answers

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.

like image 74
Yoo Avatar answered Oct 06 '22 05:10

Yoo