V
V
Viktor Yanyshev2017-08-29 21:58:58
linux
Viktor Yanyshev, 2017-08-29 21:58:58

Why is there not enough permissions to redirect output?

I want to add a host to hosts with one command:

sudo echo "121.0.0.1 test.dev" >> /etc/hosts
// Permission denied
sudo sh -c echo "121.0.0.1 test.dev" >> /etc/hosts
// Permission denied
sudo sed -e 's/test/dev/' ./etc/apache2/sites-available/test.conf
// Permission denied

Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pfg21, 2017-08-29
@villiwalla

sudo echo "121.0.0.1 test.dev" >> /etc/hosts
sudo only applies to calling the echo command, writing to the file as a normal user
is correct to do so echo is executed as a normal user, its output is redirected to the input of the tee command run as root, it appends the input stream (-a option) to the end of the file , well, and duplicates in stdout. it turns out writing to a file on behalf of the root.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question