PDA

View Full Version : 2.24 - How can I find the full hostname (FQDN)?


Loco
07-24-2002, 10:46 PM
Taken from the original FAQ.

2.24 - How can I find the full hostname (FQDN) of the system I'm running on?
From Richard Stevens (rstevens@noao.edu):

Some systems set the hostname to the FQDN and others set it to just the unqualified host name. I know the current BIND FAQ recommends the FQDN, but most Solaris systems, for example, tend to use only the unqualified host name.

Regardless, the way around this is to first get the host's name (perhaps an FQDN, perhaps unaualified). Most systems support the Posix way to do this using uname(), but older BSD systems only provide gethostname(). Call gethostbyname() to find your IP address. Then take the IP address and call gethostbyaddr(). The h_name member of the hostent{} should then be your FQDN.

From: Stanislav Shalunov

Since the DNS can be very slow in some setups it might be preferable to try to use uname() if it is supported. If the program is supposed to be portable then GNU autoconf would be the way to go. (The results of not doing this can be quite annoying: e.g., GNU Emacs does a reverse lookup to find FQDN on startup, which means that if the nameserver is not local and the system has no network connection at the moment you'd have to wait a long time to see the editor started.)

From: Erik Landry

Instead of using uname(2)/gethostname(2), gethostbyname(3), and gethostbyaddr(3) which does two DNS lookups, I suggest using socket(2) (with AF_INET), bind(2) (with INADDR_ANY) (or even just socketpair(2)), getsockname(2), and then the gethostbyaddr(3) which does only one DNS lookup.

Of course, using this method is bound to have several different implications than the DNS/reverse DNS method (e.g., even though DNS is usually implemented via socket calls there may be machines where DNS calls are available but the socket interfaces are not).

From: Jan Echternach

Some systems don't have a FQDN. And even if a system has
one, gethostbyname()/gethostbyaddr() might return the
unqualified host name instead. One one system,
gethostname() + gethostbyname() may be enough to get the
FQDN, on a different system this can return the unqualified
host name, and gethostname() + gethostbyname()
+ gehostbyaddr() can even return "localhost".

I suggest you check that the name returned by
gethostbyname() or gethostbyaddr() actually looks like a
FQDN, i.e. contains a dot, and ask the user to supply the
FQDN if it didn't.

From: Mourad
how about a simple `hostname -f` as an execv or system parameter