All right 到目前为止 您 fairly versed with KVM เราเคยมี handful of tutorials 包括 basic introduction 和 intermediate setup and use network and storage configurations 和 setting up KVM alongside VirtualBox next logical step 是学习关于 bridged networking 以便我们可以给 virtual machines usable external IP addresses 在我们需要 forward ports 和 whatnot 时
สิ่งที่我今天将向您展示的东西将与您在其他教程中看到的有所不同 具体来说 我将尝试绕过您可能在其他教程中看到的 brute-force approach 我将向您展示两种可能的方法来 expose your virtual machines to the external world 其中一种方法是 hack 但它 absolutely genius 您从未见过!和我们会谈谈 limitations 和 possible problems
Note: Image taken from Wikipedia, licensed under CC BY-SA 3.0; index teaser, same terms.
Basic preparations
让我们从 reality check 开始 Bridged networking 有点 ugly 和 require some understanding of the Linux command line 然而 有时 bridging 是 necessary 如果您想 run some kind of a server inside a virtual machine 您可能想给它 an external IP address 以便 other hosts can communicate with it directly
Example: Your router grants addresses in the range 10.0.0.X Your physical host uses 10.0.0.4 但 your virtual machine runs with 192.168.2.133 没有 some changes to how the system interprets packets sent back and forth clients on the 10.0.0 network 将不能 talk to clients on the 192.168.2 network
您可以 work around the problem by using bridging 这将 allow your virtual machines to connect to the router and gain external IP addresses 在此语境中 external means whatever your physical hosts are using 不一定 non-private addresses
By default KVM creates its own bridge 192.168.122.1 This device acts as a virtual router for your virtual machines and will grant them IP addresses in this range 然而 我们想要我们的 machines lease external addresses 在此教程中 这是 192.168.2 range
Two alternative solutions
有两种方式我们可以尝试做这件事
One 我们将 create a bridge Two devices will be bridged 我们的 physical device eth1 和 the virtual device called vnet0 在我们 ascertain that our bridge works properly 我们将 commit the changes to a network configuration file on the disk Virtual machines will lease their IP addresses from the router 这类似于我们在 VirtualBox 中做的 这是我们将在这里 cover 的方法
The second method is not guaranteed to work 但它 just might 如果您的 network adapter supports bridging out of the box 那么您 may be able to use it directly 唯一的 change 您将需要做的是 assign a small subnet of addresses to your virtual bridge in KVM which does not overlap with the addresses used by physical hosts on the network
Limitations
有很多事情可能出错 您的 router may not support bridging 您的 network interface may not support bridging 您的 available pool of IP address leases may be limited 您可能有 a firewall interfering 最后 专门针对 Ubuntu 您可能会遇到 problems 和 conflicts 如果 Network Manager is managing the bridged interface
更多阅读 请看以下 documentation:
- Bridge setup on Linux Foundation
- KVM Networking - Ubuntu Community Documentation
好的 现在您知道什么在前面等着我们 Bridging definitely not a simple thing 有几件事您需要 take into account 首先 您的 network adapter 支持 bridging at all? 例如 在一些 laptops Wireless adapters 不 really support this mode of work 第二 您 comfortable fiddling with network settings? 第三 您在尝试 achieve 什么? Perhaps NAT is good enough for you?
Basic networking concepts
虽然我的 test box 是 Ubuntu 我希望这个 tutorial 是 distro-agnostic Following Ubuntu KVM community documentation 是一件好事 但它 will only work for Debian based systems 因此 让我们 deviate a little 和 introduce networking concepts that are applicable for both RedHat and SUSE systems
On Debian-based systems like Ubuntu persistent network configurations are kept under /etc/network/interfaces On RedHat-based systems plus SUSE friends and family network configurations are stored in individual files under /etc/sysconfig/network usually prefixed with ifcfg-ethX or ifcfg-eth-MAC-address Do replace the generic eth with corresponding interface name as you see them on your system using the ifconfig command
您也可以 create bridges using the brctl utility on the command line without committing changes to your configuration files 但 settings will be lost every time you restart the network service 这是 recommended way of testing things first before editing any system files 和不要忘记 backup your configurations before fiddling 更多关于 network setups 的细节 请看我的 Linux commands article
Create bridge
现在 我们准备玩 我建议您开始使用 brctl, ifconfig 和 dhclient commands 您会 excuse me if I don't go into too many details on how these are used 如果有 popular demand 会有 extra tutorial 一般来说 sequence 是 as follows (with sudo or as root) - let's assume br0 physical and/or virtual devices eth0/1 and DHCP lease from the switch or the router:
ifconfig eth0 0.0.0.0
ifconfig eth1 0.0.0.0
brctl addbr br0
brctl addif br0 eth0
brctl addif br0 eth1
dhclient br0
请注意 virtual adapter 可能不可用 until you create and start the virtual machine 因此您不需要 add the virtual adapter just yet KVM will do that automatically for you if you choose bridged networking in the advanced options
如果您看到 your bridge interface leasing the external address from its DHCP server 那么您 on the right track 对于 home users DHCP server will be the router 如果您有 direct Internet connection 这可能不可能 因为您的 ISP might not allow more than one IP address per user
一旦 bridged interface comes up 确保您的 network functionality remains unchanged 您可能需要 edit your /etc/resolv.conf 和 possibly fix the routing table
如果这 works 您可能想要 commit the changes to the network configuration file 请注意我的例子是 generic 非常类似于 community documentation 中显示的 例如 您可能需要 enable the Spanning Tree Protocol (STP) on your bridge 以 properly handle multiple hops or cyclic routes
具体来说 在我们的案例中 我们想要 edit the /etc/network/interface and add br0 there 如果您 require STP 那么请 change bridge_stp off to bridge_stp on 您可以 verify what your bridge does by issuing btctl show command

