How to set environment variables with sudo -u USER?

2

Hello and sorry for my English in advance.

I need to execute from root command like

envVariable1=15 envVariable2=25 sudo -u user1 sudo -E myCommand

but environment variables don't pass to myCommand. This may look illogical, but it's necessary to run command from root and with sudo cause it will be used in scripts. How can I work around the problem?

HeroFromEarth

Posted 2016-10-25T13:43:33.547

Reputation: 51

Possible duplicate of How do I make sudo preserve my environment variables?

– Anthony Geoghegan – 2016-10-25T13:53:23.067

2Why the double sudo? What's wrong with envVariable1=15 envVariable2=25 sudo -E myCommand? If there's a good reason for the the first sudo, this also needs a -E. Alternatively, if you don't need to preserve other environment variables, add an env command to the final sudo, as in sudo -u user1 sudo env envVariable1=15 envVariable2=25 myCommand. – AFH – 2016-10-25T13:59:20.247

Answers

2

I should type var1=1 var2=2 sudo -E -u user1 sudo -E myCommand and then var1 and var2 will be available for user1.

Thanks to AFH to help me to find an answer!

HeroFromEarth

Posted 2016-10-25T13:43:33.547

Reputation: 51