7

I need to sudo as a different used in order to execute a certain command. I am trying to use strace with it and redirecting the output of that into a file for further analysis. The tricky part is that as the sudo'ed user I don't have permissions to write to the location I want to save my file in. (and without sudo'ing I don't have permission to execute that command to begin with).

So how can I execute my command as user A, and redirect it's output as user B?

Pavel
  • 729
  • 2
  • 11
  • 22

4 Answers4

11

Try with:

sudo sh -c "command > output.txt"

In this way you should be able to run any command and write everywhere.

If you really need, for some reason I don't understand, execute the command as user A and write as user B, you can do the following:

sudo -u A command | sudo -u B tee /somewhere > /dev/null

Where A and B are the user you want. The > /dev/null part is needed only if don't want command output to be redirected on stdout, too.

Zagorax
  • 11,440
  • 8
  • 44
  • 56
  • What are you doing with those superfluous calls to sh? The whole thing works perfectly well if you just combine `sudo` and `tee` … – filmor Jul 03 '12 at 10:44
  • False. `sudo -u nobody ls | sudo -u root tee > /test` and I get `bash: /test: Permission denied`. Actually I can't explain why, but on my system, I need to call sh (or bash). If someone has an explanation, it's welcome. – Zagorax Jul 03 '12 at 10:50
  • 1
    That's because you're using `tee` wrong :). Please read the manpage. You provide the file to write to as an /argument/. In your example the piping is done by your own user (as `>` is interpreted from the caller's shell) while with proper use of `tee` the caller of that part is relevant (which would be `root` in this case…) – filmor Jul 03 '12 at 10:52
  • 1
    Oh thanks. You're right. Updating my answer, and voting for yours, it worth an upvote. :) – Zagorax Jul 03 '12 at 10:57
7

You can use tee for that. The program reads stdin and writes the input to one or more files as well as stdout:

sudo funny_command | sudo tee output_file > /dev/null

/EDIT: Although you already accepted the other (in my eyes inferior) answer I'll just complete this anyhow:

The use cases above can be done like this

sudo command | sudo tee output.txt > /dev/null
sudo -u A command | sudo -u B tee output.txt > /dev/null

You don't have to use the redirection to /dev/null of course.

filmor
  • 30,840
  • 6
  • 50
  • 48
  • Perhaps not relevant to the answer but I just needed to execute this: echo "the text content" | sudo -u developer tee output.txt – Daniel Katz Jan 15 '20 at 22:45
1

Another option that avoids piping the stuff back and then to /dev/zero is

sudo command | sudo dd of=output.txt
Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
0

Maybe I've misunderstood the original question.

If you want to capture the entire output of sudo strace foo then the easiest way is to run script first.

If you want to capture the stderr of sudo strace foo you could try sudo strace foo 2> >(tee logfile).

If you only want to capture the output of strace and not of foo then you could try sudo strace -o /dev/fd/3 foo 3> >(tee logfile).

If you can't use 3> > then I think you can still do it with fd redirection.

Neil
  • 54,642
  • 8
  • 60
  • 72