Asked  7 Months ago    Answers:  5   Viewed   47 times

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?



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:

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

    Run sudo See Steve Bennett's answer if you don't want to create a temporary file.

  • Launch a shell with sudo -s then run your commands:

    [nobody@so]$ sudo -s
    [root@so]# ls -hal /root/ > /root/test.out
    [root@so]# ^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. To append instead of overwriting the output file (>>), use tee -a or tee --append (the last one is specific to GNU coreutils).

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

Tuesday, June 1, 2021
answered 7 Months ago

You could use the RedirectMatch directive of mod_alias:

RedirectMatch 301 ^/calendar-for-groups/.*$

Or with mod_rewrite:

RewriteRule ^calendar-for-groups/ [R=301,L]
Thursday, July 29, 2021
answered 5 Months ago

Run bash as sudo:

$ sudo bash -c "cat add_file >> /etc/file"

$ whoami;sudo bash -c "whoami";whoami
Sunday, August 8, 2021
answered 4 Months ago

The problem is that the file isn't owned by your user, but by root. You need to change the owner of the file to your user, then you can open and edit the file without the use of sudo:

sudo chown farheen ~/.bash_profile

This will, as the super-user, change the owner of the file back to you.

Sunday, August 8, 2021
answered 4 Months ago

Did you tried the Java examples from Maxmind?
And see for field descriptions

Sunday, August 29, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :