Set fleet env variable HOSTNAME

Is there a way to easily make all the devices added to my fleet have the exact same hostname? They run on different local networks and have avahi installed and I want to be able to access them within the network as myhostname.local
Right now balena makes the default hostname the first chars of the UUID and while i’m able to use that uuid.local to access it, i just want it to be my own defined one and be the same for all devices by default. I don’t want to go modify it manually after boot using the supervisor API, and I want to be able to replicate it automatically as I add more devices.
Can this be achieved with a fleet environment variable or can it at least be achieved when configuring the OS img file prior to flashing it to the SD card? (all my devices are raspberry pi 3). If so please tell me how to do that. Thank you.

Edit: If it matters, I am running multiple containers and I only really care about one container having that hostname.

@hoobs, welcome to the balenaCloud forum and thanks for getting in touch!

I think there are a few possibilities to change the hostname of all devices in your fleet. One is to set the hostname attribute of the config.json file in the root of an OS image, prior to flashing:

Another possibility, as you have hinted yourself, is to use the supervisor API. You’ve mentioned you don’t want to modify the hostname “manually” using the supervisor API, but of course this could be scripted to automatically run only once, the first time the device ever boots. This could be a script inside any of your app containers. Is there a specific reason you would want to avoid this? (Perhaps so that device never advertises an alternative hostname, not even for a few seconds?)

If I am not mistaken, the supervisor API ('/v1/device/host-config') will actually modify a device’s /mnt/boot/config.json file behind the scenes:

https://www.balena.io/docs/reference/supervisor/supervisor-api/#patch-v1devicehost-config

Note that the supervisor API can be called either from within one of your app containers, to communicate locally with the supervisor running on the host OS, or it be called remotely from anywhere on the internet, such as a cloud server under your control. These two options are explained in this section of the docs:

https://www.balena.io/docs/reference/supervisor/supervisor-api/#http-api-reference

Let us know whether any of these options sounds suitable, and we’ll go from there.

The config.json edit did the trick. New flashes will have it. I don’t know why I didn’t think of that. I wanted to have all variables managed through the dashboard since it’s so nice to see it all and i’m using a bunch of them.
The plan is to ship out the devices to customers as plug and play. So they just plug it in and access in their browser the hostname.local and go from there. So having a hostname is important as most clients don’t know how to get the local ip address of the device unfortunately and it’s easier to instruct them to just access the hostname.local

Thank you for your help and detailed response.

Cool, thanks for confirming it! For devices already flashed or in the field, if you needed to update them, note that a regular application update (like “git push”) could include a one-off script or piece of code that called the supervisor API from within the app container, to change the device’s hostname. I believe the supervisor actually updates config.json. Goes without saying, test this on a controlled environment first! :slight_smile:

Will give this a test as well. I’m sure it will come up useful at some point. Thank you for your help!