Change hostname from Container

I was able to change the hostname of the hostOS by using this API: https://www.balena.io/docs/reference/supervisor/supervisor-api/#examples-13

But, it doesn’t seem to add the new hostname to /etc/hosts so that ping <hostname> is not working on the other pc.

How can we fix this issue?

1 Like

But, it doesn’t seem to add the new hostname to /etc/hosts so that ping <hostname> is not working on the other pc.

hmm I am not sure if adding an entry to /etc/hosts would be helpful anyway, because I think that changes to this file would only have an effect on the device itself, not “on the other PC”. By the way, on the ping command line on the other PC, did you try ping hostname.local - i.e. adding the .local suffix? For example, if the device hostname was "myhost", then the ping command line would be "ping myhost.local". balenaOS relies on mDNS / zeroconf / Avahi / Bonjour, and the .local suffix is a hint to the PC’s operating system (Linux, macOS, Windows) that the hostname should be resolved using mDNS. And, of course, this only works on a local private network. Beyond a local network, a public DNS server would have to be involved.

Also, the PC where you were running ping, is it Windows? Because Windows may require some extra steps to get mDNS working: see note on balena scan on this page: https://github.com/balena-io/balena-cli/blob/master/INSTALL-WINDOWS.md#balena-scan

Is this answer helpful? Feel free to insist on any particular point. :slight_smile:

1 Like

Thanks for your reply!

Yeah, my final purpose is to get ping myhost.local and ping myhost.lan working on the other computers in the same local private network.

Any detailed guide to achieve this?

Best.

Were you able to ping previoushost.local from the other computers on the same LAN, prior to changing the hostname?

No.

I assume that is because 127.0.1.1 <previousHost> is missing in /etc/hosts file.

Were you able to ping previoushost.local from the other computers on the same LAN, prior to changing the hostname?

No. I assume that is because 127.0.1.1 <previousHost> is missing in /etc/hosts file.

I don’t think it would be because something is missing on the device’s /etc/hosts file, although adding something to the PC’s /etc/hosts file might have a made a difference. I understand that mDNS / zeroconf / Bonjour / Avahi does not require changes to the /etc/hosts file in order to resolve hostnames. For example, I have a balenaOS device here of hostname b1878c6, and on my macOS laptop ping b1878c6.local works, even though the /etc/hosts file of neither the laptop nor the device include any mention of "b1878c6".

Any detailed guide to achieve this?

Usually it “just works” - which is the whole point of “zero configuration”! :slight_smile: In practice, it isn’t always like that… But the problems are almost always with mDNS / zeroconf / Bonjour / Avahi installation on the PC, not on the device. So, depending on the PC’s operating system:

  • macOS: Bonjour is built in, and it should just work.
  • Linux: Avahi is installed by default on most desktop Linux distributions. Otherwise, on Debian or Ubuntu, the installation command would be sudo apt-get install avahi-daemon.
  • Windows: I understand that some graphical applications built on UWP (Universal Windows Platform) are capable of built-in mDNS resolution, however this would not work on a command line with the ping command. For every application to work, including ping, the Apple Bonjour service needs to be running. Many Windows machines will already have this service installed, because it is (or used to be) bundled in popular applications such as Skype (Wikipedia lists several others). Otherwise, Bonjour for Windows can be downloaded and installed from: https://support.apple.com/kb/DL999 . The caveat is that these instructions are many years old, and I don’t know how much they are still relevant. :confused:

Also, something known to get in the way of mDNS / zeroconf / Bonjour / Avahi is “Internet Sharing”. For example, if I plug an ethernet cable between a Raspberry Pi 2 (which does not have WiFi) and my laptop, and give internet access to the Pi by enabling the macOS internet connection sharing, then running ping hostname.local on my laptop does not work, and I believe it is because macOS blocks the mDNS packets over the internet sharing interface.

Similarly, some local WiFi routers could be configured to drop mDNS / zeroconf / Bonjour / Avahi packets.

A somewhat defeatist alternative is not to use mDNS / zeroconf / Bonjour / Avahi, by either setting up a proper DNS server (maybe even with a domain name and a DHCP server), or using ping <ip-address> instead. :slightly_frowning_face:

2 Likes

Thanks for your detailed explanation!

I have created an application that launches the wifi-connect if there is no internet connection so that users can configure the wifi easily.

The problem is that they want to use balena.local or balena.lan on their mobile/tablet instead of the default IP address created by wifi-connect: 192.168.42.1

Yeah, now it works. Thanks!