简体   繁体   中英

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). I am using System.getenv("myvariable") in the application to get the particular environment variable. I need to set the permanent environmental variables and currently I have set the variable in ~/.bashrc , ~/.profile and also ~/.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 . 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 .

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.

So basically I am getting null every time in my AWS EC2 linux machine.

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.

This is explained in more detail in: How to keep Environment Variables when Using SUDO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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