The Computer Oracle

How to allow user to preserve environment with sudo?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001

--

Chapters
00:00 How To Allow User To Preserve Environment With Sudo?
00:54 Answer 1 Score 0
01:15 Accepted Answer Score 67
02:10 Thank you

--

Full question
https://superuser.com/questions/636283/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#sudo #sudoers

#avk47



ACCEPTED ANSWER

Score 67


You can use the SETENV "Tag" in your sudoers file, as in :

deploy  ALL=(ALL)       SETENV: /usr/bin/git, /etc/init.d/httpd*, /sbin/service, /usr/bin/make, /bin/echo

Or, to combine it with NOPASSWD:

deploy  ALL=(ALL)       NOPASSWD:SETENV: /usr/bin/git, /etc/init.d/httpd*, /sbin/service, /usr/bin/make, /bin/echo

Relevant excerpt from the sudoers man :

SETENV and NOSETENV

These tags override the value of the setenv option on a per-command basis. Note that if SETENV has been set for a command, the user may disable the env_reset option from the command line via the -E option. Additionally, environment variables set on the command line are not subject to the restrictions imposed by env_check, env_delete, or env_keep. As such, only trusted users should be allowed to set variables in this manner. If the command matched is ALL, the SETENV tag is implied for that command; this default may be overridden by use of the NOSETENV tag.




ANSWER 2

Score 0


Don’t specify the -E option. Using -E you´re saying that all the environment variables for the user deploy should be preserved, not only GIT_DIR

Running sudo echo $GIT_DIR should work because you've added GIT_DIR to the env_keep list