如何sudo的环境变量在Linux工作? [英] How do sudo environment variables work in linux?
问题描述
我目前正在通过以下困惑:
I'm currently perplexed by the following:
sudo echo $MYVAR
从我的.bashrc输出我的变量,
outputs my variable from my .bashrc,
但
sudo ./test.sh
不,这里test.sh为可执行如下:
does not, where test.sh is the below executable:
#!/bin/sh
echo $MYVAR
我运行Ubuntu 14.04。有人可以告诉我一下,这是怎么回事呢?
I'm running Ubuntu 14.04. Can someone enlighten me as to what's going on here?
推荐答案
当您运行须藤回声$ MYVAR
,环境变量是在你的shell之前扩大...在须藤
命令获取看到它。
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.
的须藤
命令有一个选项 -E
,告诉用户要通过所有的环境命令变量。在sudoers配置文件可能或可能不准许此
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.
这篇关于如何sudo的环境变量在Linux工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!