🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
使用场景: 当我们不想通过端口映射连接到虚拟机(或通过端口映射访问虚拟机里提供的web服务),此时我们就可以通过Public Networks网络模型,让虚拟机自动获取公司内部DHCP服务器分配的IP地址,(此时虚拟机获取的IP和公司内部的其他电脑上获取的IP就在同一网段咯,如果不是,路由也能通),因此在局域网任何一台电脑上,都可以ssh到虚拟机咯,或访问虚拟机上提供的服务咯 1)配置:通过DHCP获取 * 配置Vagrantfile 绑定接口Default Network Interface,通过bridge参数,配置如下 [root@vagrant ubuntu]# vim Vagrantfile config.vm.network "public_network",bridge: "ens33" config.vm.provision "shell",run: "always",inline: "route add default gw 10.2.11.1" ens33: 表示本地物理机能够连接互联网的接口(通过在物理机ifconfig查看) 10.2.11.1: 物理机的网关 * 启动虚拟机 [root@vagrant ubuntu]# vagrant ssh Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. Last login: Tue Jan 30 09:44:08 2018 from 10.0.2.2 vagrant@ubuntu-xenial:~$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.2.11.1 0.0.0.0 UG 0 0 0 enp0s8 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 enp0s3 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3 10.2.11.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s8 enp0s8 Link encap:Ethernet HWaddr 08:00:27:0c:12:63 inet addr:10.2.11.198 Bcast:10.2.11.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe0c:1263/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:775 errors:0 dropped:0 overruns:0 frame:0 TX packets:42 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:71598 (71.5 KB) TX bytes:4578 (4.5 KB) * 通过其他机器xshell登录虚拟机 ![](https://box.kancloud.cn/ce5554092bed8f8fa5975747ebfc08fe_581x502.png) ![](https://box.kancloud.cn/31ca35fe6ea625606a208a79c032f261_581x508.png) Connecting to 10.2.11.198:22... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. Last login: Tue Jan 30 09:59:52 2018 from 10.0.2.2 vagrant@ubuntu-xenial:~$ 2)当我们需要根据实际的工作环境,配置自己想要的static ip 配置Vagrantfile文件,添加如下 config.vm.network "public_network", auto_config: false ,bridge: "ens33" config.vm.provision "shell",run: "always",inline: "ifconfig eth1 10.2.11.196 netmask 255.255.0.0 up" config.vm.provision "shell",run: "alway",inline: "route add default gw 10.2.11.1" 说明: 1、auto_config:关闭自动配置 2、ifconfig enp0s8 10.2.11.196 netmask 255.255.255.0 up 配置静态ip(这里的ip不能和公司内部的地址冲突) 3、route add default gw 10.2.11.1 指定网关(添加默认路由) 4、bridge: 绑定接口(物理机哪个接口可以上网) 启动虚拟机 [root@vagrant ubuntu16.04_xiong]# vagrant ssh Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage Get cloud support with Ubuntu Advantage Cloud Guest: http://www.ubuntu.com/business/services/cloud 0 packages can be updated. 0 updates are security updates. 查看接口IP地址,发现这个地址就是我们刚刚指定的 vagrant@ubuntu-xenial:~$ ifconfig enp0s3 Link encap:Ethernet HWaddr 02:1f:2a:60:99:e3 inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0 inet6 addr: fe80::1f:2aff:fe60:99e3/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:858 errors:0 dropped:0 overruns:0 frame:0 TX packets:532 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:283815 (283.8 KB) TX bytes:70154 (70.1 KB) enp0s8 Link encap:Ethernet HWaddr 08:00:27:25:88:28 inet addr:10.2.11.196 Bcast:10.2.255.255 Mask:255.255.0.0 inet6 addr: fe80::a00:27ff:fe25:8828/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1 errors:0 dropped:0 overruns:0 frame:0 TX packets:8 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:60 (60.0 B) TX bytes:648 (648.0 B) 查看路由 vagrant@ubuntu-xenial:~$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.2.11.1 0.0.0.0 UG 0 0 0 enp0s8 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 enp0s3 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3 10.2.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s8