Vagrant无法将静态IP分配给框

时间:2016-03-24 22:09:29

标签: vagrant virtual-machine virtualbox static-ip-address virtual-ip-address

我的Vagrant文​​件中有以下内容。

  config.vm.network "private_network", ip: "192.168.33.12"

当我“流浪”时,流浪者正在分配127.0.0.1。我不确定是什么原因引起的。以下是完整的跟踪。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/vm2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

4 个答案:

答案 0 :(得分:4)

你在这里看到的

  

默认值:SSH地址:127.0.0.1:2200

不是您的虚拟机IP地址 - 这仅适用于使用可用端口(在您的情况下为2200)将ssh插入到框中。 vagrant在您的VM上配置eth0接口以与主机通信

登录虚拟机并运行ifconfig,您将看到10.0.2.15上的eth0和192.168.33.12上的eth1(至少)有2个接口可用

答案 1 :(得分:0)

作为执行此操作的简便方法,您可以从主机运行oneliner以查看来宾上分配的IP:

$ vagrant ssh -c "ifconfig | grep 'inet addr'"

这将为您提供如下输出:

      inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
      inet addr:192.168.200.200  Bcast:192.168.200.255  Mask:255.255.255.0
      inet addr:127.0.0.1  Mask:255.0.0.0

答案 2 :(得分:0)

以下是我在设置 Ubuntu 机器的静态 ip 时选择的步骤:

ls /etc/netplan/ //go to this directory to find the config file

注意:YAML 文件对缩进非常严格,例如

  dhcp4:true //will give wrong indentation error
  dhcp4: true //right way to give

现在编辑配置文件:-

 sudo nano /etc/netplan/01-netcfg.yaml 

更改默认设置:-

network:
  version: 2
  renderer: networkd
  eth0:
    dhcp4: true
    dhcp6: false
    optional: true
    nameservers:
    addresses: [8.8.8.8,8.8.4.4]

network:
version: 2
renderer: networkd
ethernets:
   eth0:
   dhcp4: no
   addresses: [172.28.32.11/16] //here choose your desired IP
   gateway4: 172.28.32.1     //provide the gateway address,
   nameservers:
   addresses: [8.8.8.8,8.8.4.4] 

现在做:

sudo netplan apply // to apply the network settings

然后您可以检查在 vagrant 停止/启动之间的任何过程中 ip 地址是否保持不变

答案 3 :(得分:0)

这是我为 vagrant 的虚拟盒装机所做的:

将此添加到流浪文件:-

<块引用>

config.vm.network "private_network", ip: "您选择的任何 IP 地址"

然后进行 Vagrant Reload,你应该会很好。

如果 VM 在重试连接或 ssh 时卡住 Vagrant Up 命令.. 这样做:-

从cmder控制台运行以下命令:-

<块引用>

vagrant 插件安装 vagrant-vbguest

在此之后,通过将以下内容添加到 vagrant 文件来启用 Vagrant GUI:-

config.vm.provider :virtualbox do |vb| 
  vb.gui = true
end 

现在开始流浪

vagrant gui 窗口将打开并保持按“S”跳过并进入登录屏幕

使用用户名/密码登录,默认为 root/vagrant

并运行以下命令:-

sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions

现在从 vagrant gui 窗口 shell 中出来 删除之前添加的用于启用 vagrant gui 的行

从cmder控制台再次“vagrant up”

完成!