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:
- 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 (set in the Sharing pane of System Preferences)
- 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.
September 12, 2007 at 4:23 am |
Uhm… just go to System Prefs, Sharing. You can set the host name there.
September 30, 2007 at 8:24 pm |
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.
January 10, 2008 at 10:00 am |
Thanks for posting this!
I could never figure out what process OS X was using to determine my hostname and it was driving me crazy.
April 6, 2008 at 10:47 am |
Setting your hostname permanently on Mac OSX 10.4
You can set your hostname permanently on OS X 10.4 by editing the file /etc/hostconfig and adding a HOSTNAME declaration:
jeffg@eko jeffg> cat /etc/hostconfig
HOSTNAME=eko
April 21, 2008 at 1:33 am |
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?
August 12, 2008 at 2:30 pm |
This has been driving me insane for days. Thank you very much for posting this!
October 23, 2008 at 8:14 am |
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…
October 23, 2008 at 8:39 am |
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….
January 14, 2009 at 11:32 pm |
[...] Pesquisando na internet, encontrei este post do Robert Brewer que deixa a dica para resolver este inconveniente. [...]
March 14, 2009 at 1:50 am |
[...] bash prompts started showing up as “108:~” Turns out this was due to the Mac’s goofy way of choosing a hostname combined with the fact that when I rebooted my machine I was connected at a TMobile hotspot. Adding [...]