Set Environment variables at creation

June 29, 2016 8.4k views
Linux Basics Deployment

Very simply, I have been struggling to set system-wide environment variables while my droplet is being created. I have attempted to set them via the User-Data option on the dashboard, both using runcmd and straight bash.

The hurdle I am hitting is that after I echo the variables to ~.bashrc, it seems like source ~/.bashrc is not firing.

Once I ssh into the server, I can source and everything is fine, but I would prefer to have them set out of the gate.

Perhaps I am tackling this problem from the wrong angle, as I am new to this sort of thing, but I am trying to avoid manually setting them every time I spin up a new droplet.

Thanks in advance!

2 Answers

Try adding the following to your ~/.profile file (if it is missing):

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  • I just opened up a new instance, and printed the output. Your code above is not included, so it looks like I am getting somewhere. What is the best way to add that to ~/.profile in the bash script that I am firing at startup?

    Thanks in advance!

    • @Btibert3 - Perhaps the following comments from /etc/skel/.profile might be a better alternative for you:

      # ~/.profile: executed by the command interpreter for login shells.
      # This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
      # exists.
      # see /usr/share/doc/bash/examples/startup-files for examples.
      # the files are located in the bash-doc package.

Add somthing like this to your user-data section when spinning up a new droplet.

User Data

  - name: sammy
    shell: /bin/bash
    groups: sudo
      - your_public_ssh_key

  - path: /home/sammy/.profile
    owner: sammy:sammy
    permissions: '0644'
    content: |
      if [ -n "$BASH_VERSION" ]; then
          if [ -f "$HOME/.bashrc" ]; then
              . "$HOME/.bashrc"
  • Thanks for this. For sake of argument, can I do this as root? This is great, much appreciated.

    • The write_files and users modules are run once, during initialization, as root. When you log into your droplet for the first time, the given users and files will already exist according to the parameters in your cloud-config.

      If you just want to use root as your primary login, simply drop the users section and replace

      . . .
             - path: /home/sammy/.profile
               owner: sammy:sammy
      . . .


      . . .
             - path: /root/.profile
               owner: root:root
      . . .

      Check out cloud-init for more details.

      By the way, what distro are you having this problem with? It’s very odd that bash wouldn’t automatically source .bashrc before starting an interactive shell.

      • Basic Ubuntu 14.04. It has to be user error on my end, but the commands work as expected when I walk through them interactively after the droplet spins up.

        Thanks again, I really appreciate your help!

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!