简体   繁体   English

如何在AWS EC2 Linux中添加环境变量

[英]How to add environmental variables in AWS EC2 Linux

I am writing an application in Java and I need to use environmental variables in AWS EC2 machine (Linux). 我正在用Java编写应用程序,并且需要在AWS EC2机器(Linux)中使用环境变量。 I am using System.getenv("myvariable") in the application to get the particular environment variable. 我在应用程序中使用System.getenv("myvariable")来获取特定的环境变量。 I need to set the permanent environmental variables and currently I have set the variable in ~/.bashrc , ~/.profile and also ~/.bash_profile . 我需要设置永久环境变量,目前我已经在~/.bashrc~/.profile以及~/.bash_profile设置了变量。 I am giving export myvariable=xyz in al the 3 files and then I ran source ~/.bashrc , source ~/.profile and source ~/.bash_profile . 我在三个文件中都给了export myvariable=xyz ,然后我运行了source ~/.bashrcsource ~/.profilesource ~/.bash_profile However I am getting null in the application. 但是我在应用程序中得到空值。

I still don't understand why I am not able to get any environmental variables even though I am exporting the variable in ~/.bash_profile .I have even checked by running echo $myvariable and I can see xyz . 我仍然不明白为什么即使在~/.bash_profile导出变量也无法获取任何环境变量。我什至通过运行echo $myvariable进行了检查,可以看到xyz

If I set the same environment variable in my local MAC machine in the same way I set above and use the same shell to run the same java code, I can see the variable with the value. 如果以与上述相同的方式在本地MAC机器中设置相同的环境变量,并使用相同的Shell运行相同的Java代码,则可以看到带有值的变量。

So basically I am getting null every time in my AWS EC2 linux machine. 因此,基本上我每次在AWS EC2 linux计算机中都会得到null。

Is there any other place I need to set the variable? 我还需要设置变量吗? I have even restarted the machine but it didn't help. 我什至重新启动了机器,但没有帮助。

It sounds like the problem is how you are setting the env variables. 听起来问题是您如何设置环境变量。 Sudo does not preserve them by default, so you need to use the sudo -E option. Sudo默认不保留它们,因此您需要使用sudo -E选项。

This is explained in more detail in: How to keep Environment Variables when Using SUDO 这在以下内容中有更详细的解释: 使用SUDO时如何保留环境变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM