How can I stop Mac OS X overriding my hostname when I receive a DHCP request on Snow Leopard?

83

49

Whenever I go on a network where the DHCP server assigns hostnames then it overrides the settings I've set for my hostname on my Mac in the "Sharing" section of System Preferences.

How do I stop this behaviour from occurring and always have the same hostname set under Snow Leopard?

Mike McQuaid

Posted 2009-10-02T08:58:20.073

Reputation: 3 639

Answers

27

In short: there is an auto-magic behavior that Mac OS uses, by default.

You can turn it off in /etc/hostconfig.

http://excitedcuriosity.wordpress.com/2007/08/24/mac-os-x-hostname-determination/

I like the answer by Jack M., but it doesn't work in many environments of the real-world DHCP environments, because you do not have a pre-existing relationship with the DHCP server. Only in a home setup or a corporate setup (where the IT people are helpful), can you get your desired hostname via DHCP.


In /etc/hostconfig add something like this:

HOSTNAME=your_hostname.your_domain.your_tld

If you want to set it at automatic again, either remove the line or set it to -AUTOMATIC-

benc

Posted 2009-10-02T08:58:20.073

Reputation: 1 272

5For 10.6+, see @Wolf's answer – etarion – 2012-01-20T18:29:11.650

2This answer needs updating for 10.6. I'm looking at 10.5, and is already very different. – benc – 2009-12-22T08:42:04.360

131

Set it in the Terminal with:

sudo scutil --set HostName <putinyourhostname_or_fqdn_here>

like in:

sudo scutil --set HostName server1.mynetwork.com

Wolf

Posted 2009-10-02T08:58:20.073

Reputation: 2 425

3Verified that this also works on Leopard (Mac OS X 10.5.8). – Vebjorn Ljosa – 2010-01-30T21:33:02.457

Working on 10.10. – Justin Megawarne – 2014-10-26T21:27:52.347

Doesn't really work on 10.10. Now my Mac answers to both hostnames. System settings still shows the one with the extra number, scutil in terminal shows the hostname without extra number. – Arne – 2014-11-03T20:17:13.457

Works for me on 10.10. – Dan Robson – 2015-04-23T14:04:19.123

3Didn't need sudo in Snow Leopard (Mac OS X 10.6.2) – Garth Kidd – 2010-04-14T06:15:09.267

2Working on 10.11. – Nupraptor – 2015-10-19T16:49:30.030

@JakeGould , @Wolf : A better method to set all hostname types is to use this one-liner: export MY_HOSTNAME=foobar ; for n in LocalHostName ComputerName HostName ; do sudo scutil --set $n $MY_HOSTNAME; done – TrinitronX – 2016-12-30T22:04:49.753

works great, hostname now shows up on the status page for my dd-wrt flashed router. – Robert S Ciaccio – 2010-08-25T06:14:28.193

2Works in Lion (10.7.2) too – etarion – 2012-01-20T18:29:44.813

in 10.7.4 I get a > prompt after this commmand – CharlesB – 2012-06-28T13:25:08.180

Anyone know what the default is or how to reset the default? – wfaulk – 2012-08-10T17:50:42.527

2The default, as it turns out is unset. The command to unset is scutil --set Hostname "". – wfaulk – 2012-08-10T18:07:43.003

5Verified that this also works on Mountain Lion (10.8.2) – DerMike – 2013-01-28T13:00:24.570

Didn't necessarily need sudo under Mountain Lion (10.8.3), but I did get a GUI prompt for my password. – Blacklight Shining – 2013-05-08T15:59:40.110

12Works on 10.9.... – Dan Pritts – 2013-10-29T02:28:05.447

18

Depending on how your DHCP is set up, you may be able to use the "DHCP client ID".

  1. System Preferences.
  2. Network
  3. Select your network adapter on the left.
  4. Select "Advanced" button at the bottom.
  5. Set the "DHCP client ID" to your hostname.

If your DHCP server supports it, your hostname will be used for your machine.

Jack M.

Posted 2009-10-02T08:58:20.073

Reputation: 3 133

this did not work for me... scutil did however. And after using scutil, this field was filled. Strange. – Robert S Ciaccio – 2010-08-24T11:21:54.827

What if my DHCP server does support it? Every other OS I've used allows me to set my hostname myself, are you saying this is server dependant on OSX? – Mike McQuaid – 2009-10-05T09:17:54.490

This should make your hostname stick, unless your DHCP server is doing something really odd. I have my box called "shteef" in both Sharing (as you mentioned) and in the DHCP Client ID, and it works fine. – Jack M. – 2009-10-05T15:27:28.777

3@JackM. DHCP client ID just means that the client ID is sent to the server during the DHCP negotiation; there is no requirement for a supplied client ID to make it to DNS. – zigg – 2013-02-18T17:06:26.577

3

Actually it's not related to DHCP. OS X checks the reverse DNS record of your IP and if there's one it's applied as your hostname.

slash213

Posted 2009-10-02T08:58:20.073

Reputation: 39

2I realize this is an old answer, but do you know if there's any way to stop this behavior? – computergeek125 – 2016-11-17T06:25:06.933

2

The Sharing panel doesn't necessarily set your hostname, it set's your bonjour host name, and is the default hostname. DHCP can and probably will override the default hostname. I don't know of anyway to override it, but maybe someone else does.

What I have done in a similiar situation is set up a DYNDNS account, and load the DYNDNS updater on your system. Then you can use the DYNDNS hostname from anywhere, including your local lan without worrying about what your dhcp hostname is...

Benjamin Schollnick

Posted 2009-10-02T08:58:20.073

Reputation: 4 313