Tuesday, March 13, 2012

ComputerName vs. LocalHostName vs. HostName on OS X

Hostnames on OS X are...complicated. Using scutil is probably the easiest way to get at them, although there are others. It looks like scutil just parses this plist:

/Library/Preferences/SystemConfiguration/preferences.plist

Note: If you want to get/set these values without shelling out to scutil, check out SCPreferencesPathGetValue and SCPreferencesPathSetValue part of the SystemConfiguration Framework called from python like this.

ComputerName


This is the computer name visible in the GUI in Preferences | Sharing | Computer Name and the scutil manpage describes it as 'The user-friendly name for the system.'
scutil --get ComputerName
Apple says:
The AppleTalk name and the default name used for SLP/DA. The Network browser in the Finder uses SMB/CIFS to find computers that provide Windows file sharing.
SCPreferencesPath: /System/System/ComputerName

LocalHostName


This appears to be only used for Bonjour-aware services on the local network.
scutil --get LocalHostName
Apple says:
The name that designates a computer on a local subnet.
and as of 10.6:
Host names that contain only one label in addition to local, for example "My-Computer.local", are resolved using Multicast DNS (Bonjour) by default. Host names that contain two or more labels in addition to local, for example "server.domain.local", are resolved using a DNS server by default.
SCPreferencesPath: /System/Network/HostNames/LocalHostName

HostName


The name associated with hostname(1) and gethostname(3). Displayed in the default terminal command prompt (user@hostname).
scutil --get HostName
Apple says:
You can’t specify this name during server setup. Server Assistant sets the host name to AUTOMATIC in /etc/hostconfig. This setting causes the server’s host name to be the first name that’s true in this list:
- The name provided by the DHCP or BootP server for the primary IP address
- The first name returned by a reverse DNS (address-to-name) query for the primary IP address
- The local hostname
- The name “localhost”
SCPreferencesPath: /System/System/HostName

6 comments:

Sam said...

Best overview of these overly complicated yet similar names in OS X. Thank you.

Anonymous said...

There is one more name that you don't have listed. I was doing a network IP scan, and the OSX machines were showing up with names other than the ones you have listed. The IP scanner was finding the NetBios name which is listed on the WINS tab of the network settings.

Anonymous said...

And yet another computer name would be the Active Directory computer object name (if your macs bind to AD) as determined by this command:

dsconfigad -show | grep -i 'Computer' | awk -F "= " '{printf $NF}'



Anonymous said...

I've muddled through the mystery of OS X machine names for months. This quick post does the best job of demystifying them that I've seen yet. Thanks.

Anonymous said...

This should get the NetBIOS name:
sudo defaults read /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName

Anonymous said...

Thank you, just what I have been looking for. Very helpful.