diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bf5af7cc..349e351c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,8 @@ BUG FIXES: - commands/rsync-auto: Interrupt exits properly. [GH-3552] - commands/rsync-auto: Run properly on Windows. [GH-3547] - communicators/ssh: Detect if `config.ssh.shell` is invalid. [GH-3040] + - guests/debian: Can set hostname if hosts doesn't contain an entry + already for 127.0.1.1 [GH-3271] - guests/linux: For `read_ip_address` capability, set `LANG=en` so it works on international systems. [GH-3029] - providers/virtualbox: VirtalBox detection works properly again on diff --git a/plugins/guests/debian/cap/change_host_name.rb b/plugins/guests/debian/cap/change_host_name.rb index 53d0c38c4..8cb781b12 100644 --- a/plugins/guests/debian/cap/change_host_name.rb +++ b/plugins/guests/debian/cap/change_host_name.rb @@ -48,12 +48,18 @@ module VagrantPlugins # 127.0.0.1 localhost # 127.0.1.1 host.fqdn.com host.fqdn host def update_etc_hosts - ip_address = '([0-9]{1,3}\.){3}[0-9]{1,3}' - search = "^(#{ip_address})\\s+#{Regexp.escape(current_hostname)}(\\s.*)?$" - replace = "\\1 #{fqdn} #{short_hostname}" - expression = ['s', search, replace, 'g'].join('@') + if test("grep '#{current_hostname}' /etc/hosts") + # Current hostname entry is in /etc/hosts + ip_address = '([0-9]{1,3}\.){3}[0-9]{1,3}' + search = "^(#{ip_address})\\s+#{Regexp.escape(current_hostname)}(\\s.*)?$" + replace = "\\1 #{fqdn} #{short_hostname}" + expression = ['s', search, replace, 'g'].join('@') - sudo("sed -ri '#{expression}' /etc/hosts") + sudo("sed -ri '#{expression}' /etc/hosts") + else + # Current hostname entry isn't in /etc/hosts, just append it + sudo("echo '127.0.1.1 #{fqdn} #{short_hostname}' >>/etc/hosts") + end end def refresh_hostname_service @@ -79,6 +85,10 @@ module VagrantPlugins def sudo(cmd, &block) machine.communicate.sudo(cmd, &block) end + + def test(cmd) + machine.communicate.test(cmd) + end end end end