40

I would like to have script that is doing automated migrations of websites from another be able to append Includes to the /etc/httpd/conf.d/vhosts.conf file.

However, when I try to use echo to put append a string to the end of the file I get this:

$ sudo echo "Include thing" >> /etc/httpd/conf.d/vhosts.conf
-bash: /etc/httpd/conf.d/vhosts.conf: Permission denied

and yet I can vi /etc/httpd/conf.d/vhosts.conf, add the line and :wq the file to save and close it.

What am I missing?

1 Answers1

78

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
Zoredache
  • 133,737