diff --git a/libmachine/provision/ubuntu.go b/libmachine/provision/ubuntu.go index a606fe84d3..f0f266867b 100644 --- a/libmachine/provision/ubuntu.go +++ b/libmachine/provision/ubuntu.go @@ -128,9 +128,17 @@ func (provisioner *UbuntuProvisioner) Hostname() (string, error) { func (provisioner *UbuntuProvisioner) SetHostname(hostname string) error { if _, err := provisioner.SSHCommand(fmt.Sprintf( - "sudo hostname %s && echo %q | sudo tee /etc/hostname && echo \"127.0.0.1 %s\" | sudo tee -a /etc/hosts", + "sudo hostname %s && echo %q | sudo tee /etc/hostname", hostname, hostname, + )); err != nil { + return err + } + + // ubuntu/debian use 127.0.1.1 for non "localhost" loopback hostnames: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution + if _, err := provisioner.SSHCommand(fmt.Sprintf( + "if grep -xq 127.0.1.1.* /etc/hosts; then sudo sed -i 's/^127.0.1.1.*/127.0.1.1 %s/g' /etc/hosts; else echo '127.0.1.1 %s' | sudo tee -a /etc/hosts; fi", + hostname, hostname, )); err != nil { return err diff --git a/test/integration/driver-digitalocean.bats b/test/integration/driver-digitalocean.bats index c363698d65..68b459ec3a 100644 --- a/test/integration/driver-digitalocean.bats +++ b/test/integration/driver-digitalocean.bats @@ -52,6 +52,12 @@ export MACHINE_STORAGE_PATH=/tmp/machine-bats-test-$DRIVER run machine ssh $NAME -- docker version } +# currently this only checks debian/ubuntu style using 127.0.0.1 +@test "$DRIVER: hostname should be set properly" { + run machine ssh $NAME -- "grep -Fxq '127.0.1.1 $NAME' /etc/hosts" + [ "$status" -eq 0 ] +} + @test "$DRIVER: stop" { run machine stop $NAME [ "$status" -eq 0 ]