Enforce macOS Hostname to Hardware Serial Number

Change Hostname to Serial Number

Enforce macOS HostName, LocalHostName, and ComputerName to match the hardware serial number on every endpoint.

What the macOS hostname enforcer does

This Automox Worklet™ enforces the three macOS naming properties so all three match the endpoint's hardware serial number. The properties are HostName (the BSD network hostname), LocalHostName (the Bonjour name visible on the local network), and ComputerName (the friendly name shown in Sharing preferences and Finder). The serial number is read directly from IOKit using ioreg, which makes it a stable, unique identifier that survives reimaging, OS upgrades, and reassignment between users.

The Worklet is idempotent. Each evaluation reads the current values with scutil --get and compares them against the serial number captured from the IOPlatformSerialNumber registry property. Endpoints that already match are reported compliant and skipped. Endpoints that have drifted are flagged for remediation on the next policy run.

Why enforce serial-number hostnames at fleet scale

User-assigned hostnames like Johns-MacBook-Pro break asset correlation across the systems IT relies on. All systems key off the hostname or ComputerName field, so two laptops with overlapping names produce duplicate or stale records. Hostname drift compounds in three predictable ways on macOS: users rename their machines in System Settings, Migration Assistant restores a name from a backup, and poorly written onboarding scripts rewrite ComputerName without touching HostName or LocalHostName.

How macOS hostname enforcement works

  1. Evaluation phase: The script reads the serial number using ioreg -l | awk '/IOPlatformSerialNumber/ { print $4 }' | sed 's/"//g'. It checks each property (HostName, LocalHostName, ComputerName) with scutil --get. If any one value does not equal the serial number, the script echoes a diagnostic line.

  2. Remediation phase: The script runs scutil --set on each property out of compliance. It then runs dscacheutil -flushcache to ensure clients pick up the new LocalHostName without waiting for a TTL.

macOS hostname enforcement requirements

Expected state after macOS hostname enforcement

After remediation, scutil --get HostName, scutil --get LocalHostName, and scutil --get ComputerName all return the same value: the endpoint's hardware serial number. This change persists across reboots and system updates. Validate a representative endpoint by cross-referencing the values to capture the Automox activity log entry for audit evidence.