Public Networks(公有网络)

使用场景:

当我们不想通过端口映射连接到虚拟机(或通过端口映射访问虚拟机里提供的web服务),此时我们就可以通过Public Networks网络模型,让虚拟机自动获取公司内部DHCP服务器分配的IP地址,(此时虚拟机获取的IP和公司内部的其他电脑上获取的IP就在同一网段咯,如果不是,路由也能通),因此在局域网任何一台电脑上,都可以ssh到虚拟机咯,或访问虚拟机上提供的服务咯

1)配置:通过DHCP获取

  • 配置Vagrantfile

    绑定接口Default Network Interface,通过bridge参数,配置如下

    1. [root@vagrant ubuntu]# vim Vagrantfile
    2. config.vm.network "public_network",bridge: "ens33"
    3. config.vm.provision "shell",run: "always",inline: "route add default gw 10.2.11.1"

ens33: 表示本地物理机能够连接互联网的接口(通过在物理机ifconfig查看) 10.2.11.1: 物理机的网关

  • 启动虚拟机
  1. [root@vagrant ubuntu]# vagrant ssh
  2. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64)
  3. Documentation: https://help.ubuntu.com
  4. Management: https://landscape.canonical.com
  5. Support: https://ubuntu.com/advantage
  6. Get cloud support with Ubuntu Advantage Cloud Guest:
  7. http://www.ubuntu.com/business/services/cloud
  8. 0 packages can be updated.
  9. 0 updates are security updates.
  10. Last login: Tue Jan 30 09:44:08 2018 from 10.0.2.2
  11. vagrant@ubuntu-xenial:~$ route -n
  12. Kernel IP routing table
  13. Destination Gateway Genmask Flags Metric Ref Use Iface
  14. 0.0.0.0 10.2.11.1 0.0.0.0 UG 0 0 0 enp0s8
  15. 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 enp0s3
  16. 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
  17. 10.2.11.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s8
  18. enp0s8 Link encap:Ethernet HWaddr 08:00:27:0c:12:63
  19. inet addr:10.2.11.198 Bcast:10.2.11.255 Mask:255.255.255.0
  20. inet6 addr: fe80::a00:27ff:fe0c:1263/64 Scope:Link
  21. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  22. RX packets:775 errors:0 dropped:0 overruns:0 frame:0
  23. TX packets:42 errors:0 dropped:0 overruns:0 carrier:0
  24. collisions:0 txqueuelen:1000
  25. RX bytes:71598 (71.5 KB) TX bytes:4578 (4.5 KB)
  • 通过其他机器xshell登录虚拟机

第三节:Public Networks(公有网络) - 图1 第三节:Public Networks(公有网络) - 图2

  1. Connecting to 10.2.11.198:22...
  2. Connection established.
  3. To escape to local shell, press 'Ctrl+Alt+]'.
  4. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64)
  5. Documentation: https://help.ubuntu.com
  6. Management: https://landscape.canonical.com
  7. Support: https://ubuntu.com/advantage
  8. Get cloud support with Ubuntu Advantage Cloud Guest:
  9. http://www.ubuntu.com/business/services/cloud
  10. 0 packages can be updated.
  11. 0 updates are security updates.
  12. Last login: Tue Jan 30 09:59:52 2018 from 10.0.2.2
  13. vagrant@ubuntu-xenial:~$

2)当我们需要根据实际的工作环境,配置自己想要的static ip

配置Vagrantfile文件,添加如下

  1. config.vm.network "public_network", auto_config: false ,bridge: "ens33"
  2. config.vm.provision "shell",run: "always",inline: "ifconfig eth1 10.2.11.196 netmask 255.255.0.0 up"
  3. 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: 绑定接口(物理机哪个接口可以上网)

启动虚拟机

  1. [root@vagrant ubuntu16.04_xiong]# vagrant ssh
  2. Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-112-generic x86_64)
  3. Documentation: https://help.ubuntu.com
  4. Management: https://landscape.canonical.com
  5. Support: https://ubuntu.com/advantage
  6. Get cloud support with Ubuntu Advantage Cloud Guest:
  7. http://www.ubuntu.com/business/services/cloud
  8. 0 packages can be updated.
  9. 0 updates are security updates.

查看接口IP地址,发现这个地址就是我们刚刚指定的

  1. vagrant@ubuntu-xenial:~$ ifconfig
  2. enp0s3 Link encap:Ethernet HWaddr 02:1f:2a:60:99:e3
  3. inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
  4. inet6 addr: fe80::1f:2aff:fe60:99e3/64 Scope:Link
  5. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  6. RX packets:858 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:532 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:1000
  9. RX bytes:283815 (283.8 KB) TX bytes:70154 (70.1 KB)
  10. enp0s8 Link encap:Ethernet HWaddr 08:00:27:25:88:28
  11. inet addr:10.2.11.196 Bcast:10.2.255.255 Mask:255.255.0.0
  12. inet6 addr: fe80::a00:27ff:fe25:8828/64 Scope:Link
  13. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  14. RX packets:1 errors:0 dropped:0 overruns:0 frame:0
  15. TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
  16. collisions:0 txqueuelen:1000
  17. RX bytes:60 (60.0 B) TX bytes:648 (648.0 B)

查看路由

  1. vagrant@ubuntu-xenial:~$ route -n
  2. Kernel IP routing table
  3. Destination Gateway Genmask Flags Metric Ref Use Iface
  4. 0.0.0.0 10.2.11.1 0.0.0.0 UG 0 0 0 enp0s8
  5. 0.0.0.0 10.0.2.2 0.0.0.0 UG 0 0 0 enp0s3
  6. 10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 enp0s3
  7. 10.2.0.0 0.0.0.0 255.255.0.0 U 0 0 0 enp0s8