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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...