Working with host names using Azure IoT and OSConfig

Important

Version 1.0.3 (published 28 June 2022) includes breaking changes to member names which may impact existing users. For more information, see: Member names transition from PascalCase to camelCase in version 1.0.3

Audience and scope

This article is designed to support people who provision or manage devices with Azure IoT. If that doesn't sound like you, consider taking a look at Audiences for OSConfig documentation.

This article is about getting and setting device host names with the OS on the device.

Prerequisites

If you are using this article for reference (for example, you are here to copy a property name), there are no pre-requisites.

If you want to try the examples on live systems (recommended), then:

  1. You will need an Azure account with an IoT Hub

    This article assumes some familiarity with IoT Hub and related tools. For example, it assumes you are comfortable creating IoT Hubs and attaching devices. If you prefer a more prescriptive step-by-step introduction to installing and using OSConfig from scratch, see: Quickstart: Manage a single virtual IoT device using Azure CLI instead.

  2. You will need at least one Linux device with the OSConfig agent installed and connected to Azure IoT.

    For more information, see: How and where to install the OSConfig agent for Linux.

  3. You will use Azure Portal or Azure CLI to interact with the devices via your IoT Hub

    For further steps, choose your preferred experience:

  1. Ensure you are signed in to the Azure Portal and can access your IoT Hub's Overview page Screenshot showing IoT Hub and devices from the Azure Portal

The object model

The following are the key desired and reported properties related to host name.

Primary use Populated by Notes
reported.HostName.name Get scenarios OSConfig agent Name reported by the OS, analogous to cat /etc/hostname on most Linux systems
desired.HostName.desiredName Set scenarios Admin or admin tools Admin's intended host name for the device
reported.HostName.desiredName.value Set scenarios OSConfig agent Acknowledges receipt of desired host name; should match desired name
reported.HostName.desiredName.ac Set scenarios OSConfig agent Status for reaching desired state (host name set with OS); 200 indicates success

To see these properties with example values in the context of an OSConfig twin from IoT Hub, see the reference section at the end of this article.

A. Examples for individual devices

The following demonstrate specific use cases. You can use these as starting points and adapt for your unique environment.

Example A.1. Set a new host name on a specific device

  1. Using Azure Portal, you can change or update the host name on a specific device by editing the module twin for OSConfig.

    Screen capture showing set of desiredName property from Azure Portal

  2. You can verify the change is made successfully from the module twin itself or you can verify the hostname on the device has been updated. Scroll down the module twin to find reported properties for HostName .

    Screen capture showing reported name property from Azure Portal

B. At-scale examples for fleets of devices

Example B.1. At scale, inventory or audit the host names of all devices connected to the IoT Hub

Solution portals, security audits, and other inventory-oriented workflows often rely on having host name information for all devices. Choose your preferred experience to see examples:

  1. From your Azure IoT Hub's portal page, choose Device management --> Queries in the left hand navigation.

  2. Execute this query: SELECT deviceId,properties.reported.HostName.name FROM devices.modules WHERE moduleId='osconfig'"

    Screen capture showing ip address add command

Example B.2. At scale, dynamically set a unique host name on each device

In this example, we imagine an environment where we want all the host names to match the IoT Hub devices IDs. This way logs and other data from the devices will be easy to reconcile and contextualize on the back end of our cloud solution.

This scenario cannot be completed directly from the IoT Hub portal pages. It could be done via Azure Portal using another service for automation, such as Azure Functions.

For reference on the logic and object model, see the Bash / Cloud Shell example.

Reference: Example twin from Iot Hub

The following is excerpted from an osconfig module twin in IoT Hub. For context it includes most of the raw twin, with certain properties (like $metadata) removed for brevity. The properties related to this article are highlighted.

