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)
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.
(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.
(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.
(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).
The workaround is this (untested): --edit '/etc/hosts: s/unassigned-hostname.unassigned-domain/newhostname.fqdn/'
This was later fixed as bug 1372269. *** This bug has been marked as a duplicate of bug 1372269 ***