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

``sudo -E'' cann't preserve variables which I've exported.

47 views
Skip to first unread message

Hongyi Zhao

unread,
May 23, 2017, 10:37:40 PM5/23/17
to
Hi all,

See my following testings:

[1] Export the variables required by stow:

$ export PERL5LIB=/home/werner/software/software-management/stow/stow.git/
lib:$PERL5LIB
$ export PATH=/home/werner/software/software-management/stow/stow.git/bin:
$PATH

[2] Testing it without using sudo:

$ which stow
/home/werner/software/software-management/stow/stow.git/bin/stow

[3] Testing it with sudo:

$ sudo -E bash -c 'which stow'
$

Why?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Helmut Waitzmann

unread,
May 24, 2017, 7:14:18 AM5/24/17
to
Hongyi Zhao <hongy...@gmail.com>:

> See my following testings:
>
> [1] Export the variables required by stow:
>
> $ export PERL5LIB=/home/werner/software/software-management/stow/stow.git/
> lib:$PERL5LIB
> $ export PATH=/home/werner/software/software-management/stow/stow.git/bin:
> $PATH
>
> [2] Testing it without using sudo:
>
> $ which stow
> /home/werner/software/software-management/stow/stow.git/bin/stow
>
> [3] Testing it with sudo:
>
> $ sudo -E bash -c 'which stow'
> $
>
> Why?

From sudo(8):

-E

The -E (preserve environment) option will override the env_reset
option in sudoers(5)). It is only available when either the
matching command has the SETENV tag or the setenv option is set
in sudoers(5).

Also the Pluggable Authentication Modules (pam(7)) may set the
PATH variable.

But if you are allowed to run arbitrary commands with sudo, you
could do

$ sudo -E env PATH="$PATH" bash -c 'which stow'

Hongyi Zhao

unread,
May 24, 2017, 9:33:41 AM5/24/17
to
On Wed, 24 May 2017 13:13:56 +0200, Helmut Waitzmann wrote:

> But if you are allowed to run arbitrary commands with sudo, you could do
>
> $ sudo -E env PATH="$PATH" bash -c 'which stow'

Based on your above notes, I tried the following methods, it seems all of
them can do the trick:

$ sudo -E env PATH="$PATH" bash -c 'which stow'
/home/werner/software/software-management/stow/stow.git/bin/stow
$ sudo env PATH="$PATH" bash -c 'which stow'
/home/werner/software/software-management/stow/stow.git/bin/stow
$ sudo env PATH="$PATH" which stow
/home/werner/software/software-management/stow/stow.git/bin/stow
$ sudo PATH="$PATH" which stow
/home/werner/software/software-management/stow/stow.git/bin/stow

So, what's the differences among these methods?

Barry Margolin

unread,
May 24, 2017, 11:25:34 AM5/24/17
to
In article <og423i$pm2$1...@aspen.stu.neva.ru>,
Just the number of extra processes that they run to achieve the goal.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Helmut Waitzmann

unread,
May 24, 2017, 6:08:21 PM5/24/17
to
Hongyi Zhao <hongy...@gmail.com>:

> Based on your above notes, I tried the following methods, it seems all of
> them can do the trick:

> So, what's the differences among these methods?

"sudo" will set the environment variable "PATH" to the given value
(subject to the configured restrictions), then eventually
a "bash" will be started with the given command line:

> $ sudo -E env PATH="$PATH" bash -c 'which stow'
> /home/werner/software/software-management/stow/stow.git/bin/stow

Eventually the program "env" will be started, which will set the
environment variable "PATH" to the given value and start a "bash"
with the given command line:

> $ sudo env PATH="$PATH" bash -c 'which stow'
> /home/werner/software/software-management/stow/stow.git/bin/stow

Eventually the program "env" will be started, which will set the
environment variable "PATH" to the given value and start the
program "which" with the given argument:

> $ sudo env PATH="$PATH" which stow
> /home/werner/software/software-management/stow/stow.git/bin/stow

"sudo" will set the environment variable "PATH" to the given value
(subject to the configured restrictions), then eventually the
program "which" with the given argument will be started:

Hongyi Zhao

unread,
May 24, 2017, 7:15:21 PM5/24/17
to
On Thu, 25 May 2017 00:08:00 +0200, Helmut Waitzmann wrote:

>> $ sudo PATH="$PATH" which stow
>> /home/werner/software/software-management/stow/stow.git/bin/stow

Another issue is as follows:

If I want to set two or more variables, then the following form will fail:

sudo var1="$var1" var2="$var2" some_command_here

See my following testings:

$ export PERL5LIB=/home/werner/software/software-management/stow/stow.git/
lib:$PERL5LIB
$ export PATH=/home/werner/software/software-management/stow/stow.git/bin:
$PATH

$ sudo PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
sudo: stow: command not found


While all of the following forms will be ok:

$ sudo PERL5LIB=$PERL5LIB;PATH="$PATH" stow -h
$ sudo PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'
$ sudo PERL5LIB=$PERL5LIB;PATH="$PATH" bash -c 'stow -h'
$ sudo env PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
$ sudo -E env PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
$ sudo -E env PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'
$ sudo env PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'

hongy...@gmail.com

unread,
Jan 17, 2021, 8:46:57 PM1/17/21
to
On Thursday, May 25, 2017 at 7:15:21 AM UTC+8, hongy...@gmail.com wrote:
> On Thu, 25 May 2017 00:08:00 +0200, Helmut Waitzmann wrote:
>
> >> $ sudo PATH="$PATH" which stow
> >> /home/werner/software/software-management/stow/stow.git/bin/stow
> Another issue is as follows:
>
> If I want to set two or more variables, then the following form will fail:
>
> sudo var1="$var1" var2="$var2" some_command_here
>
> See my following testings:
> $ export PERL5LIB=/home/werner/software/software-management/stow/stow.git/
> lib:$PERL5LIB
> $ export PATH=/home/werner/software/software-management/stow/stow.git/bin:
> $PATH
> $ sudo PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
> sudo: stow: command not found

Sorry to continue replying to this topic after so long. The above problem should have nothing to do with sudo. It just indicates that the utility stow doesn't exist on the PATH.


> While all of the following forms will be ok:
>
> $ sudo PERL5LIB=$PERL5LIB;PATH="$PATH" stow -h

This one is wrong. The semicolon will terminate the sudo command.

> $ sudo PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'

Correct.

> $ sudo PERL5LIB=$PERL5LIB;PATH="$PATH" bash -c 'stow -h'

Wrong as noted above.

> $ sudo env PERL5LIB=$PERL5LIB PATH="$PATH" stow -h
> $ sudo -E env PERL5LIB=$PERL5LIB PATH="$PATH" stow -h

Wrong. These commands will executed using the environment variables expanded from the current shell instead of the ones living in the forked process by sudo.

> $ sudo -E env PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'
> $ sudo env PERL5LIB=$PERL5LIB PATH="$PATH" bash -c 'stow -h'

Correct.

To summarize, based on tries, all the following forms are correct:

$ sudo var1=val1 var2=val2 bash -c 'command want to be excluded'
$ sudo env var1=val1 var2=val2 bash -c 'command want to be excluded'
$ sudo -E var1=val1 var2=val2 bash -c 'command want to be excluded'
$ sudo -E env var1=val1 var2=val2 bash -c 'command want to be excluded'

Best,
HY
0 new messages