First-time kvm experience 5: two ways for vm to connect to the network: bridge and nat. First-time kvm experience

Source: Internet
Author: User

First-time kvm experience 5: two ways for vm to connect to the network: bridge and nat. First-time kvm experience

1.InstallVmSpecify the network connection mode

1) bridge

Virt-install -- name vm1 -- ram = 1024 -- vcpus = 1 -- disk path =/vm-images/vm1, size = 2 -- cdrom/root/iso/CentOS-6.6-x86_64-minimal.iso -- network bridge: br0

2) nat

Virt-install -- name vm1 -- ram = 1024 -- vcpus = 1 -- disk path =/vm-images/vm1, size = 2 -- cdrom/root/iso/CentOS-6.6-x86_64-minimal.iso -- network: default

If the-network parameter is not specified, the bridge mode is used by default.


2.InstallVmThen modify the network connection mode.

Shut down the vm before modifying the vm network connection mode. After the modification, start the vm to take effect.

1) graphic interface Modification

Use virt-manager to open the vm management page, as shown in

Nat: select "Sourcedevice" as Virtualnetwork 'default '.

Bridge: Select "Sourcedevice" as Hostdevice eth0 (Bridge 'br0 ')

2) use the command line to modify

Virsh edit vm1 modify the vm1 configuration file (the vm configuration file exists in xml format, such as vm1.xml)

<Interface> specifies the network connection mode of the vm.

Nat:

<Interface type = 'network'>

<Mac address = '52: 54: 00: eb: d7: 7d '/>

<Source network = 'default'/>

<Address type = 'pci 'domain = '0x0000' bus = '0x00' slot = '0x03' function = '0x0'/>

</Interface>

Bridge:

<Interface type = 'bridge '>

<Mac address = '52: 54: 00: eb: d7: 7d '/>

<Source bridge = 'br0'/>

<Address type = 'pci 'domain = '0x0000' bus = '0x00' slot = '0x03' function = '0x0'/>

</Interface>


3.NewVirtual network

1) Add an image through the graphic interface

Virt-manager open the vm management interface and select Edit-> Connection Details. The Virtual network already exists in the virtual Networks Tab Of the localhost Connection Details window that appears. After libvirt is installed on the host, the virtual network interface virbr0 is created by default, and its forwarding mode is nat.

Now we create a virtual network nat1, and the forwarding mode is nat

Select "+" in the Left box"

Forward

Forwad

Forwad

Forwad

Forwad

Finish.

The Localhost Connection Details window displays the newly added nat1 network information.

Select the virtual network interface of vm1 as the created nat1

Shut down vm1 and start it.

As you can see, vm1 obtains the address in the address segment of the nat1 network.

 

 

2) add

 

Reference: http://wiki.libvirt.org/page/Networking

The net-xxx command under virsh is used to manage virtual networks.

[Root @ tanghuimin thm] # virsh

Welcome to virsh, the parameter alization interactive terminal.

 

Type: 'help' for help with commands

'Quit' to quit

 

Virsh # net-

Net-autostart net-define net-dumpxml net-info net-name net-undefine net-uuid

Net-create net-destroy net-edit net-list net-start net-update


Net-list-all: list existing virtualnetwork

Virsh # net-list -- all

Name State Autostart Persistent

--------------------------------------------------

Default active yes

Nat1 active yes


Virtual network configuration files also exist in xml format, such as nat1.xml

[Root @ tanghuimin thm] # find/-name "nat1.xml"

/Etc/libvirt/qemu/networks/nat1.xml

/Etc/libvirt/qemu/networks/autostart/nat1.xml

/Var/lib/libvirt/network/nat1.xml


Create a virtual network nat2 configuration file using the default network configuration as the template

[Root @ tanghuimin thm] # virsh net-dumpxml default>/etc/libvirt/qemu/networks/nat2.xml


Modifying the parameters in nat2.xml does not overlap the existing virtual network.

<Network>

<Name> nat2 </name>

<Uuid> d8d274ff-fe1f-4e88-94f9-222c1ae11c69 </uuid>

<Forward mode = 'nat '/>

<Bridge name = 'virbr2' stp = 'on' delay = '0'/>

<Mac address = '52: 54: 00: 25: 90: c2'/>

<Ip address = '1970. 168.101.1 'netmask = '1970. 255.255.0'>

<Dhcp>

<Range start = '192. 168.101.2 'end = '192. 168.101.254 '/>

</Dhcp>

</Ip>

</Network>


Net-define defines virtual networks from xml files

Virsh # net-list -- all

Name State Autostart Persistent

--------------------------------------------------

Default active yes

Nat1 active yes

Virsh # net-define/etc/libvirt/qemu/networks/nat2.xml

Network nat2 defined from/etc/libvirt/qemu/networks/nat2.xml

Virsh # net-list -- all

Name State Autostart Persistent

--------------------------------------------------

Default active yes

Nat1 active yes

Nat2 inactive no yes


Set nat2 to self-start and start nat2

Virsh # net-autostart nat2

Network nat2 marked as autostarted

 

Virsh # net-start nat2

Network nat2 started

 

Virsh # net-list -- all

Name State Autostart Persistent

--------------------------------------------------

Default active yes

Nat1 active yes

Nat2 active yes


Select the virtual network interface of vm1 as the created nat2

Virsh edit vm1


......

......

<Interface type = 'network'>

<Mac address = '52: 54: 00: eb: d7: 7d '/>

<Source network = 'nat2'/>

<Address type = 'pci 'domain = '0x0000' bus = '0x00' slot = '0x03' function = '0x0'/>

</Interface>

......

......

Shut down vm1 and start it.

As you can see, vm1 obtains the address in the address segment of the nat2 network.

[Root @ localhost ~] # Ifconfig

Eth0 Link encap: Ethernet HWaddr 52: 54: 00: EB: D7: 7D

Inet addr: 192.168.101.21 Bcast: 192.168.101.255 Mask: 255.255.255.0

Inet6 addr: fe80: 5054: ff: feeb: d77d/64 Scope: Link

Up broadcast running multicast mtu: 1500 Metric: 1

RX packets: 12 errors: 0 dropped: 0 overruns: 0 frame: 0

TX packets: 16 errors: 0 dropped: 0 overruns: 0 carrier: 0

Collisions: 0 FIG: 1000

RX bytes: 1198 (1.1 KiB) TX bytes: 2178 (2.1 KiB)

Interrupt: 11 Base address: 0x8000

 

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: 65536 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)

 

[Root @ localhost ~] # Ping 192.168.1.1

PING 192.168.1.1 (192.168.1.1) 56 (84) bytes of data.

64 bytes from 192.168.1.1: icmp_seq = 1 ttl = 63 time = 5.53 MS

#

--- 192.168.1.1 ping statistics ---

1 packets transmitted, 1 received, 0% packet loss, time 926 ms

Rtt min/avg/max/mdev = 5.531/5.531/5.531/0.000 MS

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.