University Crest

[wlug] sudo and writing to /var/log

wlug archive index About the wlug list Mailing lists home
To The University of Waikato HomepageWaikato Home > Waikato Mailing Lists > wlug Info > wlug archives
Daniel Lawson daniel@m...
Wed Nov 30 15:33:15 NZDT 2005

>You would think (or at least, I do) that something like "sudo rsync 
>options source destination >> /var/log/logfile" would NOT give you a 
>permission denied error. Similarly with "sudo date > /var/log/logfile". 
>But you would be dead wrong.
>Running the entire script with sudo (ie, sudo b.netbackup) will work 
>fine, as will running it from a hash prompt.

That's all correct :)

>Anyone got any ideas why I can't write to a log file using sudo? 
>Hopefully it isn't something really obvious that will demonstrate my 
>fundamental lack of understanding of how Linux works..

It may not demonstrate a fundamental lack of understanding, but it is a 
fairly fundamental point.

If I run the command "sudo date > /var/log/logfile", then only 'data' is 
run within the sudo command. The output redirection to /var/log/logfile 
is executed within the current bash process - running as the user.

All is not lost - run the command AND the redirection from within the 
sudo command:

sudo /bin/bash -c " date > /var/log/logfile"      will tell sudo to run 
bash, and have bash execute the command (or shell snippet) contained 
inside the following string.

More information about the wlug mailing list
NOTICE: This is an archive of a public mailing list. The University of Waikato is not responsible for its contents.

The University of Waikato - Te Whare Wananga o Waikato