Unix administration - host $(/bin/hostname) differs from gethostbyname(HOSTNAME)

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > June 2007 > host $(/bin/hostname) differs from gethostbyname(HOSTNAME)





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author host $(/bin/hostname) differs from gethostbyname(HOSTNAME)
jrw32982@gmail.com

2007-06-07, 1:21 pm

Can someone explain (roughly) the algorithm that the host command uses
to determine the IP address?

On my server, /etc/hosts maps 127.0.0.1 to HOSTNAME, and
gethostbyname(HOSTNAME) returns 127.0.0.1 as expected. However, the
host command, when given HOSTNAME, returns the actual IP address. Is
the host command bypassing /etc/hosts and/or not using gethostbyname?
Is the host command going directly to the DNS server to get the IP
address?

BTW, I'm trying to convince my sysadmin that /etc/hosts should not map
127.0.0.1 to HOSTNAME (unless the server is not on a network) because
of this exact problem, but he's not convinced because he's not seeing
any ill effects. Only my code, which uses gethostbyname(HOSTNAME), is
exhibiting the problem of finding the wrong IP address.

Thanks!

Barry Margolin

2007-06-08, 1:19 am

In article <1181230431.195298.186430@q75g2000hsh.googlegroups.com>,
jrw32982@gmail.com wrote:

> Can someone explain (roughly) the algorithm that the host command uses
> to determine the IP address?


It uses DNS. It's part of the BIND package, which is just DNS server
and client software.

> On my server, /etc/hosts maps 127.0.0.1 to HOSTNAME, and
> gethostbyname(HOSTNAME) returns 127.0.0.1 as expected. However, the
> host command, when given HOSTNAME, returns the actual IP address. Is
> the host command bypassing /etc/hosts and/or not using gethostbyname?
> Is the host command going directly to the DNS server to get the IP
> address?


gethostbyname() uses whatever methods are specified for host resolution
in /etc/nsswitch.conf. It's a general-purpose name resolution library,
which can look in /etc/hosts, use DNS, use NIS, etc.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
jrw32982@gmail.com

2007-06-08, 1:19 am

On Jun 7, 9:33 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
> gethostbyname() uses whatever methods are specified for host resolution
> in /etc/nsswitch.conf. It's a general-purpose name resolution library,
> which can look in /etc/hosts, use DNS, use NIS, etc.


Thanks for your reply. I'm trying to determine the names/aliases/IP
addresses for the current server by recursively calling
gethostbyname() and gethostbyaddr(), starting with $(/bin/hostname).
However, my sysadmin has set up /etc/hosts (which is specified before
DNS in /etc/nsswitch.conf) to contain HOSTNAME on the 127.0.0.1 line.
This thwarts my attempt to determine the real IP address for HOSTNAME
which is specified later in /etc/hosts.

Is there some other API that I should call to get aliases and IP
addresses for HOSTNAME? Or should I tell my sysadmin to remove
HOSTNAME from the 127.0.0.1 line? I am unable to find a definitive
reference which says that a server (on a network) should not have
HOSTNAME on the 127.0.0.1 line.

-- John

Barry Margolin

2007-06-09, 1:28 am

In article <1181276912.752856.65540@p47g2000hsd.googlegroups.com>,
jrw32982@gmail.com wrote:

> On Jun 7, 9:33 pm, Barry Margolin <bar...@alum.mit.edu> wrote:
>
> Thanks for your reply. I'm trying to determine the names/aliases/IP
> addresses for the current server by recursively calling
> gethostbyname() and gethostbyaddr(), starting with $(/bin/hostname).
> However, my sysadmin has set up /etc/hosts (which is specified before
> DNS in /etc/nsswitch.conf) to contain HOSTNAME on the 127.0.0.1 line.
> This thwarts my attempt to determine the real IP address for HOSTNAME
> which is specified later in /etc/hosts.
>
> Is there some other API that I should call to get aliases and IP
> addresses for HOSTNAME? Or should I tell my sysadmin to remove
> HOSTNAME from the 127.0.0.1 line? I am unable to find a definitive
> reference which says that a server (on a network) should not have
> HOSTNAME on the 127.0.0.1 line.
>
> -- John


You could use the DNS resolver library: man resolver

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com