Bug 1251499 - virt-builder --hostname should replace FQDN in /etc/hosts on Ubuntu
virt-builder --hostname should replace FQDN in /etc/hosts on Ubuntu
Status: CLOSED DUPLICATE of bug 1372269
Product: Virtualization Tools
Classification: Community
Component: libguestfs (Show other bugs)
unspecified
Unspecified Unspecified
unspecified Severity unspecified
: ---
: ---
Assigned To: Pino Toscano
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2015-08-07 10:00 EDT by Dominic Cleal
Modified: 2016-09-22 12:00 EDT (History)
3 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2016-09-22 12:00:22 EDT
Type: Bug
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Dominic Cleal 2015-08-07 10:00:13 EDT
Description of problem:
virt-builder --hostname does not replace the default FQDN that's in /etc/hosts:

# cat /etc/hosts
127.0.0.1	localhost
127.0.1.1	unassigned-hostname.unassigned-domain	test.example.com

This causes the FQDN to resolve incorrectly when running `hostname -f`.


Version-Release number of selected component (if applicable):
libguestfs-tools-1.28.5-1.fc21.noarch

How reproducible:
Always

Steps to Reproduce:
1. virt-builder --hostname test.example.com ubuntu-14.04
2. start up the image and log in
3. hostname -f
4. hostname

Actual results:
unassigned-hostname.unassigned-domain
test.example.com

Expected results:
test.example.com
test.example.com

(or arguably, test.example.com and test)
Comment 1 Pino Toscano 2015-08-07 11:42:30 EDT
We already replace the hostname in /etc/hosts; without that, the hosts line would be:

127.0.1.1	unassigned-hostname.unassigned-domain	unassigned-hostname

but /etc/hostname contains only "unassigned-hostname", so we replace that only.
I guess /etc/hostname should contain only the hostname and not FQDN, which would rather go in /etc/hosts. But then, we should somehow assume that "unassigned-hostname.unassigned-domain" is a template, which is something I wanted to avoid if possible... but then, there seems to be no other place where FQDN is set.
Comment 2 Dominic Cleal 2015-08-10 03:24:39 EDT
(In reply to Pino Toscano from comment #1)
> We already replace the hostname in /etc/hosts; without that, the hosts line
> would be:
> 
> 127.0.1.1	unassigned-hostname.unassigned-domain	unassigned-hostname
> 
> but /etc/hostname contains only "unassigned-hostname", so we replace that
> only.
> I guess /etc/hostname should contain only the hostname and not FQDN, which
> would rather go in /etc/hosts.

Indeed, it's more typical under Debian/Ubuntu to take the hostname portion only and put it in /etc/hostname (so `hostname` returns "test", while on a RH OS it would typically be the FQDN).

> But then, we should somehow assume that
> "unassigned-hostname.unassigned-domain" is a template, which is something I
> wanted to avoid if possible... but then, there seems to be no other place
> where FQDN is set.

That's right, it would be determined from a name lookup (DNS or /etc/hosts by default).

The ideal behaviour would be to split the --hostname argument into the short name and FQDN, put the short name into /etc/hostname, perform the existing /etc/hosts replacement with the short name and then replace any "oldhostname.*" nodes in /etc/hosts with the FQDN.
Comment 3 Pino Toscano 2015-08-10 05:37:40 EDT
(In reply to Dominic Cleal from comment #2)
> (In reply to Pino Toscano from comment #1)
> > We already replace the hostname in /etc/hosts; without that, the hosts line
> > would be:
> > 
> > 127.0.1.1	unassigned-hostname.unassigned-domain	unassigned-hostname
> > 
> > but /etc/hostname contains only "unassigned-hostname", so we replace that
> > only.
> > I guess /etc/hostname should contain only the hostname and not FQDN, which
> > would rather go in /etc/hosts.
> 
> Indeed, it's more typical under Debian/Ubuntu to take the hostname portion
> only and put it in /etc/hostname (so `hostname` returns "test", while on a
> RH OS it would typically be the FQDN).

Right.
This also reminds me we are not changing the hostname at all in /etc/hosts on Fedora/RHEL systems.

> > But then, we should somehow assume that
> > "unassigned-hostname.unassigned-domain" is a template, which is something I
> > wanted to avoid if possible... but then, there seems to be no other place
> > where FQDN is set.
> 
> That's right, it would be determined from a name lookup (DNS or /etc/hosts
> by default).
> 
> The ideal behaviour would be to split the --hostname argument into the short
> name and FQDN, put the short name into /etc/hostname, perform the existing
> /etc/hosts replacement with the short name and then replace any
> "oldhostname.*" nodes in /etc/hosts with the FQDN.

The problem I see is that "oldhostname.*" may match also other nodes (not common case, but still potentially there): on Fedora/RHEL systems it is not an issue, since knowing the FQDN allows us to replace it fully, but on Debian/Ubuntu systems this is problematic.
Comment 4 Dominic Cleal 2015-08-10 05:50:42 EDT
(In reply to Pino Toscano from comment #3)
> (In reply to Dominic Cleal from comment #2)
> > (In reply to Pino Toscano from comment #1)
> > > We already replace the hostname in /etc/hosts; without that, the hosts line
> > > would be:
> > > 
> > > 127.0.1.1	unassigned-hostname.unassigned-domain	unassigned-hostname
> > > 
> > > but /etc/hostname contains only "unassigned-hostname", so we replace that
> > > only.
> > > I guess /etc/hostname should contain only the hostname and not FQDN, which
> > > would rather go in /etc/hosts.
> > 
> > Indeed, it's more typical under Debian/Ubuntu to take the hostname portion
> > only and put it in /etc/hostname (so `hostname` returns "test", while on a
> > RH OS it would typically be the FQDN).
> 
> Right.
> This also reminds me we are not changing the hostname at all in /etc/hosts
> on Fedora/RHEL systems.

Indeed, it's just more common with Debian/Ubuntu due to the FQDN being set up as a localhost alias by the installer.

> > > But then, we should somehow assume that
> > > "unassigned-hostname.unassigned-domain" is a template, which is something I
> > > wanted to avoid if possible... but then, there seems to be no other place
> > > where FQDN is set.
> > 
> > That's right, it would be determined from a name lookup (DNS or /etc/hosts
> > by default).
> > 
> > The ideal behaviour would be to split the --hostname argument into the short
> > name and FQDN, put the short name into /etc/hostname, perform the existing
> > /etc/hosts replacement with the short name and then replace any
> > "oldhostname.*" nodes in /etc/hosts with the FQDN.
> 
> The problem I see is that "oldhostname.*" may match also other nodes (not
> common case, but still potentially there): on Fedora/RHEL systems it is not
> an issue, since knowing the FQDN allows us to replace it fully, but on
> Debian/Ubuntu systems this is problematic.

True, and I suppose I was skipping over this concern as I was thinking only of virt-builder where the inputs are more constrained.  With virt-customize etc it'd be a risk.

You could be more specific in only changing an /etc/hosts entry if you've already replaced the short hostname on the same line (given that you're dealing with a canonical name and an alias, rather than two separate addresses).
Comment 5 Richard W.M. Jones 2015-08-10 06:58:12 EDT
The workaround is this (untested):

  --edit '/etc/hosts:
        s/unassigned-hostname.unassigned-domain/newhostname.fqdn/'
Comment 6 Pino Toscano 2016-09-22 12:00:22 EDT
This was later fixed as bug 1372269.

*** This bug has been marked as a duplicate of bug 1372269 ***

Note You need to log in before you can comment on or make changes to this bug.