BalenaOS - Changing Hostname
If you are using Balena Cloud to manage your IoT devices, you will most likely run into the requirement to change the hostname of the device. This can be done in multiple ways, in the alternatives below, I am assuming you are running Balena OS on a SBC such as Raspberry Pi where the boot media is a SD card.
Editing config.json on SD card
This option is easiest way to change the hostname, but this requires physical access to the device and its’ boot media. This can be done both before first boot and after first boot.
- Take out the SD card and insert it into a computer. If you are using a Windows device, only the
/bootpartition will show, which is fine. - Open the
config.jsonfile. - If the
hostnamekey exists, update the value, otherwise the following at the end of the file, before the last}:
, "hostname": «newhostname» - Re-insert the SD card and boot device,
- The new hostname should now be active.
Changing hostname using Cloud API
This option does not require any special tools except curl, but I have only been able to get this working on a non-windows client (due to issues with quotes).
- Find the full
uuidof your device.- One option is using
balena-cli:balena device list - Another option is to navigate to the device page on dashboard. The
uuidis in the web address:https://dashboard.balena-cloud.com/devices/«uuid»
- One option is using
- Find and copy your
session tokenin the dashboard On a non-windows host, run the following command, replacing the values in the first three lines:
UUID=uuid_of_device TOKEN=session_token HOSTNAME=new_hostname curl -X POST --header "Content-Type:application/json" \ --header "Authorization: Bearer $TOKEN" \ --data "{\"uuid\": \"$UUID\", \"method\": \"PATCH\", \"data\": {\"network\": {\"hostname\": \"$HOSTNAME\"}}}" \ "https://api.balena-cloud.com/supervisor/v1/device/host-config"Reboot device, and the new hostname should now be active.
Editing config.json on the device
This option requires the following:
balena-cliinstalled and signed insshaccess to device
- Setup to
sshinto the device:- Download, install and sign into
balena-cli. - Add your clients public
sshto your Balena account. See this post for creating asshkey. - Run
balena device list. - Find the
uuidof your device, both the short and longuuidworks.
- Download, install and sign into
- Run
balena device ssh «uuid». You should now be connected to the host. - Re-mount the boot partition as
rw:mount -o remount,rw /mnt/boot. - Take a backup using
cat /mnt/boot/config.json - Check if the key
hostnamealready exists inconfig.json - Open the
config.jsonfile invi:vi /mnt/boot/config.json - If the
hostnamekey exists, update the value, otherwise the following at the end of the file, before the last}:
, "hostname": «newhostname» - To save and exit
vi:- Press
Escto enter command mode. - Type
:wqto write and quit the file.
- Press
- Doublecheck that the edit was successful by running
cat /mnt/boot/config.json. - If everything looks good, finish by rebooting the device.
- The new hostname should now be active.