Vagrant:不将主机名映射到/etc/hosts中的环回地址

Vagrant:不将主机名映射到/etc/hosts中的环回地址,vagrant,fedora,hosts,vagrantfile,resolve,Vagrant,Fedora,Hosts,Vagrantfile,Resolve,我正在使用Vagrant(v1.7.2)来配置Linux(Fedora22)主机和插件(v1.6.1) 写入/etc/hosts,以便主机可以相互访问 我的流浪汉档案: Vagrant.configure(2) do |config| config.vm.box = "workshop" config.hostmanager.enabled = true config.hostmanager.include_offline = true config.vm.define "

我正在使用Vagrant(v1.7.2)来配置Linux(Fedora22)主机和插件(v1.6.1) 写入
/etc/hosts
,以便主机可以相互访问

我的
流浪汉档案

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
当I
vagrant up
时,
服务器
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插件,所以我还尝试在设置期间运行hostmanager,方法是将
Vagrant文件
更改为:

  config.hostmanager.enabled = false
  config.hostmanager.include_offline = true
  config.vm.provision :hostmanager
结果是一样的

问题描述
127.0.0.1
行与vagrant hostmanager添加的信息冲突。我需要抑制主机名与环回地址的关联,以便在每个VM上主机名解析为专用网络地址,正如vagrant hostmanager所添加的那样


我如何才能做到这一点?

问题的原因是Vagrant为Fedora来宾更改主机名的功能。特别是在plugins/guests/fedora/cap/change\u host\u 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\u etc\u hosts
方法将原始主机名(对于Fedora,这是
localhost.localdomain
,绑定到环回地址
127.0.0.1
)替换为新主机名。然后它用短主机名更新
/etc/hostname
,尽管系统调用仍然返回完整主机名,因为它出现在
/etc/hosts

解决方案 我提供了额外的供应器(在上述黑客攻击发生后运行),以:

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

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

  • 秩序至关重要。这是Vagrant的
    文件
    代码:

    # 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"
    

    当主机名通过vagrant设置时,大多数linux发行版都具有更改主机名vagrant功能(、、等)所导致的错误

    设置主机名时,
    change\u host\u name
    中的以下行将vm主机名和vm名称作为别名前置到
    127.0.0.1
    ,这与vagrant hostmanager的别名冲突

    sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
    
    他们不打算修复它,因为只要不修改/etc/hosts(流浪的hostmanager就是这么做的),它就可以正常工作

    有趣的是,许多工具(dig、host、nslookup)使用最后一个指定的别名,它可以很好地处理defualt Vagrant行为。不幸的是,其他工具,即使用or的工具,具有不同的行为

    例如,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
    

    我想,即使没有流浪汉hostmanager插件,你也会有同样的体验。一旦您为您的VM/etc/hosts设置了主机名,它就会在127.0.0.1环回中添加主机名,因此我假设这部分不是由插件管理的,而是保留的untouched@Fr是的,里森里。问题是Vagrant对
    /etc/hosts
    所做的更改妨碍了Vagrant hostmanager的成功使用。我用一些准备脚本(见答案)解决了这个问题。很好,所以脚本编写是可能的——你有没有试着挂接插件?这可能是一个改进,使它对其他人如此有用。@FrédéricHenri我们在插件中没有条件,但我同意它可能是有用的;我先申请一张票。