Skip to main content

How do I use sudo to redirect output to a location I don"t have permission to write to?


I've been given sudo access on one of our development RedHat linux boxes, and I seem to find myself quite often needing to redirect output to a location I don't normally have write access to.



The trouble is, this contrived example doesn't work:




sudo ls -hal /root/ > /root/test.out



I just receive the response:




-bash: /root/test.out: Permission denied



How can I get this to work?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Your command does not work because the redirection is performed by your shell which does not have the permission to write to /root/test.out. The redirection of the output is not performed by sudo.

    There are multiple solutions:


    Run a shell with sudo and give the command to it by using the -c option:


    sudo sh -c 'ls -hal /root/ > /root/test.out'

    Create a script with your commands and run that script with sudo:


    #!/bin/sh

    ls -hal /root/ > /root/test.out


    Run sudo ls.sh
    Launch a shell with sudo and then run your commands:


    $ sudo -s

    % ls -hal /root/ > /root/test.out

    % ^D

    $

    Use sudo tee (if you have to escape a lot when using the -c option):


    sudo ls -hal /root/ | sudo tee /root/test.out > /dev/null


    The redirect to /dev/null is needed to stop tee from outputting to the screen. Just in case someone needs it: to append to the output file, use tee -a or tee --append.


    Thanks go to Jd, Adam J. Forster and Johnathan for the second, third and fourth solutions.

    ReplyDelete
  2. Someone here has just suggested sudoing tee:

    sudo ls -hal /root/ | sudo tee /root/test.out > /dev/null


    This could also be used to redirect any command, to a directory that you do not have access to. It works because the tee program is effectively an "echo to a file" program, and the redirect to /dev/null is to stop it also outputting to the screen to keep it the same as the original contrived example above.

    ReplyDelete
  3. The problem is that the command gets run under sudo, but the redirect gets run under your user. This is done by the shell and there is very little you can do about it.

    [dsm@localhost:~]$ sudo command > /some/file.log
    `-----v-----'`-------v-------'
    command redirect


    The usual ways of bypassing this are:


    Wrap the commands in a script which you call under sudo
    If the commands and/or log file changes, you can make the
    script take these as arguments. For example:
    sudo log_script command /log/file.txt
    Call a shell and pass the command line as a parameter with `-c`
    This is especially useful for one off compound commands.
    For example:
    sudo bash -c "{ command1 arg ; command2 arg ; }
    > /log/file.txt;"

    ReplyDelete
  4. Make sudo run a shell, like this:

    sudo sh -c "echo foo > ~root/out"

    ReplyDelete
  5. How about writing a script?

    Filename: myscript

    #!/bin/sh

    /bin/ls -lah /root > /root/test.out

    # end script


    Then use sudo to run the script:

    sudo ./myscript

    ReplyDelete
  6. Whenever I have to do something like this I just become root:

    # sudo -s
    # ls -hal /root/ > /root/test.out
    # exit


    It's probably not the best way, but it works.

    ReplyDelete
  7. Maybe you been given sudo access to only some programs/paths? Then there is no way to do what you whant. (unless you will hack it somehow)
    If it is not the case then maybe you can write bash script

    cat > myscript.sh
    #!/bin/sh
    ls -hal /root/ > /root/test.out
    press ctrl+d

    chmod a+x myscript.sh
    sudo myscript.sh

    Hope it help.

    ReplyDelete
  8. A trick I figured out myself was

    sudo ls -hal /root/ | sudo dd of=/root/test.out

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex