Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Why does redirection, using sudo give me an error for the following commands?

$ sudo printf "foo" >/etc/file
bash: /etc/file: Permission denied

$ sudo printf "foo" ~/file; cat ~file >/etc/file 
bash: /etc/file: Permission denied

...yet I have no such problem when I use an editor, or cp.
I don't think I've ever tried this before, so I don't know if there is something haywire on my system, or if this is normal. It seems a bit restrictive to be normal, but(?) maybe it is intended to be restrictive... (using Ubuntu)

share|improve this question

marked as duplicate by dr01, Anthon, G-Man, chaos, Eric Renouf Aug 28 '15 at 14:54

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.

up vote 19 down vote accepted

It is normal. The file after the > is not open by the process running under sudo, but by the shell, which isn't. Try this instead:

printf "foo" | sudo tee /etc/file
share|improve this answer
Got it, thanks.. so this works too, sudo sh -c 'printf "foo" >/etc/file' – Peter.O Aug 30 '11 at 3:37
Also, as mentioned in the Q/A linked to by Gilles comment, tee -a caters for appending to a file... – Peter.O Aug 30 '11 at 12:15

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