Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sudo and echo

10 views
Skip to first unread message

cor...@free.fr

unread,
Apr 28, 2023, 8:00:06 PM4/28/23
to

Hello list,

When I run this command:

$ sudo echo 123 > /root/123.txt

It tells me "permission rejected".

Why this sudo can't get success?


Thanks.

Corey H

The Wanderer

unread,
Apr 28, 2023, 8:10:05 PM4/28/23
to
On 2023-04-28 at 19:52, cor...@free.fr wrote:

> Hello list,
>
> When I run this command:
>
> $ sudo echo 123 > /root/123.txt
>
> It tells me "permission rejected".
>
> Why this sudo can't get success?

If I'm not very much mistaken: because redirection like that is (as I
understand matters) processed by the currently-active shell, not by the
environment that will be created inside the sudo invocation.

What this is almost certainly doing is passing 'echo' and '123' to sudo,
and telling the *current* shell to redirect the output of 'sudo echo
123' into /root/123.txt. If the current shell process does not have
permission to write to that location, then you will get that error.


I would suspect that

$ sudo 'echo 123 > /root/123.txt'

would produce the effect you're after, but as I don't have sudo
installed, cannot verify that myself.

--
The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man. -- George Bernard Shaw

signature.asc

Greg Wooledge

unread,
Apr 28, 2023, 8:40:05 PM4/28/23
to
On Sat, Apr 29, 2023 at 01:52:11AM +0200, cor...@free.fr wrote:
> $ sudo echo 123 > /root/123.txt
>
> It tells me "permission rejected".
>
> Why this sudo can't get success?

Because the redirection is done by your shell before sudo is executed.

See <https://mywiki.wooledge.org/BashPitfalls#pf53> for suggestions,
but basically you're looking at variants of:

sudo sh -c 'echo 123 > /root/123.txt'

Greg Wooledge

unread,
Apr 28, 2023, 8:40:05 PM4/28/23
to
On Fri, Apr 28, 2023 at 08:00:02PM -0400, The Wanderer wrote:
> I would suspect that
>
> $ sudo 'echo 123 > /root/123.txt'
>
> would produce the effect you're after, but as I don't have sudo
> installed, cannot verify that myself.

No, that won't work. It'll try to execute a program named
'echo 123 > /root/123.txt' which is really, really unlikely to exist
in your current working directory.

cor...@free.fr

unread,
Apr 28, 2023, 8:40:05 PM4/28/23
to
Thanks for explanation. I appreciate it.

The Wanderer

unread,
Apr 28, 2023, 8:50:08 PM4/28/23
to
That was the other possibility, the existence of which which was the
reason why I couched this in terms so far from certainty.

The need to invoke a shell around them was also a possibility I
considered, but I didn't want to rewrite the post three more times
trying to express that too without also adding so much more complexity
as to make the thing unreadable.

You managed it all much more concisely and helpfully than I had done.
signature.asc

David Wright

unread,
Apr 28, 2023, 11:20:05 PM4/28/23
to
You could also use the construction:

$ echo 123 | sudo tee /root/123.txt > /dev/null

which has the advantage, when required, that the program producing
the output doesn't have to run with root privileges.

Cheers,
David.

to...@tuxteam.de

unread,
Apr 29, 2023, 1:00:06 AM4/29/23
to
My favourite alternative to tee for this is dd: you don't have to
"throw away" the other stream:

echo 123 | sudo dd of=/root/123.txt

Actually, any program willing to pass stdin to an output file
whose name you can choose will do the trick.

Cheers
--
t
signature.asc

Byung-Hee HWANG

unread,
Apr 29, 2023, 1:30:06 AM4/29/23
to
<to...@tuxteam.de> writes:

> (...)
> My favourite alternative to tee for this is dd: you don't have to
> "throw away" the other stream:
>
> echo 123 | sudo dd of=/root/123.txt
>
> Actually, any program willing to pass stdin to an output file
> whose name you can choose will do the trick.

Hellow tomas,

It is very amazing, excellent job!

Sincerely,

--
^고맙습니다 _布德天下_ 감사합니다_^))//

Mimiko

unread,
Apr 29, 2023, 1:10:06 PM4/29/23
to
On 29.04.2023 02:52, cor...@free.fr wrote:
> Hello list,
>
> When I run this command:
>
> $ sudo echo 123 > /root/123.txt
>

A better use is to do:

echo 123 | sudo tee /root/123.txt

or

sudo tee /root/123.txt <<EOF
123
EOF

If rather adding text to a file then add parameter '-a' to the 'tee' command.

--
Mimiko desu.


--
This email has been checked for viruses by Avast antivirus software.
www.avast.com
0 new messages