Bug 1251499 - virt-builder --hostname should replace FQDN in /etc/hosts on Ubuntu
Summary: virt-builder --hostname should replace FQDN in /etc/hosts on Ubuntu
Keywords:
Status: CLOSED DUPLICATE of bug 1372269
Alias: None
Product: Virtualization Tools
Classification: Community
Component: libguestfs
Version: unspecified
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Pino Toscano
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2015-08-07 14:00 UTC by Dominic Cleal
Modified: 2016-09-22 16:00 UTC (History)
3 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-09-22 16:00:22 UTC
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Bugzilla 953907 0 unspecified CLOSED virt-sysprep does not correctly set the hostname on Debian/Ubuntu 2021-02-22 00:41:40 UTC

Internal Links: 953907

Description Dominic Cleal 2015-08-07 14:00:13 UTC
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 15:42:30 UTC
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 07:24:39 UTC
(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 09:37:40 UTC
(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 09:50:42 UTC
(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 10:58:12 UTC
The workaround is this (untested):

  --edit '/etc/hosts:
        s/unassigned-hostname.unassigned-domain/newhostname.fqdn/'

Comment 6 Pino Toscano 2016-09-22 16:00:22 UTC
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.