Sometimes, you need to deal with a really time-wasting issue. I think it happens even more often if you work in the IT industry. You know the resolution must be simple, but all your attempts failed. So you try to search the web, looking for a plain sentence that will lead you out of that mess...
If you want to write some text to a file that needs a root privileges, you do it this way:
echo "Text I want to write" | sudo tee /path/to/file > /dev/null
or (updated after reading discussion below):
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 /path/to/file > /dev/null
or (updated after reading discussion below):
sudo sh -c 'echo "Text I want to write" >> /path/to/file'
This won't work:
sudo echo "Text I want to write" > /path/to/file