macOS uses different names for the system and for local network discovery. The scutil utility sets macOS’s HostName, distinct from its display name and Bonjour local hostname.
Apple’s naming controls in System Settings affect other fields. General → About contains the computer name, represented by ComputerName, while General → Sharing contains the Bonjour local hostname, represented by LocalHostName. Editing either field is different from explicitly setting HostName.
Saving a HostName requires administrator privileges and makes the setting persist across restarts. The change can affect scripts or services that identify the Mac by its hostname, but it does not create a DNS record that lets other computers reach the new name.
Steps to change the hostname on macOS:
- Record the saved HostName setting in Terminal before changing it. Example output:
$ scutil --get HostName old-mac
If the result is HostName: not set, there is no explicit saved HostName override; the command returns a nonzero exit status in that case. That absence is the previous setting to preserve for recovery.
- Check the active hostname before making the change. Example output:
$ hostname old-mac
The active hostname can exist even when HostName is not set. Copying that active name back later would create an explicit override rather than restore the unset state.
- Choose a hostname such as mac-workstation that starts with a letter, ends with a letter or digit, and contains only letters, digits or hyphens.
- Set HostName to the chosen name with administrator privileges. Example output:
$ sudo scutil --set HostName mac-workstation
Here, mac-workstation stands for your chosen name. A successful change normally produces no output.
If you need to undo the change, the recovery command restores the saved value recorded earlier, represented here by old-mac. Example output:
$ sudo scutil --set HostName "old-mac"
If the original result was HostName: not set, an empty value removes the override instead. Example output:
$ sudo scutil --set HostName ""
After recovery, scutil --get HostName should show the original saved value or HostName: not set, respectively; hostname shows the resulting active name.
- Check that the saved HostName matches the chosen name. Example output:
$ scutil --get HostName mac-workstation
- Confirm that the active hostname matches the saved HostName. Example output:
$ hostname mac-workstation
Matching values confirm that the saved setting and the active hostname agree. ComputerName and LocalHostName can retain their previous values. A shell prompt may still display an older name and is not a substitute for the hostname result.