How do sudo environment variables work in linux?

为君一笑 提交于 2020-01-13 20:28:05

问题


I'm currently perplexed by the following:

sudo echo $MYVAR

outputs my variable from my .bashrc,

but

sudo ./test.sh

does not, where test.sh is the below executable:

#!/bin/sh
echo $MYVAR                 

I'm running Ubuntu 14.04. Can someone enlighten me as to what's going on here?


回答1:


When you run sudo echo $MYVAR, the environment variable is expanded in your shell ... before the sudo command gets to see it.

When you run a script using sudo, that script only sees the environment variables that sudo has included in the environment. The default is to not to include them ... for security reasons.

The sudo command has an option -E that tells the command that the user wants to pass through all environment variables. The sudoers configuration file may or may not permit this.



来源:https://stackoverflow.com/questions/23850668/how-do-sudo-environment-variables-work-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!