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

env sudo and sudo env

27 views
Skip to first unread message

Hongyi Zhao

unread,
Oct 3, 2019, 4:16:59 AM10/3/19
to
See this:

$ env "PATH=$PATH" sudo which python
/usr/bin/python
$ sudo env "PATH=$PATH" which python
/home/werner/.pyenv/shims/python

Why the former env command will not take effect?

Hongyi Zhao

unread,
Oct 3, 2019, 4:25:13 AM10/3/19
to
Another thing I cannot figure out is as follows:

$ sudo -h | grep -- '-E'
-E, --preserve-env preserve user environment when running
command

But the tests with and without -E give same results:

$ sudo -E which python
/usr/bin/python

$ sudo which python
/usr/bin/python

Why?

Grant Taylor

unread,
Oct 3, 2019, 10:38:58 PM10/3/19
to
The first env is not doing what you think / want it to do. Or rather it
is, but sudo is ignoring it.

Check the sudoers man page for information on how sudo sanitizes the
environment, including the PATH. You almost always can't specify an
environment variable to be used by a command running as another user via
sudo.

The second command alters what is passed to the env command, which is
run via sudo.

Check sudo's log file. You will likely see completely different
expanded / enumerated command line for the two commands.



--
Grant. . . .
unix || die

Hongyi Zhao

unread,
Oct 3, 2019, 11:35:39 PM10/3/19
to
On Thu, 03 Oct 2019 20:38:50 -0600, Grant Taylor wrote:

> Check sudo's log file. You will likely see completely different
> expanded / enumerated command line for the two commands.

With the commands discussed here, I cannot see any logs in the following
file:

/var/log/auth.log

ps.

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 9.9 (stretch)
Release: 9.9
Codename: stretch

Helmut Waitzmann

unread,
Oct 4, 2019, 9:33:12 PM10/4/19
to
Hongyi Zhao <hongy...@gmail.com>:
Because you either don't read or don't understand the sudo(8) and
sudoers(5) manual pages. It's described therein: Search for “-E“
and for “PATH” in sudo(8) and for “setenv”, “SETENV”, “env_reset”,
“env_check”, “env_delete”, “env_keep”, and “PATH” in sudoers(5)
and read the appropriate paragraphs. If you don't understand,
what's written there, post that paragraphs, asking for
explanation.

But as long as you don't do that, it's not worth while answering
your questions.

hongy...@gmail.com

unread,
Jan 17, 2021, 9:14:06 PM1/17/21
to
Thanks a lot for pertinent advice. I'll read them thoroughly.

Best,
HY
0 new messages