Vagrant网络设置
Vagrant 网络设置
基本用法
如何配置
Vagrant 的网络配置是通过在 Vagrantfile
中调用 config.vm.network
方法来实现的,例如:
# 2 是版本号的意思
Vagrant.configure("2") do |config|
# 配置网络端口转发,配置生效后,可从宿主机8080端口访问
config.vm.network "forwarded_port", guest: 80, host: 8080
end
上面可理解为:调用 config.vm 对象的 network 方法,方法的参数有三个:
- 字符串 "forwarded_port"
- 命名参数 guest,值为80
- 命名参数 host,值为8080
若干知识点
- 可以通过多次调用
config.vm.network
来声明多种网络定义 - 档调用命令
vagrant up
或vagrant reload
的时候网络会自动启用 - 可通过
config.vm.hostname
属性设置主机名
三种网络配置方式
端口转发 forwarded_port
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080
end
关于端口冲突的问题
如果出现端口冲突,可以过设置 auto_correct: true
来自动修正,修正的信息会打印在 vagrant up
或 vagrant reload
的输出中。
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
end
除此之外,还可设置一个可用的端口范围,让Vagrant在遇到端口冲突时从你指定的范围去应用一个端口:
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
config.vm.usable_port_range = 8000..8999
end
私人网络 private_network
私人网络就是NAT的方式,只有宿主机能通过虚拟机的ip访问到虚拟机,但是(除了宿主机之外的)其他主机是无法访问到虚拟机的。
下面是配置虚拟机网络连接方式为 private_network
,虚拟机通过DHCP方式自动获取ip,从vagrant up
的信息可以看出其实就是NAT
,hostonly
也可以理解到是只有宿主机能访问虚拟机:
# 查看 Vagrantfile 配置
$ cat Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu:18.04"
# :private_network 可以写成 "private_network"
config.vm.network :private_network, type: "dhcp"
end
# 启动虚拟机
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat(这里)
default: Adapter 2: hostonly(还有这里)
# ssh 连接到虚拟机
$ vagrant ssh
# 查看虚拟机 ip
$ ifconfig
# ......
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.28.128.3 netmask 255.255.255.0 broadcast 172.28.128.255
inet6 fe80::a00:27ff:fedc:2366 prefixlen 64 scopeid 0x20<link>
# ......
# 可以从宿主机上ping过去试试,能ping通表示配置没问题
也可以设置静态的ip:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu:18.04"
# type: "static" 可以不写
config.vm.network :private_network, type: "static", ip: "192.168.55.2"
end
但是必须注意:
设置静态ip时候注意不要设置成 .1
,如:192.168.1.1
、192.168.55.1
、192.168.77.1
,因为 .1
通常预留来做路由的,vagrant up
或 vagrant reload
时候也会有相应的提示信息:
$ vagrant reload
==> default: You assigned a static IP ending in ".1" to this machine.
==> default: This is very often used by the router and can cause the
==> default: network to not work properly. If the network doesn't work
==> default: properly, try changing this IP.
如何从虚拟机ping宿主机?
参考:Vagrant ping or curl from guest to host machine
-
如果虚拟机设置的是静态 ip,如
192.168.55.2
,那么可以通过预留的192.168.55.1
来 ping 通宿主机 -
可以在虚拟机中执行
ip route show
查看路由信息得知:vagrant@ubuntu-bionic:~$ ip route show default via 10.0.2.2 dev enp0s3 proto dhcp src 10.0.2.15 metric 100 10.0.2.0/24 dev enp0s3 proto kernel scope link src 10.0.2.15 10.0.2.2 dev enp0s3 proto dhcp scope link src 10.0.2.15 metric 100 192.168.55.0/24 dev enp0s8 proto kernel scope link src 192.168.55.2 vagrant@ubuntu-bionic:~$ ping 10.0.2.2 PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data. 64 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=0.176 ms 64 bytes from 10.0.2.2: icmp_seq=2 ttl=64 time=0.274 ms
-
可以直接 ping 宿主机的 ip(我自己机器能行,但其它机器上没测试过)
公共网络 public_network
# 配置公共网络
$ cat Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu:18.04"
config.vm.network "public_network", ip: "192.168.55.2"
end
# 启动虚拟机
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu:18.04'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: network_default_1614743427484_66744
==> default: Clearing any previously set network interfaces...
==> default: Available bridged network interfaces:
1) eno1
2) wlp4s0
3) docker0
4) br-ed585c0c0346
5) vethbf396bd
6) veth7ce7b74
7) vpn_a
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
==> default:
default: Which interface should the network bridge to? 1
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
# ssh 进入虚拟机
$ vagrant ssh
# 在虚拟机中查ip
vagrant@ubuntu-bionic:~$ ifconfig
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.55.2 netmask 255.255.255.0 broadcast 192.168.55.255
inet6 fe80::a00:27ff:fe6e:193b prefixlen 64 scopeid 0x20<link>
ether 08:00:27:6e:19:3b txqueuelen 1000 (Ethernet)
RX packets 38686 bytes 2384911 (2.3 MB)
RX errors 0 dropped 35952 overruns 0 frame 0
TX packets 15 bytes 1146 (1.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0