Mac OS X hostname determination

I recently came across this information in an Apple PDF worksheet and couldn’t find anything via Google that really laid out the full situation, so I thought I would write this up for posterity.

In Mac OS X 10.4.x Server (and the client version I believe [and possibly earlier versions], by default your hostname is determined dynamically. This is controlled by the following line in the file /etc/hostconfig:

HOSTNAME=-AUTOMATIC-

(note that in my Mac OS X 10.4.10 client this line is absent from /etc/hostconfig so I assume that absence defaults to automatic as well). If you want to your hostname to be static, just change -AUTOMATIC- to whatever fully qualified hostname you want like foo.example.com.

When the hostname is determined automatically, the OS steps through this list of possibilities and goes with the first valid name found:

  1. The name provided by the DHCP or BootP server for the primary IP address
  2. The first name returned by a reverse DNS (address-to-name) query for the primary IP address
  3. The local hostname (set in the Sharing pane of System Preferences)
  4. The name localhost

So this means if you get different IP addresses at home, work, and on the road you will see your hostname bounce around, which can be disconcerting if you aren’t expecting your shell prompt to change (wave to Philip :)).

Update: I changed the hostname of a Mac OS X 10.5 Server, but in certain places (such as the output of the hostname command) I would still get the old hostname. Turns out that there is a preference file that didn’t get updated for some reason:/Library/Preferences/SystemConfiguration/preferences.plist. I found a reference to the old hostname there, and after editing the file and rebooting, it appears that the last traces of the old hostname are gone.

Thanks to this post from this thread on maxosxhints.com for the pointer on how to fix this.

About these ads

20 thoughts on “Mac OS X hostname determination

  1. Robert Brewer Post author

    The hostname in Sharing preferences is only used in step 3 above. If you get a name via DHCP or reverse DNS, that name will be used instead of what you have in Sharing preferences. Which is my whole point.

    Reply
  2. Miran

    I had the strange problem with setting the hostname permanently in /etc/hostconfig. In this configuration, the name was indeed allways the same, but the Mac didn’t send it to the DHCP server (DNSMASQ as DHCP and DNS server on a dd-wrt based Linksys router device, a very nice thing for home networks). Without an appropriate DNS entry, I wasn’t able to access my Mac.

    So I deleted the entry in /etc/hostconfig and made an entry in the Sharing pane instead. It worked and it made my day, because steps 1) and 2) do not occur in my home network. However, I still do not understand why the /etc/hostconfig variant didn’t work. Any ideas?

    Reply
  3. Aaron T

    Miran has a good point…

    We are using dnsmasq at my work, and the /etc/hostconfig entry seems (so far) not to be sent to dnsmasq. Which is annoying.
    In fact, it appears there are roughly 3 places a hostname can be stored:

    1) /etc/hostconfig/
    2) Sharing PrefPane (the first box for Computer Name)
    3) Sharing PrefPane (from the ‘Edit’ button, the Local Hostname)

    So far, I think that 1) is good for setting your hostname as it appears in Terminal.
    And I believe 3) is meant for Bonjour (the .local enables it to be picked up by Bonjour), but I’ve seen where 2) and 3) differ, and 2) is actually seen by Bonjour in Finder (from other computers on the local network).

    I’m going to keep researching…

    As a SysAdmin, I find that sometimes dnsmasq will do something strange with laptops – with 2 network interfaces, it seems to think ‘my-laptop’ is already taken as a dns name, so it gives out ‘my-laptop-2′ or ‘my-laptop (2)’. This is an issue when I’m trying to get to machines and their names are all messed up with spaces, parentheses, etc. that I have to comment out in scripts and what-not!
    —–
    I’d like a reliable way to permanently set a computer name for each machine on the network…

    Reply
  4. Aaron T

    Okay – got some good information….

    There ARE 3 names for your computer, as documented here:
    http://images.apple.com/server/docs/Worksheet_v10.4.pdf
    (READ TOP OF PAGE 5!)

    and also here:
    http://www.afp548.com/article.php?story=20060503145111816
    (See PuLSe’s post, although server-related, it applies mostly to clients too)

    The scutil command will set the following 3 items:
    1) ComputerName (AppleTalk Name, SharingPrefPane, “Computer Name”)
    2) LocalHostName (‘DNS’ name, how to get to a computer on a network by hostname, the ‘Edit’ button in Sharing PrefPane)
    3) HostName (UNIX hostname, what shows up in terminal.)

    For 1 and 2, scutil will make changes to this file: /Library/Preferences/SystemConfiguration/preferences.plist (Root -> System -> System)

    SharingPrefPane ALSO makes changes to this .plist

    So, if you’re a SysAdmin, like me, you can set ComputerName and LocalHostName by command line using:
    sudo scutil –set ComputerName compname
    sudo scutil –set LocalHostName compname

    and sudo scutil –get ComputerName to retrieve the change.
    You can see it immediately changed in the .plist

    More to follow if I discover other things….

    Reply
  5. Pingback: Definindo estaticamente o hostname no OS X | Henrique Bastos.NET

  6. Pingback: News Flash: MacOS NDG Either « TechArcana

  7. Pingback: Fix for random hostnames on Mac OS X | learn

  8. Greg

    Excellent thread and comments, thanks everyone, Apple needs to fix this. It’s a good example of how you can’t do everything from their gui.

    Reply
  9. Josh The Geek

    This is really annoying. Every now and then, I open Terminal and see my hostname is wrong. I use apache and have many local test sites, and it is always changed to one of the site addresses. In console, I see ‘setting hostname to ‘macbook-pro.local”, and less than five seconds later, ‘setting hostname to ‘test1.wp.localhost”, always by configd. What’s interesting is when firefox logs something about the same address, which I’ve not accessed in a month. ??? I use scutil –set HostName macbook-pro.local to reset it. This helps, now I wont need to. Thanks!

    Reply
  10. Pingback: Domain Name

  11. Heiko Haller

    Hi,
    in Lion, the first line of /etc/hostconfig is:

    # This file is going away

    So, I’m somewhat hesitant to use this fix…
    Any ideas how this is supposed to be fixed in Lion?

    Reply
  12. Robert Brewer Post author

    @Heiko, it says that on Snow Leopard too. I assume it means that at some point in the future, they plan to put this functionality elsewhere. I haven’t tried this on Lion, so I suggest giving it a try. Report back to let us know whether it worked or not! :)

    Reply
  13. Aldy Waani

    Hi there, this article is realy helpfull. Currently i’m installing a network license on my Macbook Pro, and it’s require hostname and computer ID (MAC Address).. even if it’s set to ‘localhost’ or 127.0.0.1 when registering the license it still get an error since i’m connected to my company’s network that has a windows domain configuration.. Changing the /etc/hosts file won’t help.
    So i will try this method later on..

    Many thanks

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s