Sreenatha M D
Sreenatha M D

Reputation: 37

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.

Upvotes: 1

Views: 2193

Answers (1)

gsaslis
gsaslis

Reputation: 3186

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

Upvotes: 3

Related Questions