Search code examples
linuxpipesudoredirectstandardoutput

Redirect output as a different user


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?


Solution

  • 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.