Openhab 2 - correct place to set environment variables


I’m running OH2 on Linux using the apt-get installer.

I’ve amended /usr/share/openhab2/runtime/bin/setenv to change the ports and add symlinks for USB devices; I’ve also amended /etc/init.d/openhab2 to change the default user. However both get overwritten each time I update.

In OH1.8 I fixed this by editing etc/default/openhab, but /etc/default/openhab2 seems to have a different format. If anyone has an example that would be most helpful



I set the ports for USB devices in /etc/default/openhab2. Hope this helps.

Hi @dan12345, the correct place to set environment variables is indeed /etc/default/openhab, this file gets called before openHAB starts, so you should be free to set variables used by openHAB on a new line. A common configuration is:


Hope that helps!

Thank you! Can I include other items, like this:



You should be able to, the start script will only set all of those if they have not been set previously e.g.:

if [ -z "$USER_AND_GROUP" ]; then

and /etc/openhab2/defaults will get called first so the above code shouldn’t run.

smashing - than you. I see the variables are slightly different from OH1.8 - i.e. OPENHAB_HTTP_PORT instead of HTTP_PORT

That’s correct, whilst i’m not sure on the reasoning it’s probably due to HTTP_PORT being a fairly common thing to set as an environment variable.

oddly this works to set the openhab user, and to set the symlinks, but it doesn’t work to change the ports - I have to manually edit /usr/share/openhab2/runtime/bin/setenv . Am I doing something subtly wrong?

My best guess is that the test “-z” is failing when 8081 isn’t a string.

Have you expressed the variable as a string? i.e:


And does this work?

it doesn’t, unfortunately.

This is my /etc/default/openhab2 file (I use it to change the ports and it does not get overwritten after a apt-get upgrade):


and it works fine