Adding DB_HOST environment varibale to Ubuntu VM in provision.sh

437 Views Asked by At

This is the code I have currently in my provision.sh file for vagrant to run when setting up my VM: (I do have other code before this to install packages etc, it is all working, it's just that this environment variable is not being created and set)

#Add DB_HOST env variable
export DB_HOST=192.168.10.150:27017/posts
echo "DB_HOST=192.168.10.150:27017/posts" >> ~/.bashrc
source ~/.bashrc

Is there something massively wrong with this? Or is there some other method that I have to use?

2

There are 2 best solutions below

0
Mo0rBy On BEST ANSWER

Thank you to KamilCuk for helping me with this.

He told me to echo into the /etc/environment file, and add the DB_HOST env variable in there to make it persistent.

I did have some issues doing this and here is the command I used to make it work > echo "DB_HOST=[db-ip]" | sudo tee -a /etc/environment

So, echo the variable name and its value, then pipe that into a tee command, which basically just opens a file and allows write access (I think). You need to do it in this way because you need root user permissions to open and write to the /etc/environment file, and you don't need to use sudo in order to complete the echo.

0
koyaanisqatsi On

Another way...

To let a linux programm start with a special set of environment variables you can use: env -i ...

Example for a shell function that provides a special environment for the Lua interpreter...

lua () 
{ 
    ( env -i LANG='de_DE.UTF-8' TERM='xterm-256color' LUA_PATH='./lua/?.lua' LUA_CPATH='./lua/?.so' /usr/local/bin/lua "${@}" )
}

...give me the environment what i want.
This example use full path to Lua executable and need therefore no PATH variable.

The function is placed in .bashrc (user system login) or .profile (user remote login) of a normal users home folder.

Impression...

$ lua
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> require('dialog')
dialog: 0x56690120  ./lua/dialog.lua
-- Do special german chars work too?
> _G['ÜüÄäÖöß']='It works!'
> print(_G['ÜüÄäÖöß'])
It works!
-- Environment is also used by child(s) (inherit)...
> os.execute('env')
PWD=/home/knoppix
LINES=54
LANG=de_DE.UTF-8
COLUMNS=190
TERM=xterm-256color
SHLVL=0
LUA_CPATH=./lua/?.so
LUA_PATH=./lua/?.lua
_=/usr/bin/env
true    exit    0

This is working in multiuser environments without being superuser root first.
Because normal users arent allowed to write something in / or /etc/ that belongs to: root