guest我使用的是自己制作的一个linux版本的kvm模板机,磁盘文件是test.img ,安装命令为:
virt-install --name test01 --os-variant=linux --ram 2048 --vcpus=2 --disk path=/opt/test.img --network=bridge:br0 --vnc --vncport=5923 --vnclisten=0.0.0.0 --force --import --autostart
以上命令里需要重点关注一下这些参数:
--network=bridge:br0 --vncport=5923 --vnclisten=0.0.0.0
安装完毕后,生成的kvm配置文件是这样的:
[root@slave1 ~]# cat /etc/libvirt/qemu/test01.xml <!-- WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE OVERWRITTEN AND LOST. Changes to this xml configuration should be made using: virsh edit test01 or other application using the libvirt API. --> <domain type='kvm'> <name>test01</name> <uuid>91ee89da-55d7-4222-b1be-1217bc6b43d3</uuid> <memory unit='KiB'>2097152</memory> <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>2</vcpu> <os> <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> <apic/> </features> <cpu mode='custom' match='exact' check='partial'> <model fallback='allow'>SandyBridge</model> </cpu> <clock offset='utc'> <timer name='rtc' tickpolicy='catchup'/> <timer name='pit' tickpolicy='delay'/> <timer name='hpet' present='no'/> </clock> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <pm> <suspend-to-mem enabled='no'/> <suspend-to-disk enabled='no'/> </pm> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/opt/test.img'/> <target dev='hda' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <controller type='usb' index='0' model='ich9-ehci1'> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> </controller> <controller type='usb' index='0' model='ich9-uhci1'> <master startport='0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> </controller> <controller type='usb' index='0' model='ich9-uhci2'> <master startport='2'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> </controller> <controller type='usb' index='0' model='ich9-uhci3'> <master startport='4'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> </controller> <controller type='pci' index='0' model='pci-root'/> <controller type='ide' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <interface type='bridge'> <mac address='52:54:00:80:06:c6'/> <source bridge='br0'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='pty'> <target type='isa-serial' port='0'> <model name='isa-serial'/> </target> </serial> <console type='pty'> <target type='serial' port='0'/> </console> <input type='mouse' bus='ps2'/> <input type='keyboard' bus='ps2'/> <graphics type='vnc' port='5923' autoport='no' listen='0.0.0.0'> <listen type='address' address='0.0.0.0'/> </graphics> <video> <model type='cirrus' vram='16384' heads='1' primary='yes'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </memballoon> </devices> </domain>
此时,通过vnc客户端,可以通过宿主机IP 192.168.217.17+端口5923连接到kvm虚拟机,查询虚拟机IP如下:
此时,可以发现,该虚拟机的IP地址和宿主机是同一个网段了,因此,可以通过xshell等工具直连kvm虚拟机啦,通过xshell连接guest后,可以看到IP确实是192.168.217.129
如果是默认的nat模式,xshell等工具是无法连接的,只能ssh直连。bridge模式可以理解为guest虚拟机变为一台独立的主机啦。
总结:
kvm虚拟机需要设置恰当的网络模式,而bridge模式相对nat模式来说,应用范围更加广泛,更加的灵活。NAT方式是kvm安装后的默认方式。它支持主机与虚拟机的互访,同时也支持虚拟机访问互联网,但不支持外界访问虚拟机,从这一点来说,安全性自然相对bridge是要高一些的。