How to set environment variables permanently in Amazon Linux 2

17,250

Solution 1

It's better to set universal variables by creating scripts in /etc/profile.d.

You want to create it with an extension of your shell name. For example, if it's bash, it will be called script.sh for example.

/etc/profile.d/script.sh

The syntax inside will be:

export SOME_VAR_NAME=some-var-value

You will need to start a new shell session to add the variable to your environment which you can do by logging out and back in. It will added for the other users' environments when they do the same or the next time they log in if they aren't currently logged in.

Just a note: you don't actually need the shebang line as it's sourced in according to your shell. I put it in myself sometimes as it's just a force of habit but it doesn't hurt or affect anything. You can leave it out if you'd like.

Solution 2

just append it to the ~/.bash_profile file
and then activate (run) your changes:

echo "export YOUR_ENV_VAR_NAME=your_env_var_value" >> ~/.bash_profile && \
source ~/.bash_profile
Share:
17,250

Related videos on Youtube

RabT
Author by

RabT

Updated on September 18, 2022

Comments

  • RabT
    RabT almost 2 years

    I have experimented with many approaches to setting permanent environment variables EC2 instances running Amazon Linux 2, but none of the approaches are persistent across users and across login sessions.

    What specific syntax and process will successfully set environment variables so that the values of the variables will be available to all users and during every session?

    So far, methods that I have tried include setting values separately in each of the following three files during the USERDATA's launch script for the instance:

    /etc/csh.cshrc
    /etc/environment
    /etc/profile
    

    I tried each file one at a time and not all three at the same time.

    I also tried using Python's os.environ but that did not work either.


    User Suggesions

    Per @NasirRiley's suggestion, it now works when I create a setVars.sh file and place it in /etc/profile.d during the instance's USERDATA startup sequence:

    #!/bin/bash    
    export SOME_VAR_NAME=some-var-value  
    
    • Nasir Riley
      Nasir Riley about 5 years
      Have you tried putting them in a shell file in /etc/profile.d.
    • RabT
      RabT about 5 years
      @NasirRiley Not yet. I can try that right now. I was hoping for some guidance instead of trying an uneducated shotgun approach.
    • Nasir Riley
      Nasir Riley about 5 years
      It is important to make sure that you start a new shell session after creating the scripts in /etc/profile.d.
    • RabT
      RabT about 5 years
      @NasirRiley That did not work. Please see the results I posted at the end of the OP. What else can I try?
    • Nasir Riley
      Nasir Riley about 5 years
      That's because the line to set the variable is written wrongly. It should be export SOME_VAR_NAME=some-var-value. What you have there is just going to print "export SOME_VAR_NAME=some-var-value" in the terminal when the user opens it. Replace the echo line with export SOME_VAR_NAME=some-var-value.
    • RabT
      RabT about 5 years
      @NasirRiley Yes, I caught that before you wrote your comment. I edited the end of the OP to reflect actual results. Since you pointed me in the right direction, I would be happy to mark your answer as accepted and +1 if you choose to submit an answer.
    • Nasir Riley
      Nasir Riley about 5 years
      It's added as an answer.