Vagrant Network

1.forwarded_port

  1. First, you need to be explicit about every port you want to forward.
  2. Forwarded ports are also accessible from outside your own computer.
  3. And finally, with VirtualBox, Vagrant can’t forward to ports less than 1024 on the host system.
  • 宿主机host的端口转发到虚拟机VM的某一个端口上
  • 虚拟机可以访问外部网络

配置参数

  • guest:
  • guest_ip:
  • host: >1024 的端口
  • host_ip:
    • “127.0.0.1”: to forbid remote access to the forwarded port
  • protocol:
    • “tcp”
    • “udp”
  • auto_correct: true 防止端口占用(Autocorrecting Port Collisions)

V1

1
2
3
4
config.vm.forward_port 80, 8080 
#config.vm.forwarded_port 80, 8080, auto_correct: true
#config.vm.usable_port_range = (2200..2250)
#config.vm.forwarded_port 80, 8080, protocol: "udp"

V2

1
2
3
4
5
config.vm.network :forwarded_port, guest: 80, host: 8888
config.vm.network :forwarded_port, guest: 80, host: 8888, auto_correct: true
config.vm.network "forwarded_port", guest: 8001, host: 8001, host_ip: "127.0.0.1"

# protocol: "udp"

Pros

  • 容易实现外网访问虚拟机

Cons

  • 端口较多时比较麻烦,需手动配置
  • 不支持在宿主机器上使用小于1024的端口来转发。比如:不能使用SSL的443端口来进行https连接
  • It is a communication channel that originates on the host side; you cannot use it for NFS or X11 because port forwarding doesn’t allow the guest to communicate with services running on the host.
  • Port Collision(One port on your host can be assigned to only one service.)

2. Private network(Host-Only)

  • Vagrant supports host-only networks by specifying a static IP for the machine.
  • The machine can then be accessed directly using this IP.
  • Machines outside of the host, such as other machines on the local network, cannot access the assigned static IP.
  • The virtual machines can also communicate with the host itself. This can be useful for accessing services running on the host machine. Forwarded ports, on the other hand, can only be accessed from the host machine. The guest can’t talk to the host.(属主机host可以访问虚拟机)
  • Host-only networks that multiple virtual machines can communicate with each other by being a part of the same network. (同一网段的虚拟机可以相互访问)
  • 虚拟机可以访问外部网络

配置参数

  • 手动配置IP
    • config.vm.network "private_network", ip: "10.20.30.40"
  • VirtualBox built-in DHCP server
    • config.vm.network "private_network", type: "dhcp"

V1

1
config.vm.network "hostonly", "192.168.33.10"

V2

1
2
config.vm.network "private_network", ip: "10.11.100.200"
config.vm.network "private_network", ip: "192.168.33.10"

Pros

  • 安全, 只有自己能访问
  • Avoid typing the configuration of each forwarded port separately
  • Avoid using port numbers such as 8800 for well-known services
  • Use the same port numbers in different projects
  • Communicate between multiple guests
  • Use NFS for shared directories

Cons

  • Because it is isolated, coworkers and team members who may be working on the same project can’t look at your work.(私有网络,不能团队协作)
  • 宿主机上会自动创建虚拟网卡,destory虚拟机也不会自动删除
1
2
3
4
5
ifconfig 
VBoxManage hostonlyif remove vboxnet0
VBoxManage hostonlyif remove vboxnet1
VBoxManage hostonlyif remove vboxnet2
VBoxManage list hostonlyifs
  • dhcp
1
2
VBoxManage list dhcpservers
VBoxManage dhcpserver remove --netname HostInterfaceNetworking-vboxnet0

3. public network(Bridged Networking)

  • Bridged networking bridges the virtual machine onto a device on your physical machine, making the virtual machine look like another separate physical machine on the network.(虚拟机像实体机一样的网络配置, 也可以有静态IP)
  • It becomes available for remote access.

V1

1
config.vm.network "bridged"

V2

  • default
1
config.vm.network "public_network"
  • 静态IP
1
config.vm.network "public_network", ip: "192.168.1.120"
  • 公有网络中还可以设置桥接的网卡
1
config.vm.network "public_network", :bridge => 'en1: Wi-Fi (AirPort)'

Pros

  • 方便团队协作,别人可以访问你的虚拟机

Cons

  • 需要有网络,有路由器分配IP

4. Composing Networking Options

  • 同一VM网络可组合

V1

1
config.vm.forwarded_port 80, 8080
config.vm.network "hostonly", "192.168.33.10"
config.vm.network "bridged"

The static IP for private networks

  • 10.0.0.0-10.255.255.255 (10/8 prefix)
  • 172.16.0.0-172.31.255.255 (172.16/12 prefix)
  • 192.168.0.0-192.168.255.255 (192.168/16 prefix)

vagrant-hosts

1
$ vagrant plugin install vagrant-hosts
1
web.vm.provision :hosts

Comments