和一些 STP fun - sounds naughty doesn't it

Create virtual machine
我们之前学过如何做这个 所以这不是新事物 在 final stage under Advanced options change the network address assignment 和 make sure the virtual adapter has a unique MAC address that does not conflict with any other


如果您 lucky 您可能甚至不需要 setting up special bridging 您的 network adapter could support it out of the box 然后您 fine! 但您可能 not going to be lucky 所以 expect a handful of hard keyboard labor
To make sure everything works:

如果您可以 ping it 您可以 hack it 这 almost identical to what we did with VirtualBox 现在 您可以 expose services share data 和 whatever you need Enjoy
Alternative setup (might not work, but it's brilliant)
您可能 not so lucky Network Manager may refuse to cooperate 和 there might be a ton of other issues with your setup 为此 您可能想要 create a separate virtual network adapter 和 route it to a physical device that you believe supports bridging out of the box Mind the conflicting IP addresses
最后 如果这 works for you 那么您可以尝试 a very dirty hack Create a virtual network that shares the same /24 space with your network 例如 如果您的 router is on the 192.168.2.0 network create a virtual network that starts at 192.168.2.160 和 give it only a small range maybe 16 addresses altogether Make sure your router never gives out those addresses to other clients on the network
这里是一个例子: 我创建了一个 network 192.168.2.160/28 我不 expecting my router to ever lease an address in that range 但我可以 limit its DHCP assignment 然后 route the virtual network to a physical device 在此例中 是 Wireless adapter eth1
现在 创建您的 virtual machine 和 route to eth1 就像上面 一旦它 online 注意 lovely cooperation Host 有 .102 address virtual NIC virbr1 有 .161 address 而 virtual machine came up with .168 address
和这里是 a ping from a physical Windows 7 box on the same network with .103 address pinging the virtual adapter with .161 address on the Ubuntu Lucid box both Wireless both connected through the same router showing how cute and smart this setup really is 现在 它 won't always work 但当它 does 它是 genius
Existing virtual machines
对于 virtual machines that already exist 您也可以 edit the settings by changing directives under /etc/libvirt/ Each virtual machine has a separate .xml configuration file 您将需要 power cycle your virtual machines for any change to take effect 例如:
<interface type='bridge'>
<mac address='00:11:22:33:44:55'/>
<source bridge='br0'/>
</interface>
Conclusion
KVM bridged networking setup is not really intuitive 它 about as friendly as VirtualBox was some two or three years ago 但它 manageable 虽然 your real limitations will be the understanding and the functionality of the network topology 它将 surely get better with time
如果您 lucky 您的 network adapters will support bridging out of the box 如果不是 您将需要 create a bridge adapter 和 assign physical and virtual network devices 然后 resolve any potential issues with DNS, DHCP 或 routing 最后 作为 a dirty hack 您可能想要尝试 abusing your network ranges and creating a subnet within a subnet make sure the IP addresses never overlap 和 then route your virtual adapter to your physical device
这个 tutorial is fairly basic yet with a cunning twist 但可能有 numerous issues left uncovered 如果您有 questions suggestions 或需要 tips on overcoming difficult network setups in KVM 发邮件给我 我可能会 concoct a sequel howto addressing these new problems
Cheers.