Skip to content

Instantly share code, notes, and snippets.

@algal
Last active September 5, 2018 02:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save algal/0dd167c196b4af3dc06c2b57d6f05245 to your computer and use it in GitHub Desktop.
Save algal/0dd167c196b4af3dc06c2b57d6f05245 to your computer and use it in GitHub Desktop.
A note on how a Mac gets its various names

Names on macOS

Since I couldn't find documentation on how a Mac machine gets its name, I thought I'd write up these notes for my own reference later.

A macOS machine has a few different names, which might or might not be equal.

1. ComputerName

Every machine has a pretty user-facing name. This is the ComputerName.

It's what you access by System Preferences / Sharing / Computer Name, by networksetup -getcomputername, or by scutil --get ComputerName. For instance, you could name your machine "apollo".

2. LocalHostName

Every machine also has a LocalHostName. This is the name used to form the system's multicast DNS name, which will be a string like "apollo.local.". This is the name seen by other machines via AirDrop and Bonjour-distributed services.

If it's not set explicitly, this name is generated by character-simplifying the ComputerName.

3. The inferred "network name"

Every machine may also have what I will call a network name, which is a plain old unicast DNS name which it recommends for reaching the system on the local network.

This is the name which appears in macOS's Sharing pane in System Preferences, as part of the SSH connection string for Remote Login, the VNC URL for Screen Sharing, and the SMB URL for File Sharing. (This name may also define the NetBIOS name reported as currently being used -- I'm not sure.)

Since this is the name that macOS advises you to give other systems in order for them to reach you, what matters is that this is what will work for those systems as opposed to what this machine wants to be called. So this network name is not defined by local configurations but by the surrounding network environment.

Specifically, macOS seems to use the following rule. On startup, a machine decides its network name by using the first item on this list that is available:

  1. The name it was assigned by its DHCP server (I'm not 100% sure of this.)
  2. The name returned by doing a reverse-DNS lookup on its IP address, from its DNS server

If there's a network name, then the Sharing pane recommends using it. Otherwise, it recommends using a numerical IP address.

4. The effective hostname

Finally, every machine also has a hostname.

This is the name returned by the hostname -s command, which is also the value of bash's $HOSTNAME variable (therefore appearing on the prompt). The rule for determining this seems to be to take the first of these which is available:

  1. The inferred network name
  2. The value of HostName
  3. The value of LocalHostName

How to check your system's names

These various names live in various places. This script will expose them all:

#!/bin/sh

echo
echo '==== System Configuration, ComputerName:'
scutil --get ComputerName

echo
echo '==== System Configuration, LocalHostName:'
scutil --get LocalHostName

echo
echo '==== System Configuration, HostName:'
scutil --get HostName

echo
echo '==== hostname command:'
hostname

echo
echo '==== System Configuration, NetBIOS name:'
echo 'show State:/Network/NetBIOS foo' | scutil

echo
echo '==== SystemConfiguration plist file reports various Name':
plutil -p /Library/Preferences/SystemConfiguration/preferences.plist | grep '"HostName"'
plutil -p /Library/Preferences/SystemConfiguration/preferences.plist | grep '"LocalHostName"'
plutil -p /Library/Preferences/SystemConfiguration/preferences.plist | grep '"ComputerName"'

echo
echo '==== nvram reports computer-name'
nvram -p | grep computer-name

echo
echo '==== System Configuration, Setup:/System :'
echo 'show Setup:/System foo' | scutil

echo
echo '==== System Configuration, Setup:/Network/HostNames :'
echo 'show Setup:/Network/HostNames foo' | scutil

# echo
# echo '==== System Configuration, Setup:/Network/HostNames/LocalHostName :'
# echo 'show Setup:/Network/HostNames LocalHostName' | scutil

echo
echo '==== defaults system, value for NetBIOSName:'
defaults read /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName

echo
echo '==== NetworkSetup reports ComputerName:'
networksetup -getcomputername

echo
echo '==== Active Directory computer object name:'
dsconfigad -show | grep -i 'Computer' | awk -F "= " '{printf $NF}'

How to rename your system

In summary:

  1. If you want to rename your system, first try doing it from System Preferences / Sharing / Computer Name.
  2. If this doesn't work, and you're still seeing an undesired name for your sharing services, then you probably need to reconfigure your router to give you a different name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment