[erlang-questions] detection of hostname

Raimo Niskanen <>
Wed Oct 24 09:21:58 CEST 2012


On Tue, Oct 23, 2012 at 08:32:57AM +0200, Vlad Dumitrescu wrote:
> Thank you all for the advice! I will use some of it to improve the detection.
> 
> I would still want to know how is it possible to run "erl -name foo"
> and "erl -name bar" and these nodes can connect to each other all
> right (the hostname erlang assigns being "desktop.domain.com"), but on
> the same machine "ping desktop.domain.com" can't resolve the name...

This problem of yours is a mess, and there is no simple answer.

The hostname is a property of the operating system. On Unixes it can
be retreived with the gethostname(2) syscall. That is what
inet:gethostname/0 does, also removing the domain name
if it is contained in the hostname. On BSD it is customary to
configure the fully qualified domain name as the hostname,
but not necessary. Therefore inet:gethostname/0 returns the
short hostname as it is more portable.

When a node starts it reads the hostname and uses it as a
base for the node name if neded. If the hostname contains
a domain name that is used as the domain name for the node,
if needed. If the node starts distributed with long names
some system configuration files (e.g. /etc/resolv.conf)
are parsed to try to find a domain name. On Unix, if the
domain name still is unknown, the short hostname is looked up
using the native lookup method (per default) to try to find
the domain name.

The process is also customizable through the inetrc file and
some environment variables. It is all in inet_config:init/0,
if the documentation (ERTS User's Guide, Inet configuration)
is not enough.

Erlang tries to do it's best to find the hostname and domain name,
but there are so many ways to misconfigure a system, some perfectly
valid. For instance the FQDN the machine thinks it has may be invalid
from the Internet because company policy want to hide network topology
and individual machines.

So, the short hostname returned by inet:gethostname() is the best guess.
The domain name returned by inet_db:res_option(domain) is the best
that can be guessed without knowing which interface and which IP address
that is supposed to be external. The default name lookup method (native)
is the one most likely to work since it is the same as the OS uses
and the one the system administrator has configured and is responsible for.

> 
> The conclusion I draw is that the only way to know is to start a node
> and try to connect to it.
> 
> best regards,
> Vlad
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list