使用場景:
當我們不想通過端口映射連接到虛擬機(或通過端口映射訪問虛擬機里提供的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登錄虛擬機


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
- 第一章:Vagrant基礎介紹
- 第二章:Vagrant的安裝部署
- 第一節:在centos7.3的系統上安裝部署vagrant
- 第二節:在windows server 2008 R2的系統上安裝部署vagrant
- 第三章:vagrant常見命令
- 第一節:box
- 第二節:虛擬機相關的命令
- 第四章:vagrant管理虛擬機
- 第一節:初始化centos6的系統
- 第二節:初始化ubuntu16.04的系統
- 第三節:打包自己的box
- 第五章:Vagrant之網絡配置
- 第一節:通過Forwarded Ports(轉發端口)
- 第二節:Private Networks(私有網絡)
- 第三節:Public Networks(公有網絡)
- 第六章:常見錯誤
- 第七章:Vagrant之文件同步方式
- 第一節:synced_folder(把物理機的目錄同步到虛擬機里某個目錄)
- 第二節:通過NFS共享
- 第三節:通過rsync共享
- 第八章:Vagrantfile配置文件