Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Possible Duplicate:
Redirecting stdout to a file you don't have write permission on

Yeah I could (and probably will) just escalate to root, but I'd like to know why this doesn't work?

sudo cat .mplayer/config >> /home/griff/.mplayer/config
zsh: permission denied: /home/griff/.mplayer/config

sudo is configured to be able to run any command, I've placed no restrictions on it.

share|improve this question
I remember an old question asking how to use sudo just like this... –  phunehehe Dec 14 '10 at 14:46
here it is unix.stackexchange.com/q/1416/250 –  phunehehe Dec 14 '10 at 14:47
@phunhehe++ I didn't know this had been asked –  xenoterracide Dec 14 '10 at 15:18
add comment

marked as duplicate by xenoterracide Dec 14 '10 at 15:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

This fails because the redirection >> is always done before the execution of the command regardless of the command.

In this case, the shell is running as you (not root) and tries to append to the .../config file using your current permissions, not root's, and fails before sudo even runs.

A common metaphor for doing what your command intends is:

sudo tee --append /home/griff/.mplayer/config < .mplayer/config

(assuming that you have read permission for .mplayer/config). Because /home/griff/... is opened by tee in the root context of sudo, it has root permissions to write that file.

I'm not wild about this approach as it copies the contents of .mplayer/config to the standard output - along with appending it to griff's file - but it does work.

share|improve this answer
add comment

As answered by msw before, this happens because >> happens before the actual command execution and does not run with the elevated sudo privileges.

An alternative way to do this, is to wrap the entire command in another bash command shell:

sudo bash -c "cat .mplayer/config >> /home/griff/.mplayer/config"

This will start a new bash shell with sudo privileges and closes it after executing the command.

share|improve this answer
add comment


sudo is capable of running any command

do you mean that conceptually sudo can be configured to run any command or that you have configured sudo on the system in question as, roughly, <user> <host(s)>=<opt(s)> ALL?

Have you tried sudo -u 0 cat ...? This would force sudo to execute the command as root, provided that your user id is allowed to do so.

What does sudo -l print?

share|improve this answer
no I meant that I haven't restricted its ability to run commands, which sudo is capable of. –  xenoterracide Dec 14 '10 at 14:02
User xenoterracide may run the following commands on this host: (ALL) NOPASSWD: ALL # waits for lynchmob –  xenoterracide Dec 14 '10 at 14:04
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.