Saving a file when you forgot to open it as root

OK, this is not really related to monitoring, but Brandon Metcalf, one of our diligent TechOps superstars, posted this on our internal blog, and  I thought it was super handy, so I thought I’d share…

How many times have you opened a file in vi which is owned by root, made changes,  and then couldn’t save it because you didn’t sudo first? Sure, you could save it to an alternate location and then copy it into place, but the following is a handy trick:

:w !sudo tee %

This will save your file in place provided you have sudo privileges.

Enjoy!