我有一个已经预配置的 RHEL 镜像,我不知道它最初是如何设置的。 默认情况下,它在 ip 192.168.50.50 上配置了一个本地网络接口(interface)。我想做的是从 Vagrant 脚本配置它的 ip。
这似乎没有做任何事情:
config.vm.network "private_network", ip: "192.168.50.10"
这确实改变了 ip:
sudo nmcli con mod bond0 ipv4.addresses 192.168.50.10/24
service network restart
但在那之后显然 Vagrant 不会自动检测要连接的 ip,所以我需要添加:
config.ssh.host = LOCAL_IP
但是问题来了:第一次,ip 是默认的 (.50.50)。所以我不能已经将 config.ssh.host
设置为我想要的 ip。如果我省略 config.ssh.host
行,它将第一次运行但之后不会运行,并且 vagrant ssh
也会失败。
有没有一种方法可以在不编辑第一个和第二个 vagrant up
之间的 Vagrant 脚本的情况下设置盒子 ip?
编辑:vagrant up --debug
命令的结果:http://pastebin.com/BTccc4NT
编辑:问题是来自默认框的 Vagrant 文件(在 Windows 上,它位于 C:\Users\user\.vagrant.d\boxes\nameofbox\virtualbox\Vagrantfile
) 本身有这一行:
config.vm.network "private_network", ip: "192.168.50.50", auto_config: false
最佳答案
嗯,很奇怪,它创建了 2 个接口(interface)
DEBUG network: Normalized configuration: {:adapter_ip=>"192.168.50.1", :auto_config=>false, :ip=>"192.168.50.50", :mac=>nil, :name=>nil, :netmask=>"255.255.255.0", :nic_type=>nil, :type=>:static, :adapter=>2}
INFO network: Searching for matching hostonly network: 192.168.50.50
INFO subprocess: Starting process: ["C:/Program Files/Oracle/VirtualBox/VBoxManage.exe", "list", "hostonlyifs"]
......
DEBUG network: Normalized configuration: {:adapter_ip=>"192.168.50.1", :auto_config=>true, :ip=>"192.168.50.10", :mac=>nil, :name=>nil, :netmask=>"255.255.255.0", :nic_type=>nil, :type=>:static, :adapter=>3}
INFO network: Searching for matching hostonly network: 192.168.50.10
INFO subprocess: Starting process: ["C:/Program Files/Oracle/VirtualBox/VBoxManage.exe", "list", "hostonlyifs"]
以此类推,在适配器 2 上你有 192.168.50.50,在适配器 3 上你有 192.168.50.10
这可能的原因是您使用的盒子有一个特定的 Vagrantfile
它已经在静态地址上定义了一个网络。
我对 windows 不是很熟悉,但是在 mac 上,框定义在 ~/.vagrant/boxes/<yourbox>/<theprovider>/Vagrantfile
下(请注意,这不是您项目中的 Vagrantfile,这实际上是一个 Vagrantfile,它将应用于从这个盒子构建的任何 VM);检查文件并删除网络配置(如果看到)
作为documented Vagrantfile 从不同的位置合并
At each level, settings set will be merged with previous values. What this exactly means depends on the setting. For most settings, this means that the newer setting overrides the older one. However, for things such as defining networks, the networks are actually appended to each other. By default, you should assume that settings will override each other. If the behavior is different, it will be noted in the relevant documentation section.
因此默认情况下 Vagrant 将创建额外的网络接口(interface),并且不会替换来自盒子 Vagrantfile 的网络接口(interface)。
关于linux - 从 Vagrant 脚本设置 RHEL box 的 ip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39568446/