[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Determining FQDN (was Re: Yow, Madduck!)



Lo, on Thursday, January 10, dman did write:

<SNIP>

One minor nit to pick from an otherwise very good explanation (and I
wouldn't bother, except that I've been bitten by this before).

> This directive tells exim to use that name as the hostname in the SMTP
> greeting (HELO/EHLO) instead of that reported by the gethostbyname() C
> function (which returns the first thing after 127.0.0.1 in
> /etc/hosts).

<SNIP>

Not quite.  gethostbyname() returns the host record for whatever name or
address you supply as a parameter.  I think, although I'm not actually
certain, that by default, MTAs like exim use the machine's FQDN on the
HELO/EHLO line.  Finding the FQDN is a somewhat complicated process:

1) Find the machine's local hostname (e.g., in my case, ankh-morpork).
   This is stored in a kernel variable which root can set with
   hostname(1); it's initialized on boot from the contents of
   /etc/hostname.  To check this value, run either `hostname' or `uname
   -n' from the command line, or use the uname(2) system call in a C
   program.  (It'll be in the nodename field of the utsname struct.)

2) Pass this value to gethostbyname(3), which resolves it to an IP
   address, then determines the canonical hostname for that IP.  This
   resolution and lookup follows the normal mechanism for host lookups:
   DNS, /etc/hosts, NIS, whatever (see /etc/nsswitch.conf and
   /etc/resolv.conf).  This canonical hostname is the FQDN.

I believe DNS records mark one of the hostnames as canonical; I would
assume that NIS records have a similar ability.  For lines in
/etc/hosts, the first hostname on the line is considered canonical.

So, the upshot of all this:

* If you have a dynamic IP (ppp or dhcp), then you should have the
  following line in /etc/hosts:
    127.0.0.1   HOSTNAME.DOMAIN.TLD HOSTNAME localhost
  replacing HOSTNAME, DOMAIN, and TLD with the appropriate values.  This
  is what I do at work, since my IP is assigned via dhcp.

* If you have a static IP, like I do at home, then you'll want the
  following lines in /etc/hosts:
    127.0.0.1   localhost
    1.2.3.4     HOSTNAME.DOMAIN.TLD HOSTNAME
  where 1.2.3.4 is your IP, and HOSTNAME, DOMAIN, and TLD are as above.

In either situation, the order of the hostnames on the line *is*
signficant!

Richard



Reply to: