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:
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:
sudo sh -c 'echo "Text I want to write" >> /path/to/file'