Hostname changes in the terminal when connecting to some wifi networks

When I connect to some wifi networks my computer hostname changes in the terminal - is this normal? Is there a way I can prevent this from happening?


On Lion (at least), the best way to achieve this is by running scutil:

sudo scutil --set HostName NAME

replacing NAME with the hostname you want. Note that /etc/hostconfig is deprecated, if you can believe a comment at the top of the file.


OS X normally gets the hostname from a reverse lookup of the IP address the machine has. When you're on DHCP, that means your hostname can change. If you want to force a hostname, you can edit /etc/hostconfig (use the cli editor of your choice, you'll need to use sudo) and change HOSTNAME=-AUTOMATIC- to HOSTNAME=NameYouWant