virtualbox下centos实现主宿互访

1.网络连接方式

  • NAT
  • 桥接
  • Host-Only

 

NAT: 网络地址转换,virtualbox默认采用这种连接方式,特点:

1.虚拟机配置稍作修改就能连上外网

2.虚拟机可以ping通主机,主机不能ping通虚拟机

 

 桥接:虚拟机就像一台真实的机器,有独立ip,特点:

1.静态ip

2.主机和虚拟机能互相ping通

3.网卡只能选无线或者有线

 

Host-Only:

虚拟机在主机上建立一个网卡,虚拟机通过这个网卡和主机进行相互通信

 

2.网卡配置

 我们使用命令查看当前网卡状态:

[pigfly@localhost src]$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:ff:1a:18 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 76520sec preferred_lft 76520sec
    inet6 fe80::11d1:6afa:b77a:8bdd/64 scope link
       valid_lft forever preferred_lft forever

  

可以看到有两个网卡,enp0s3就是虚拟机默认采用NAT连接的网卡,编辑网卡文件随系统自动启动此网卡:

[pigfly@localhost src]$ vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=02e3fd29-ae05-4131-9f9d-7ed71057531a
DEVICE=enp0s3
ONBOOT=yes
[pigfly@localhost ~]$ service network restart
[pigfly@localhost ~]$ ping 192.168.0.104 #虚拟机ping主机,通 C:\Users\think>ping 10.0.2.15 #主机ping虚拟机,不通 [pigfly@localhost ~]$ ping baidu.com #ping外网,通

 如果我们想要虚拟机和主机能互相ping通,就需要再加一个桥接的网卡了,打开虚拟机设置->网络,连接方式选择主机的无线网卡:

启动虚拟机,查看网卡状态:

[root@localhost network-scripts]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:ff:1a:18 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
valid_lft 85958sec preferred_lft 85958sec
inet6 fe80::11d1:6afa:b77a:8bdd/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:71:49:ad brd ff:ff:ff:ff:ff:ff
inet 192.168.0.102/24 brd 192.168.0.255 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::65d9:3a1f:6ec8:16fd/64 scope link
valid_lft forever preferred_lft forever

  

可以发现多了个enp0s8的配置,这就是我们刚才在虚拟机里面添加的桥接模式的网卡,添加网卡配置:

[root@localhost ~]# cd /etc/sysconfig/network-scripts

[root@localhost network-scripts]# cp ifcfg-enp0s3 ifcfg-enp0s8

[root@localhost network-scripts]# vi ifcfg-enp0s8

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s8
UUID=02e3fd29-ae05-4131-9f9d-7ed71057531a #删掉uuid
DEVICE=enp0s8
IPADDR=192.168.0.102 #ip配置和本机ip在同一个网段
ONBOOT=yes

[root@localhost network-scripts]# service network restart [pigfly@localhost ~]$ ping -c 2 192.168.0.104 PING 192.168.0.104 (192.168.0.104) 56(84) bytes of data. 64 bytes from 192.168.0.104: icmp_seq=1 ttl=128 time=0.221 ms 64 bytes from 192.168.0.104: icmp_seq=2 ttl=128 time=0.448 ms --- 192.168.0.104 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.221/0.334/0.448/0.114 ms [pigfly@localhost ~]$ ping -c 2 baidu.com PING baidu.com (123.125.114.144) 56(84) bytes of data. 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=1 ttl=50 time=42.3 ms 64 bytes from 123.125.114.144 (123.125.114.144): icmp_seq=2 ttl=50 time=43.1 ms --- baidu.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms rtt min/avg/max/mdev = 42.324/42.752/43.180/0.428 ms

  

 主机ping虚拟机:

到此主机和虚拟机互相能ping通,虚拟机也可以ping通外网,这样就OK了吗?

由于我的wifi老是掉线,只要wifi一掉线,虚拟机和主机就互相都ping不通了,这个不太好啊,于是又把桥接改成Host-Only:

 

我们打开主机的网络连接看看这块Host-Only网卡的配置:

 

得知网卡ip地址是192.168.56.1,如此一来虚拟机的网卡配置也因做相应更改:

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# vi ifcfg-enp0s8
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s8
UUID=efa33ed9-ee5e-492d-98f8-64b4701aba30
DEVICE=enp0s8
IPADDR=192.168.56.100 #和虚拟网卡在同一个网段
ONBOOT=yes
[root@localhost network-scripts]# service network restart
posted @ 2017-10-23 18:45  pigfly  阅读(513)  评论(0编辑  收藏  举报