# A hack to change the device hostname

**URL:** <https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195>\
**Category:** Product support\
**Tags:** pendinguserresponse\
**Created:** [April 23, 2019, 10:20am UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195 "2019-04-23T10:20:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [April 23, 2019, 10:20am UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/1 "2019-04-23T10:20:30Z")

</div>

Hi all,

I’ve been having some trouble working out how to change the device hostname properly using the API.

We need this as we have some IoT devices using mDNS to talk to a broker running in a Docker container.

I’ve worked out a hack that may help some.

- connect to the host OS on the device (i.e not a container app)
- mount the boot partition as read/write

`mount -o remount,rw /mnt/boot`

- edit the config.json file

`nano /mnt/boot/config.json`

- add a key-pair to the end of the JSON

`,"hostname":"my_new_hostname" }`

- reboot and hey presto you have a new host name

I’d really appreciate some for dummies advice on where to find the API key so I can use the curl mechanism.

Also how to set up multiple containers with docker-compose with support for different hostnames published to the network with mDNS?

Thanks!

Alex

---

<div class="post-metadata">

**Author:** ![zrzka](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/zrzka/32/1612_2.png) [@zrzka](https://forums.balena.io/u/zrzka)\
**Post date:** [April 23, 2019, 3:07pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/3 "2019-04-23T15:07:58Z")

</div>

Hi,

if you’d like to use `curl` to change the hostname, you can do it via:

```auto
curl -X PATCH -H "Authorization: Bearer $BALENA_SUPERVISOR_API_KEY" \
  -H "Content-Type: application/json" \
  "$BALENA_SUPERVISOR_ADDRESS/v1/device/host-config" \
  -d '{"network":{"hostname":"foo"}}'

```

You can get / verify it via:

```auto
curl -X GET -H "Authorization: Bearer $BALENA_SUPERVISOR_API_KEY" \
  -H "Content-Type: application/json" \
   "$BALENA_SUPERVISOR_ADDRESS/v1/device/host-config"

```

Supervisor API address is available via `BALENA_SUPERVISOR_ADDRESS` environment variable and the API key is available via `BALENA_SUPERVISOR_API_KEY`. Or you can use session token or [generated access tokens](https://dashboard.balena-cloud.com/preferences/access-tokens).

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [April 23, 2019, 3:24pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/4 "2019-04-23T15:24:55Z")

</div>

Thanks @zrzka - I hunted high and low for the environment variables but couldn’t find them anywhere in the device. I’m using OpenBalena. Dunno if that makes a difference?

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [April 23, 2019, 3:32pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/5 "2019-04-23T15:32:25Z")

</div>

@ajlennon The `BALENA_SUPERVISOR_API_KEY` should be generated by the supervisor when it starts a container and then it is injected into the container if it has the `io.balena.features.supervisor-api: 1` label for that service in the compose.yml. Do you not see this in your service containers?

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [April 27, 2019, 12:22pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/6 "2019-04-27T12:22:19Z")

</div>

I don’t think I have this - will check and revert - thanks!

---

<div class="post-metadata">

**Author:** ![jviotti](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jviotti/32/220_2.png) [@jviotti](https://forums.balena.io/u/jviotti)\
**Post date:** [April 29, 2019, 1:31pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/7 "2019-04-29T13:31:34Z")

</div>

Hey @ajlennon! How did it go in the end?

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 1, 2019, 2:59pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/9 "2019-05-01T14:59:29Z")

</div>

Hiya,

So having a bit of a look at this now. Couple of bits of feedback

- so mostly I’m using single containers on OpenBalena so no docker-compose file
- however I do have one multi-container with a docker-compose I’ll modify

With the current setup with OpenBalena I’m using the SSH forwarding setup to SSH into remote units so this goes into the base image not a container. Given I’m in the base image I’d like to just “run something” to change the hostname but I couldn’t find a way (hence the original post). There are no BALENA\_ env vars.

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 1, 2019, 3:05pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/10 "2019-05-01T15:05:50Z")

</div>

So with that label added to a container definition in my docker compose I see the env vars in the container 🙂

```
bash-4.4# set | grep BALENA
...
BALENA_SUPERVISOR_ADDRESS=http://172.17.0.1:48484
BALENA_SUPERVISOR_API_KEY=8a....
...
```

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 1, 2019, 3:09pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/11 "2019-05-01T15:09:54Z")

</div>

Hmmm - I’m sending

```
curl -X GET -H Authorization: Bearer 8a.... -H Content-Type: application/json http://172.17.0.1:48484/v1/device/host-config

```

And getting

```
Unauthorized
```

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 1, 2019, 3:23pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/12 "2019-05-01T15:23:22Z")

</div>

On a side-note while I look at this it would be useful to be able to broadcast different container hostnames via mDNS from a single physical device. Is that possible?

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 3, 2019, 11:40am UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/13 "2019-05-03T11:40:00Z")

</div>

Any thoughts @jviotti?

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [May 3, 2019, 8:18pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/15 "2019-05-03T20:18:29Z")

</div>

@ajlennon are you using quotes in `-H "Authorization: Bearer <apikey>"`? (Not sure if that would cause that error, but just checking)

---

<div class="post-metadata">

