如何sudo的环境变量在Linux工作? [英] How do sudo environment variables work in linux?

查看:97
本文介绍了如何sudo的环境变量在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