virtualbox中linux设置NAT和Host-Only上网(实现双机互通同时可上外网)
关于虚拟机中几种网络连接方式请参考其他教程。
连接方式:NAT网络
网卡2选择:
连接方式:仅主机(Host-Only)网络
[root@rhel-server network-scripts]# vim ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
NETMASK=255.255.255.0
IPADDR=192.168.56.101
执行ifconfig -a,会出现2块网卡,eth0和eth1:
eth0 Link encap:Ethernet HWaddr 08:00:27:59:84:A1
inet addr: 10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe59:84a1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3762 (3.6 KiB) TX bytes:5272 (5.1 KiB)
eth1 Link encap:Ethernet HWaddr 08:00:27:7B:A4:51
inet addr: 192.168.56.101 Bcast:192.168.56.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe7b:a451/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:481 errors:0 dropped:0 overruns:0 frame:0
TX packets:481 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:46070 (44.9 KiB) TX bytes:53168 (51.9 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
64 bytes from 119.75.213.61: icmp_seq=1 ttl=50 time=36.0 ms
64 bytes from 119.75.213.61: icmp_seq=2 ttl=50 time=41.8 ms
64 bytes from 119.75.213.61: icmp_seq=3 ttl=50 time=39.4 ms
64 bytes from 172.30.100.73: icmp_seq=1 ttl=63 time=1.03 ms
64 bytes from 172.30.100.73: icmp_seq=2 ttl=63 time=1.12 ms
64 bytes from 172.30.100.73: icmp_seq=3 ttl=63 time=1.12 ms
正在 Ping 192.168.56.101 具有 32 字节的数据:
来自 192.168.56.101 的回复: 字节=32 时间<1ms TTL=64
平常,我们安装好虚机,用桥接方式也就够了。毕竟它能上内网和外网。
但是有个问题,如果你的网络环境发生变化,虚机的Ip也会随之改变(桥接的Ip和主机ip必须是同一网段)。
为了解决此问题,喜欢折腾的我选择了NAT网络+Host-Only的组合。NAT网络,负责上外网通信;
Host-Only网络,负责主机和虚机通信。1.关闭虚拟机系统
2.管理-全局设定,添加NAT网络
3.打开虚拟机设置-网络
网卡1选择:连接方式:NAT网络
界面名称:选择2中设定的NAT网络,我这里是默认的:NatNetwork
网卡2选择:
连接方式:仅主机(Host-Only)网络
界面名称:VirtualBox Host-Only Network, 这地方对应的就是网络连接中的网络。一定要启用它否则选不到的。
关于此网络的详细信息,请看全局工具:
不要启用,我们后面会再linux里配置静态IP。
4.启动虚拟机系统,创建ifcfg-eth1
[root@rhel-server ~]# cd /etc/sysconfig/network-scripts/
[root@rhel-server ~]# touch ifcfg-eth1[root@rhel-server network-scripts]# vim ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
NETMASK=255.255.255.0
IPADDR=192.168.56.101
执行ifconfig -a,会出现2块网卡,eth0和eth1:
eth0 Link encap:Ethernet HWaddr 08:00:27:59:84:A1
inet addr: 10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe59:84a1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3762 (3.6 KiB) TX bytes:5272 (5.1 KiB)
eth1 Link encap:Ethernet HWaddr 08:00:27:7B:A4:51
inet addr: 192.168.56.101 Bcast:192.168.56.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe7b:a451/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:481 errors:0 dropped:0 overruns:0 frame:0
TX packets:481 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:46070 (44.9 KiB) TX bytes:53168 (51.9 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
eth0对应的NAT网络;
eth1对应Host-Only网络。5.验证网络联通性:
5.1 虚机访问百度:
[root@rhel-server network-scripts]# ping www.baidu.com
PING www.a.shifen.com (119.75.213.61) 56(84) bytes of data.64 bytes from 119.75.213.61: icmp_seq=1 ttl=50 time=36.0 ms
64 bytes from 119.75.213.61: icmp_seq=2 ttl=50 time=41.8 ms
64 bytes from 119.75.213.61: icmp_seq=3 ttl=50 time=39.4 ms
5.2 虚机访问主机:
[root@rhel-server network-scripts]# ping 172.30.100.73
PING 172.30.100.73 (172.30.100.73) 56(84) bytes of data.64 bytes from 172.30.100.73: icmp_seq=1 ttl=63 time=1.03 ms
64 bytes from 172.30.100.73: icmp_seq=2 ttl=63 time=1.12 ms
64 bytes from 172.30.100.73: icmp_seq=3 ttl=63 time=1.12 ms
5.3 主机访问虚机:
正在 Ping 192.168.56.101 具有 32 字节的数据:
来自 192.168.56.101 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.101 的回复: 字节=32 时间<1ms TTL=64