{
    "deviceId": "device03",
    "moduleId": "osconfig",
    "connectionState": "Connected",
    "modelId": "dtmi:osconfig:deviceosconfiguration;5",
    "version": 28,
    "properties": {
        "desired": {
            "Settings": {
                "__t": "c",
                "DeviceHealthTelemetryConfiguration": 2,
                "DeliveryOptimizationPolicies": {
                    "PercentageDownloadThrottle": 50,
                    "CacheHostSource": 1,
                    "CacheHost": "http://mycachehost"
                }
            },
            "CommandRunner": {
                "__t": "c",
                "CommandArguments": {
                    "CommandId": "set_timezone__And_report_back",
                    "Arguments": "timedatectl set-timezone UTC; timedatect l grep zone | tr -d '[:space:]'",
                    "SingleLineTextResult": false,
                    "Action": 3
                }
            },
            "HostName": {
                "__t": "c",
                "DesiredName": "NewHostName123",
                "DesiredHosts": "127.0.0.1 localhost;::1 ip6-localhost ip6-loopback;fe00::0 ip6-localnet;ff00::0 ip6-mcastprefix;ff02::1 ip6-allnodes;ff02::2 ip6-allrouters;ff02::3 ip6-allhosts;10.1.2.3 my-on-prem-equipment"
            },
            "Ztsi": {
                "__t": "c",
                "DesiredServiceUrl": "https://my-attestation-endpoint",
                "DesiredEnabled": true
            },
            "$version": 8
        },
        "reported": {
            "Firewall": {
                "__t": "c",
                "FirewallState": 1,
                "FirewallFingerprint": "86cadaa93d8c0e0ee42e36190413265c2e218c87a0c494c026cf7de601765e72"
            },
            "CommandRunner": {
                "__t": "c",
                "CommandStatus": {
                    "CommandId": "set_timezone__And_report_back",
                    "ResultCode": 0,
                    "TextResult": "",
                    "CurrentState": 2
                },
                "CommandArguments": {
                    "value": {
                        "CommandId": "set_timezone__And_report_back",
                        "Arguments": "timedatectl set-timezone UTC; timedatect l grep zone | tr -d '[:space:]'",
                        "SingleLineTextResult": false,
                        "Action": 3
                    },
                    "ac": 200,
                    "ad": "-",
                    "av": 4
                }
            },
            "HostName": {
                "__t": "c",
                "Name": "NewHostName123",
                "Hosts": "127.0.0.1 localhost;::1 ip6-localhost ip6-loopback;fe00::0 ip6-localnet;ff00::0 ip6-mcastprefix;ff02::1 ip6-allnodes;ff02::2 ip6-allrouters;ff02::3 ip6-allhosts;10.1.2.3 my-on-prem-equipment",
                "DesiredName": {
                    "value": "NewHostName123",
                    "ac": 200,
                    "ad": "-",
                    "av": 5
                },
                "DesiredHosts": {
                    "value": "127.0.0.1 localhost;::1 ip6-localhost ip6-loopback;fe00::0 ip6-localnet;ff00::0 ip6-mcastprefix;ff02::1 ip6-allnodes;ff02::2 ip6-allrouters;ff02::3 ip6-allhosts;10.1.2.3 my-on-prem-equipment",
                    "ac": 200,
                    "ad": "-",
                    "av": 6
                }
            },
            "Networking": {
                "__t": "c",
                "NetworkConfiguration": {
                    "InterfaceTypes": "eth0=ether;lo=loopback",
                    "MacAddresses": "eth0=00:22:48:7b:04:39;lo=00:00:00:00:00:00",
                    "IpAddresses": "eth0=10.0.0.6,fe80::222:48ff:fe7b:439;lo=127.0.0.1,::1",
                    "SubnetMasks": "eth0=/24,/64;lo=/8,/128",
                    "DefaultGateways": "eth0=10.0.0.1",
                    "DnsServers": "eth0=168.63.129.16",
                    "DhcpEnabled": "eth0=false;lo=false",
                    "Enabled": "eth0=true;lo=unknown",
                    "Connected": "eth0=true;lo=true"
                }
            },
            "Tpm": {
                "__t": "c",
                "TpmStatus": 2
            },
            "Ztsi": {
                "__t": "c",
                "Enabled": 0,
                "ServiceUrl": "",
                "DesiredServiceUrl": {
                    "value": "https://my-attestation-endpoint",
                    "ac": 200,
                    "ad": "-",
                    "av": 7
                },
                "DesiredEnabled": {
                    "value": true,
                    "ac": 200,
                    "ad": "-",
                    "av": 8
                }
            },
            "Settings": {
                "__t": "c",
                "DeviceHealthTelemetryConfiguration": {
                    "value": 2,
                    "ac": 200,
                    "ad": "-",
                    "av": 2
                },
                "DeliveryOptimizationPolicies": {
                    "value": {
                        "PercentageDownloadThrottle": 50,
                        "CacheHostSource": 1,
                        "CacheHost": "http://mycachehost"
                    },
                    "ac": 400,
                    "ad": "-",
                    "av": 3
                }
            },
            "$version": 20
        }
    }
}

Next steps

For an overview of OSConfig scenarios and capabilities, see:

For specific practical examples, see: