diff --git a/libmachine/provision/generic.go b/libmachine/provision/generic.go index 97d4affac2..72f8bfc000 100644 --- a/libmachine/provision/generic.go +++ b/libmachine/provision/generic.go @@ -46,8 +46,15 @@ func (provisioner *GenericProvisioner) SetHostname(hostname string) error { } // 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", + if _, err := provisioner.SSHCommand(fmt.Sprintf(` + if ! grep -xq .*%s /etc/hosts; then + 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 + fi`, + hostname, hostname, hostname, )); err != nil {