在本分类第15篇文章(地址:https://www.cnblogs.com/Nick-Hu/p/13276594.html)中说道使用vagrant成功创建了镜像实例,并成功访问了。但是由于默认配置下的vagrant实例网络访问是采用与宿主机端口映射的方式访问的。这会导致在镜像实例每安装一个软件都需要做一次端口配置,及其不方便。所以需要将vagrant的镜像实例的网络设置重新配置一下。
1.找到linux镜像实例的配置文件“Vagrantfile”将其打开,定位到如下配置项,并去除其“#”注释符(注意:一定要将去除注释符的配置与上面的注释符对齐,否则将重启失败):
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
2.打开cmd输入“ipconfig”指令,查看VirtualBox的虚拟网卡的网段信息,确定"private_network"中的ip的网段配置范围为:“192.168.56.X”
C:UsersNick>ipconfig
Windows IP 配置
以太网适配器 以太网:
连接特定的 DNS 后缀 . . . . . . . :
IPv6 地址 . . . . . . . . . . . . : 240e:fe:2c55:e900:500c:219:a19a:3366
临时 IPv6 地址. . . . . . . . . . : 240e:fe:2c55:e900:311d:65d7:c455:4334
本地链接 IPv6 地址. . . . . . . . : fe80::500c:219:a19a:3366%12
IPv4 地址 . . . . . . . . . . . . : 192.168.1.125
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : fe80::1%12
192.168.1.1
以太网适配器 VirtualBox Host-Only Network:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : fe80::3991:331f:dffd:dedb%7
IPv4 地址 . . . . . . . . . . . . : 192.168.56.1
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . :
3.将“Vagrantfile”文件中的“private_network”的ip配置设置为:192.168.56.124,使用"vagrant reload"指令重启linux的镜像实例
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.56.124"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
4.互相ping一下对方,测试网络是否通畅(linux实例ping宿主机)
vagrant@ubuntu-xenial:~$ ping 192.168.1.125 PING 192.168.1.125 (192.168.1.125) 56(84) bytes of data. 64 bytes from 192.168.1.125: icmp_seq=1 ttl=127 time=0.382 ms 64 bytes from 192.168.1.125: icmp_seq=2 ttl=127 time=0.639 ms 64 bytes from 192.168.1.125: icmp_seq=3 ttl=127 time=0.650 ms 64 bytes from 192.168.1.125: icmp_seq=4 ttl=127 time=0.664 ms 64 bytes from 192.168.1.125: icmp_seq=5 ttl=127 time=0.639 ms
4.1.宿主机ping向linux实例
C:UsersNick>ping 192.168.56.124 正在 Ping 192.168.56.124 具有 32 字节的数据: 来自 192.168.56.124 的回复: 字节=32 时间<1ms TTL=64 来自 192.168.56.124 的回复: 字节=32 时间<1ms TTL=64 来自 192.168.56.124 的回复: 字节=32 时间<1ms TTL=64 来自 192.168.56.124 的回复: 字节=32 时间<1ms TTL=64 192.168.56.124 的 Ping 统计信息: 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 0ms,最长 = 0ms,平均 = 0ms