QEMU KVM libvirt Manual (9): Network

Source: Internet
Author: User
Tags nameserver

The virtual network card is defined by the-net NIC

# qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntutest.img-vnc:19-net NIC

If we view the info network in monitor

You can see the following

Sometimes, we see VLAN 0, where the VLAN and 802.1.q a little relationship, is the concept of virtual hub, in the new version has been changed.

We can do it in monitor.

Set_link e1000.0 off

Set_link e1000.0 on

To start and stop the network card

-net nic [, VLAN = n] [, macaddr = Mac] [, model = Type] [, Name = str] [, addr = str] [, vectors = v]

VLAN parameter indicates which virtual hub is connected to

You can specify Mac Addr

Model has a variety of

# qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntutest.img-vnc:19-net nic,model=?
qemu:supported NIC Models:ne2k_pci,i82551,i82557b,i82559er,rtl8139,e1000,pcnet,virtio

You can specify a name

Addr refers to the address on the PCI bus

We can add multiple NICs

# qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntutest.img-vnc:19-net nic,vlan=3,model=ne2k_pci-ne T user,vlan=3-net nic,vlan=2-net user,vlan=2

You can create a user-configured network

# qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntu-14.04.img-vnc:19-net nic-net User

The default assigned IP is starting with 10.0.2.15

But you can't ssh in from the outside.

Port-redirects from the host system (HOSTFWD)

# qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntu-14.04.img-vnc:19-net nic-net user,hostfwd=tcp:: 12345-:22

The packet on the host machine to Port 12345 is forwarded to Port 22 of the guest machine

We can ssh from the host

# ssh-p 12345 [email protected]
The authenticity of host ' [localhost]:12345 ([127.0.0.1]:12345) ' can ' t be established.
ECDSA key fingerprint is f2:ef:bc:8f:d8:bd:66:97:48:53:4d:0d:c1:d8:6d:c3.
Is you sure want to continue connecting (yes/no)? Yes
Warning:permanently added ' [localhost]:12345 ' (ECDSA) to the list of known hosts.
[email protected] ' s password:
Welcome to Ubuntu 14.04 LTS (gnu/linux 3.13.0-24-generic x86_64)

* documentation:https://help.ubuntu.com/

System information as of Sun Jul 14:12:34 EDT 2014

System load:0.0 Memory usage:2% processes:71
Usage of/: 39.9% of 2.89GB Swap usage:0% Users logged in:0

Graph this data and manage the system at:
https://landscape.canonical.com/

The packages can be updated.
Updates are security updates.

Last Login:sun Jul 13:03:03
[email protected]:~$ IP addr
1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65 536 Qdisc noqueue State UNKNOWN Group default
    link/loopback 00:00:00:00:00:00 BRD 00:00:00:00:00:0 0
    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:eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc Pfifo_fast State up G Roup default Qlen
    link/ether 52:54:00:12:34:56 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 BRD 10.0.2.255 scope global eth0
       valid_lft Forever Preferred_ LfT Forever
    inet6 fe80::5054:ff:fe12:3456/64 scope link
        Valid_lft Forever Preferred_lft Forever

-net user, hostfwd = [TCP | udp]: [HOSTADDR]: Host-port [GUESTADDR]: Guest port

You can also add and remove hostfwd with the following command in monitor

(QEMU) Hostfwd_add [vlan_id name] [TCP | udp]: [HOSTADDR]: Host-port [GUESTADDR]: Guest port

(QEMU) Hostfwd_remove [vlan_id name] [TCP | udp]: [HOSTADDR]: Host port

Redirects from the guest system (GUESTFWD)

Forward Guest TCP connections to the IP address in port to the server port character device dev . This option can be given multiple times.

# qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntu-14.04.img-vnc:19-net nic-net user,guestfwd=tcp: 10.0.2.1:80-stdio
Get/index.html http/1.1
user-agent:wget/1.15 (LINUX-GNU)
Accept: */*
host:10.0.2.1
Connection:keep-alive

The following output is shown when running wget http://10.0.2.1/index.html in guest

In the above network setup, we found that the guest system and the host system network is completely isolated, if you need to communicate, you need to carry out the FWD function, if you want to all-round communication, it is more difficult.

So there's TUN/TAP network interfaces.

It connects the network of the guest system with the network of the host system.

With Tun/tap adapter, a virtual network card TAP is generated on the host system, and Tun establishes a point-to-point network device, which makes the guest system's NIC and TAP virtual network card a pair, so guest All network packages for system, which can be received by the host system.

This driver basically allows a user-space application to obtain a file descriptor which are connected to a network device. Any frames sent to the kernel over the file descriptor would be a received by the device and any frames transmitted via the D Evice'll is received by the application.

The process is created as follows

(1) Create Bridge BR0 on the host machine

Brctl ADDBR Br0

(2) Set Br0 to Up

IP link Set br0 up

(3) Create a tap device

# Tunctl-b
Tap0

(4) Set Tap0 to Up

IP link Set tap0 up

(5) Adding tap0 to the BR0

Brctl addif br0 tap0

(6) Start the virtual machine

Qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntu-14.04.img-boot c-vnc:19-net nic,model=virtio-net Tap,ifname=tap0,script=no,downscript=no

Virtual Machine Connection Tap0,tap0 connection Br0

(7) After the virtual machine starts, the network card is not configured, so can not connect the network, first set an IP for BR0

Ifconfig br0 192.168.57.1/24

(8) In the virtual machine, set the address of the network card

Ifconfig eth0 192.168.57.2/24

This time in the virtual machine can ping the pass 192.168.57.1, but still can't access the extranet

(9) Set NAT on host and enable IP forwarding

# sysctl-p
Net.ipv4.ip_forward = 1

sudo iptables-t nat-a postrouting-o eth0-j Masquerade

(10) Set the default gateway inside the virtual machine

Route add–net default GW 192.168.57.1

This time, you can ping the extranet gateway

Ping 16.158.164.1

(11) Then set the DNS in the virtual machine, you can do Apt-get

# cat/etc/resolv.conf
# Dynamic resolv.conf (5) file for glibc resolver (3) generated by resolvconf (8)
# do not EDIT this FILE by HAND--YOUR changes'll be overwritten
NameServer 16.110.135.52
NameServer 16.110.135.51 multiple instances with sockets networking

The-net socket parameter allows multiple instance to be interconnected

TCP socket, one listening to connect, both connected, communicate with each other

Host ~ $ qemu-system-x86_64 plate01.img-net nic,macaddr=52:54:00:12:34:56-net socket, listen=:1234

Host ~ $ qemu-system-x86_64 plate02.img-net nic,macaddr=52:54:00:12:34:57-net socket, connect = 127.0.0.1:1234

instance that belong to different VLANs can also be connected to each other

UDP Multicast Socket

Host ~ $ qemu-system-x86_64 plate01.img-net nic,macaddr = 52:54:00:12:34:56-net socket, mcast = 230.0.0.1:1234

Host ~ $ qemu-system-x86_64 plate02.img-net nic,macaddr = 52:54:00:12:34:57-net socket, mcast = 230.0.0.1:1234

Host ~ $ qemu-system-x86_64 plate03.img-net nic,macaddr = 52:54:00:12:34:58-net socket, mcast = 230.0.0.1:1234

The same multicast group can communicate with each other bridging and switching

You can use Brctl (bridge-utils), or you can use the Openvswitch

Netdev parameters

-netdev [user | tap | vde | socket], id = str [, option] [, option] [, ...]

Network Protocol Analysis

-net dump [, Vlan=n][,file=f][,len=n]

Qemu-system-x86_64-enable-kvm-name ubuntutest-m 2048-hda ubuntu-14.04.img-vnc:19-net nic-net user-net Dump

A file is generated

Qemu-vlan0.pcap

Need to view with tcpdump

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.