Setting a fixed hostname alias on mac overriding DHCP

Posted: 2013-02-08

This turned up as an issue when developing webapps.

If you set a fixed hostname ( for example) via an /etc/hosts alias to (perhaps you need to have access to cookies or similar) then you can browse localhost via the name.

However - when starting tomcat under java this gave an error on hostname/url. I've had the issue running tomcat 6 under java 7 - have had reports of tomcat 7 under java 6 also being an issue.

It turns out that Mac OSX takes the hostname given by DHCP - and this can change often.

It seems to be trying to do some kind of network lookup/matching and won't start tomcat unless it finds a good match.

So - you need to force the mac to ignore hostname allocation by setting a fixed hostname.

For Lion and Mountain Lion:

scutil --set HostName

Previous versions of OSX had some settings in /etc/hostconfig - but I don't have an old enough install to test on.

YMMV on whether you need sudo or not for editing /etc/hosts and scutil call

Tip: Don't pick an alias that's an existing host - it'll prevent you reaching that host by name :)