From e799388e8598b740a5559c0c77850c70b049f241 Mon Sep 17 00:00:00 2001 From: dongmx Date: Thu, 3 Dec 2015 18:40:09 +0800 Subject: [PATCH] issue #2485, If a permanent IP address and hostname has alread in /etc/hosts Signed-off-by: dongmx --- libmachine/provision/generic.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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 {