Why can I update a file owned by root using sudo vi, but not append a line to it with sudo echo "Thing" >> file?

Solution 1:

Sudo elevates the process it calls, it does not elevate any of the current shell's processing like redirection, globbing, etc.

The file redirection >> /etc/httpd/conf.d/vhosts.conf is being processed by your current shell, which is still running under your current privileges.

You could try something like this.

sudo bash -c 'echo "Include thing" >> /etc/httpd/conf.d/vhosts.conf'

Or

echo "Include thing" | sudo tee -a /etc/httpd/conf.d/vhosts.conf