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
/boot
partition will show, which is fine. - Open the
config.json
file. - If the
hostname
key 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
uuid
of your device.- One option is using
balena-cli
:balena device list
- Another option is to navigate to the device page on dashboard. The
uuid
is in the web address:https://dashboard.balena-cloud.com/devices/«uuid»
- One option is using
- Find and copy your
session token
in 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-cli
installed and signed inssh
access to device
- Setup to
ssh
into the device:- Download, install and sign into
balena-cli
. - Add your clients public
ssh
to your Balena account. See this post for creating assh
key. - Run
balena device list
. - Find the
uuid
of your device, both the short and longuuid
works.
- 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
hostname
already exists inconfig.json
- Open the
config.json
file invi
:vi /mnt/boot/config.json
- If the
hostname
key exists, update the value, otherwise the following at the end of the file, before the last}
:
, "hostname": «newhostname»
- To save and exit
vi
:- Press
Esc
to enter command mode. - Type
:wq
to 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.