在默认情况下,如果不指定任何网络模式的话,kvm会默默的为我们选择NAT模式,除了NAT之外还有Bridge及Vlan模式。

        

User Networking(NAT模式)

        采用该模式,不需要对宿主机进行任何额外配置。正常情况下虚拟机可以直接访问宿主机的物理网络、宿主机无法访问虚拟机。

        优点是简单、无需任何配置,如果宿主机可以上网则虚拟机也可以上网。

        缺点是虚拟机与虚拟机之间网络不可达、ICMP协议无法正常工作(即ping功能不能正常返回值)、NAT采用的是转发机制,因此效率比较低。

# 宿主机: ip地址
[zhengtong@localhost qemuImages]$ ifconfig p3p1
p3p1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.102  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::ba88:e3ff:fef3:af7c  prefixlen 64  scopeid 0x20<link>
        ether b8:88:e3:f3:af:7c  txqueuelen 1000  (Ethernet)
        RX packets 232953  bytes 210207598 (200.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 282248  bytes 40669709 (38.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# 宿主机: 可以正常访问互联网
[zhengtong@localhost qemuImages]$ ping 
PING www.a.shifen.com (119.75.217.56) 56(84) bytes of data.
64 bytes from 119.75.217.56: icmp_seq=1 ttl=56 time=33.8 ms
64 bytes from 119.75.217.56: icmp_seq=2 ttl=56 time=31.8 ms 

# 宿主机: 启动虚拟机
[zhengtong@localhost qemuImages]$ sudo qemu-kvm -m 2048 -hda networkNode.img


虚拟机:

        ping www.baidu.com                  结论: ping不通互联网

        ping 宿主机(192.168.1.102)        结论: ping不通宿主机及互联网

        wget http://www.baidu.com        结论: 可以正常下载页面

wKioL1R7OkfQmBOpAAWLvPCsCv0257.jpg


        最后通过一些高级特性可以实现宿主机访问虚拟机。

# 宿主机: 启动虚拟机时提供hostfwd参数
# 该参数允许宿主机通过60000端口连接到虚拟机的22端口, 其他方面无任何变化.
[zhengtong@localhost qemuImages]$ sudo qemu-kvm -m 2048 -hda networkNode.img -net nic -net user,hostfwd=tcp:127.0.0.1:60000-:22

# 宿主机: 连接到虚拟机    结论: 可以正常连接到虚拟机
[zhengtong@localhost qemuImages]$ ssh root@localhost -p 60000
The authenticity of host '[localhost]:60000 ([127.0.0.1]:60000)' can't be established.
RSA key fingerprint is 42:73:36:58:f3:75:12:35:8a:07:8d:4d:32:1a:59:3a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:60000' (RSA) to the list of known hosts.
root@localhost's password: 
Last login: Sun Nov 30 23:53:27 2014


[root@networkNode ~]# ifconfig 
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::5054:ff:fe12:3456  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:12:34:56  txqueuelen 1000  (Ethernet)
        RX packets 66  bytes 8768 (8.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 81  bytes 11168 (10.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 6  bytes 560 (560.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 560 (560.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0