Why can't sudo redirect stdout to /etc/file, but sudo 'nano' or 'cp' can?
11,596
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
Related videos on Youtube
Author by
Peter.O
Updated on September 18, 2022Comments
-
Peter.O over 1 year
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)-
Gilles 'SO- stop being evil' over 12 years
-
-
Peter.O over 12 yearsGot it, thanks.. so this works too,
sudo sh -c 'printf "foo" >/etc/file'
-
Peter.O over 12 yearsAlso, as mentioned in the Q/A linked to by Gilles comment,
tee -a
caters for appending to a file...