This is the documentation for the latest (main) development branch of Zephyr. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

Hostname Configuration

Overview

A networked device might need a hostname, for example, if the device is configured to be a mDNS responder (see DNS Resolve for details) and needs to respond to <hostname>.local DNS queries.

The :kconfig:`CONFIG_NET_HOSTNAME_ENABLE` must be set in order to store the hostname and enable the relevant APIs. If the option is enabled, then the default hostname is set to be zephyr by :kconfig:`CONFIG_NET_HOSTNAME` option.

If the same firmware image is used to flash multiple boards, then it is not practical to use the same hostname in all of the boards. In that case, one can enable :kconfig:`CONFIG_NET_HOSTNAME_UNIQUE` which will add a unique postfix to the hostname. By default the link local address of the first network interface is used as a postfix. In Ethernet networks, the link local address refers to MAC address. For example, if the link local address is 01:02:03:04:05:06, then the unique hostname could be zephyr010203040506. If you want to set the prefix yourself, then call net_hostname_set_postfix() before the network interfaces are created. For example for the Ethernet networks, the initialization priority is set by :kconfig:`CONFIG_ETH_INIT_PRIORITY` so you would need to set the postfix before that. The postfix can be set only once.

API Reference

group net_hostname

Network hostname configuration library.

Defines

NET_HOSTNAME_MAX_LEN

Functions

static inline const char *net_hostname_get(void)

Get the device hostname.

Return pointer to device hostname.

Returns

Pointer to hostname or NULL if not set.

static inline void net_hostname_init(void)

Initialize and set the device hostname.

static inline int net_hostname_set_postfix(const uint8_t *hostname_postfix, int postfix_len)

Set the device hostname postfix.

Set the device hostname to some value. This is only used if CONFIG_NET_HOSTNAME_UNIQUE is set.

Parameters
  • hostname_postfix – Usually link address. The function will convert this to a string.

  • postfix_len – Length of the hostname_postfix array.

Returns

0 if ok, <0 if error