0

I want to save output of command to a file that my current user do not have access to. But my user can have root access using sudo. I'm running for example:

sudo crontab -l > /var/BackUp/crontab
-bash: sudo: Permission denied

But I can run just sudo crontab -l So I think sudo relevant only to crontab command and as user do not have access to /var/BackUp/crontab it gives such error.

Is it possible to write output to a file that requires root access under current user?

Index
  • 275
  • You are confusing the question by using inappropriate terms. current user is one user, root is a different user and has no dependency on any different user, so any user who knows the root password, or has sudo privileges can be root. The owner of the file (root) can assign permissions for anyone. The owner can be changed, too. Groups can be created for finer-tuned access. But it's not clear what you actually desire. – Marty Fried Sep 10 '14 at 15:57

2 Answers2

2

The redirection is not part of the sudo command. Depending on the operation you can do:

sudo bash -c 'crontab -l >/var/backup/crontab'

Or if the command doesn't need sudo:

crontab -l | sudo tee /var/backup/crontab
muru
  • 197,895
  • 55
  • 485
  • 740
0

Test this:

sudo -i
crontab -l > /var/BackUp/crontab
Panther
  • 102,067
kyodake
  • 15,401
  • 3
    sudo -i is prefered over sudo -s, sudo su, or other options due to environmental variable. See http://ubuntuforums.org/showpost.php?p=6188826&postcount=4 . In a nutshell, sudo -i sets $HOME to /root and prevents files in a user $HOME from changing ownership to root. Changing ownership of some files in $HOME to root causes problems. – Panther Sep 10 '14 at 16:28