【问题标题】:Vagrant: do not map hostname to loopback address in /etc/hostsVagrant:不要将主机名映射到 /etc/hosts 中的环回地址
【发布时间】:2015-10-14 06:17:44
【问题描述】:

我正在使用 Vagrant (v1.7.2) 来配置 Linux (Fedora 22) 主机和 vagrant-hostmanager 插件 (v1.6.1) 写/etc/hosts以便主机可以互相访问。

我的Vagrantfile

Vagrant.configure(2) do |config|

  config.vm.box = "workshop"

  config.hostmanager.enabled = true
  config.hostmanager.include_offline = true

  config.vm.define "server" do |server|
    server.vm.network "private_network", ip: "192.168.33.10"
    server.vm.hostname = "server.local"
  end

  config.vm.define "client" do |client|
    client.vm.network "private_network", ip: "192.168.33.20"
    client.vm.hostname = "client.local"
  end

end

当我vagrant up时,server VM 有以下/etc/hosts

127.0.0.1 server.ipademo.local server
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

## vagrant-hostmanager-start
192.168.33.10   server.ipademo.local

192.168.33.20   client.ipademo.local

## vagrant-hostmanager-end

(对于 VM client仅在第一行替换 s/server/client/。)

因为 Vagrant >= 1.5.0 在配置之前运行 vagrant-hostmanager 插件,我还尝试通过将Vagrantfile 更改为:

  config.hostmanager.enabled = false
  config.hostmanager.include_offline = true
  config.vm.provision :hostmanager

这有同样的结果。

问题描述

127.0.0.1 <fqdn> <shortname> 行与 vagrant-hostmanager 添加的信息冲突。我需要抑制主机名与环回地址的关联,以便在每个 VM 上,主机名解析为由 vagrant-hostmanager 添加的专用网络地址。

我怎样才能做到这一点?

【问题讨论】:

  • 我认为即使没有 vagrant-hostmanager 插件,您也会拥有相同的功能。一旦你为你的虚拟机设置了主机名,/etc/hosts 就会在 127.0.0.1 环回中添加主机名,所以我认为这部分不是由插件管理的,并且保持不变
  • @FrédéricHenri 这是正确的。问题是 Vagrant 对/etc/hosts 所做的更改妨碍了 vagrant-hostmanager 的成功使用。我用一些配置脚本解决了这个问题(见答案)。
  • 很好,因此可以编写脚本-您是否尝试挂接插件?潜在地它可能是一种改进,在那里对其他人如此有用..
  • @FrédéricHenri 我们在插件中没有条件,但我同意它可能有用;我将提交一张票作为开始。

标签: vagrant fedora hosts vagrantfile resolve


【解决方案1】:

问题的原因是 Vagrant 为 Fedora 来宾提供的更改主机名功能。特别是在plugins/guests/fedora/cap/change_host_name.rb

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('@')

  sudo("sed -ri '#{expression}' /etc/hosts")
end

update_etc_hosts 方法用新的主机名替换原来的主机名(对于 Fedora,这是localhost.localdomain,绑定到环回地址127.0.0.1)。然后它用短主机名更新/etc/hostname,尽管系统调用仍然返回完整的主机名,因为它出现在/etc/hosts中。

解决办法

我提供了额外的供应商(在上述黑客事件发生之后运行):

  1. 查询长主机名 (FQDN) 并将其写回/etc/hostname。这是必需的,所以hostname --fqdn 在我们在下一步修复/etc/hosts 后实际上返回完整的主机名。

  2. 恢复/etc/hosts中的环回行,以便机器的主机名解析为vagrant-hostmanager设置的专用网络地址。

顺序很关键。这是Vagrantfile 代码:

# Vagrant's "change host name" sets the short host name.
# Before we undo the /etc/hosts silliness (see below) let's
# reset /etc/hostname to the *full* host name
#
config.vm.provision "shell",
  inline: "hostname --fqdn > /etc/hostname && hostname -F /etc/hostname"

# Vagrant's "change host name" capability for Fedora
# maps hostname to loopback, conflicting with hostmanager.
# We must repair /etc/hosts
#
config.vm.provision "shell",
  inline: "sed -ri 's/127\.0\.0\.1\s.*/127.0.0.1 localhost localhost.localdomain/' /etc/hosts"

【讨论】:

    【解决方案2】:

    当通过 Vagrant 设置主机名时,大多数 Linux 发行版都有由 change_host_name vagrant 功能(redhatdebianarch 等)引起的same issue

    change_host_name 中的以下行在设置主机名时将 vm 主机名和 vm 名称作为别名添加到 127.0.0.1,这与 vagrant-hostmanager 的别名冲突。

    sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
    

    Vagrant authors have said that this is intentional 并且他们不打算修复它,因为它可以按原样运行,只要您不修改 /etc/hosts(vagrant-hostmanager 会这样做)。

    有趣的是,许多工具(dig、host、nslookup)使用 LAST 指定的别名,这与默认的 Vagrant 行为配合得很好。不幸的是,其他工具,即使用 gethostbyname/etc/nsswitch.conf 的工具,具有不同的行为。

    例如,Ping 似乎使用 /etc/hosts 中的第一个别名,而 Curl(编译为使用 NSS 时)使用多个别名作为自上而下的后备。

    因此,通常不鼓励别名匹配多个 IP。

    以下Vagrantfile 解决方法应该足够了:

    config.hostmanager.enabled = true
    
    name = "name"
    hostname = "hostname"
    
    config.vm.define name do |machine|
      machine.vm.hostname = hostname
      machine.vm.provision :shell, inline: "sed -i'' '/^127.0.0.1\\t#{hostname}\\t#{name}$/d' /etc/hosts"
      ...
    end
    

    【讨论】:

      猜你喜欢
      • 2014-10-06
      • 1970-01-01
      • 2016-07-08
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      • 2015-05-10
      相关资源
      最近更新 更多