**Author:** ![hedss](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/hedss/32/210_2.png) [@hedss](https://forums.balena.io/u/hedss)\
**Post date:** [May 7, 2019, 2:55pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/17 "2019-05-07T14:55:48Z")

</div>

Hi there @ajlennon,

You should be able to publish container hostnames from a service container on a device with the device’s IP address by using the DBus interface to the Avahi service running on the host.

If you’re running singlecontainer, then this will be privileged and you won’t need to do anything extra apart from installing the DBus library via your Dockerfile. If you’re using multicontainer, you’ll need the `io.balena.features.dbus` label set. You’ll also need to set the DBus socket address. There’s more information on working with DBus in balena [here](https://www.balena.io/docs/learn/develop/runtime/#dbus-communication-with-host-os).

Briefly, using a Debian base image, you should be able to:

```auto
RUN apt-get update && \
    apt-get install -yq --no-install-recommends \
    libdbus-glib-1-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

```

in your Dockerfile to install the DBus library, and then use it in your application to call Avahi via it. In the following snippet, I’m using NodeJS (via TypeScript) and using the `dbus-native` NPM module (I also use Bluebird here for async callbacks):

```auto
import { Message, systemBus } from 'dbus-native';
import * as Bluebird from 'bluebird';
...
const dbus = systemBus();
const dbusInvoker = (message: Message): PromiseLike<any> => {
	return Bluebird.fromCallback(cb => {
		return dbus.invoke(message, cb);
	});
};
...
const group = await dbusInvoker({
		destination: 'org.freedesktop.Avahi',
		path: '/',
		interface: 'org.freedesktop.Avahi.Server',
		member: 'EntryGroupNew',
});

await dbusInvoker({
		destination: 'org.freedesktop.Avahi',
		path: group,
		interface: 'org.freedesktop.Avahi.EntryGroup',
		member: 'AddAddress',
		body: [-1, -1, 0x10, 'myNewHostname', '192.168.1.20'],
		signature: 'iiuss',
});

await dbusInvoker({
		destination: 'org.freedesktop.Avahi',
		path: group,
		interface: 'org.freedesktop.Avahi.EntryGroup',
		member: 'Commit',
});

```

You should be able to replace `myHostname` with the container hostname programmatically however you choose, (eg. via `require('os').hostname()` for NodeJS), and the IP address for the host can easily be achieved by querying the Supervisor API (the device endpoint, [here](https://github.com/balena-io/balena-supervisor/blob/master/docs/API.md#get-v1device) has an `ip_address` field with the current IP facing IP addresses).

Hopefully this is enough to get you going, let us know if anything’s unclear!

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 7, 2019, 4:41pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/19 "2019-05-07T16:41:21Z")

</div>

Hi @pcarranzav - yes I’m using quotes

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 7, 2019, 4:42pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/20 "2019-05-07T16:42:04Z")

</div>

That looks great thanks @hedss - I’ll give it a go!

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 7, 2019, 4:44pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/21 "2019-05-07T16:44:57Z")

</div>

So the ping test works…

```auto
curl -X GET --header "Content-Type:application/json" \
    "$BALENA_SUPERVISOR_ADDRESS/ping?apikey=$BALENA_SUPERVISOR_API_KEY"

```

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 7, 2019, 4:48pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/22 "2019-05-07T16:48:55Z")

</div>

OK looking at this page I need to provide the key on the GET line

[https://www.balena.io/docs/reference/supervisor/supervisor-api/](https://www.balena.io/docs/reference/supervisor/supervisor-api/)

```auto
curl -X GET -H "Authorization: Bearer ${BALENA_SUPERVISOR_API_KEY}" \
  -H "Content-Type: application/json" \
   "${BALENA_SUPERVISOR_ADDRESS}/v1/device/host-config?apikey=${BALENA_SUPERVISOR_API_KEY}"

```

This works for me…

---

<div class="post-metadata">

**Author:** ![thgreasi](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thgreasi/32/454_2.png) [@thgreasi](https://forums.balena.io/u/thgreasi)\
**Post date:** [May 7, 2019, 5:03pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/23 "2019-05-07T17:03:55Z")

</div>

Hi,  
Do you mean that `curl "$BALENA_SUPERVISOR_ADDRESS/v1/device/host-config?apikey=$BALENA_SUPERVISOR_API_KEY"` didn’t work for you when not specifying an Authorization header?  
I can confirm on my side that this works on v2.27.0.  
What os/supervisor version are you using?

---

<div class="post-metadata">

**Author:** ![ajlennon](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/ajlennon/32/63_2.png) [@ajlennon](https://forums.balena.io/u/ajlennon)\
**Post date:** [May 7, 2019, 5:12pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/24 "2019-05-07T17:12:48Z")

</div>

I have to provide ?apikey= on the GET line or the authorisation isn’t working (i.e. via the header)

---

<div class="post-metadata">

**Author:** ![thgreasi](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thgreasi/32/454_2.png) [@thgreasi](https://forums.balena.io/u/thgreasi)\
**Post date:** [May 7, 2019, 6:02pm UTC](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195/25 "2019-05-07T18:02:11Z")

</div>

That’s weird @ajlennon What os/supervisor version are you using?

[Next page](https://forums.balena.io/t/a-hack-to-change-the-device-hostname/6195.md?page=2)
