Jud Dagnall Photography Blog

Photography, technology and occasional rants!

redirecting sudo output

Posted on January 25th, 2005 in , by jud || No Comment

Sometimes I need to use the linux sudo command to run another command as another user, but also write the output of that command as another user. I always forget the proper syntax for this sort of thing, since simple quoting and escaping don’t work. Ryan pointed out a straightforward way to accomplish it.

For example, if I wanted to run a command as *apache* when running as user *me*,

(me@home) $ sudo -u apache command 

However, trying to redirect the output of the command will cause the output to be written as user *me*, not *apache*, causing
an error when writing to a directory or file not writable by *me*.

(me@home) $ sudo -u apache command > /home/apache/out
bash: /home/apache/out: Permission denied

Ryan’s solution is as follows: use sudo to call an shell with the quoted command as a parameter:

(me@home) $ sudo -u apache /bin/bash -c "command > /home/apache/out"

Leave a Reply

Your email address will not be published. Required fields are marked *