0

I often encouter this problem and it really bugs me on why it is so. So let's take this example into account :

[me@localhost:~]$ sudo command > /some/file.log

As far as I know, this will not work if the user has no permission to write in /some/file.log, even if the command is executed as sudo. I found some workarounds on this website so that's not a problem, but I would like to know what is the reason behind it ? Why would the command be executed as sudo, and the redirection as user ? Any solid reason to back this up ?

marked as duplicate by jasonwryan, Braiam, jw013, jimmij, don_crissti Oct 27 '14 at 22:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • As said in my post, I already knew how to make this work properly. My question was more about why is that so, and Anthon gave me the reason :) – user88918 Oct 27 '14 at 21:21
  • 1
    @eyoung100 did you test that. I can not see how it can work, so I tested it, and it did not. – ctrl-alt-delor Oct 27 '14 at 21:21
  • @eyoung100 you just repeated the command, that does not work, that was in your earlier comment (that you deleted). – ctrl-alt-delor Oct 27 '14 at 21:37
  • I changed the directory to root, but I'll delete them both. – eyoung100 Oct 27 '14 at 21:41
1

You sudo the command command, the redirection of the output to /some/file.log is done by your current shell, which is running as the normal user.

What you could try in order to get the output written by root is:

sudo bash -c "command > /some/file.log"