Today to introduce you to QEMU network configuration method.
QEMU two ways to access the Internet:
User Mode network:
This way to achieve a virtual machine online is very simple, similar to the VMware Nat,qemu startup when the-user-net parameters, virtual machines using DHCP, you can communicate with the Internet, but this way the virtual machine and host communication is not convenient.
Tap/tun Network:
This is a bit more complicated than user mode, but it's easy to set up a virtual machine <--> an Internet virtual machine <--> host communication.
This way, a host-only,qemu like VMware is set up to use the TUN/TAP device to add a virtual network device (TUN0) to the host, and then configure it like a real network card.
First, tap/tuns devices are supported in the kernel:
Device Drivers--->
Networking Support--->
[M] Universal Tun/tap device driver Support
If the current kernel does not support it, you can recompile the module by adding it simply:
The current kernel profile CP to the kernel source directory:
[Root@lfs ~] #cp/boot/config-[kernel-version]/usr/src/linux
[root@lfs ~] #cd/usr/src/linux
Configure the kernel to select the Tun/tap module (M) as shown above:
Re-compile only module (M), do not compile core (*) supported Dongdong:
[Root@lfs ~] #make Modules
After compiling, the Tun.ko can be found under/usr/src/linux/drivers/net:
[Root@lfs NET] #ls-L/usr/src/linux/drivers/net/tun.ko
-rw-r--r--1 root 11116 Mar 20:29/usr/src/linux/drive Rs/net/tun.ko
[Root@lfs net]#
It is CP to the current kernel's module directory at the appropriate location:
[Root@lfs NET] #cp/usr/src/linux/drivers/net/tun.ko/lib/modules/' uname-r '/kernel/drivers/net
To re-establish a module dependency:
Now it's ready to load:
[Root@lfs NET] #modprobe Tun
Check:
[Root@lfs NET] #lsmod |grep Tun
Tun 8704 0
[Root@lfs net]#
Ok. Success does not recompile the entire kernel join special module support
If your XX card is not supported by the kernel, you can compile into a module, you are afraid of recompiling the kernel after the problem can be used to compile only the module you need, and then manually install to the appropriate location, and then load it.
This compiles the speed to also be quicker than compiles the entire kernel, does not have any damage to the system, may use on the XX card. ^_^
Pay attention to three points:
1. Kernel source code must be exactly the same as the current kernel version, otherwise the compiled module is not available.
2. Note Only make modules (compiled module), no make Modules_install (automatic installation module to/lib/modules)
3. You must run Depmod before loading the newly compiled module, otherwise modprobe cannot find it
In fact, using the current kernel configuration file (/boot/config-[kernel-version]), only to add the modules you need, do not do any other changes, make modules_install should not have problems.
But the safest way is to manually install it, control in their own hands more down-to-earth:-)
OK, turn back to QEMU on the internet:)
If you use a Udev management device (typically the distribution of the 2.6.x kernel already uses udev), the/dev/net directory is automatically created after Modprobe Tun, and the Tun device is built to do the relevant linking:
[Root@lfs NET] #ls-L/dev/net/tun
lrwxrwxrwx 1 root root 6 Mar 15:35/dev/net/tun->. /tun
[Root@lfs net]#
If it's unfortunate that you don't see it, you need to do the work yourself manually:(
[Root@lfs ~] #mkdir/dev/net
[root@lfs ~] #mknod/dev/net/tun c 10 200
OK, the related device is ready and you need a TUN/TAP initialization script:
/etc/qemu-ifup:
#!/bin/sh
/sbin/ifconfig $172.20.0.1
Then give Qemu-ifup x Execute permission to put it under/etc.
This script can only be executed by the root user, and if you need to use QEMU for the average user, you need to change to sudo/sbin/ifconfig ... Then set sudo related permissions.
When QEMU is started, it adds a virtual network device (TUN0) to the host:
[Root@lfs ~] #ifconfig tun0
tun0 Link encap:ethernet hwaddr 0a:d3:8a:5d:97:cd inet addr:172.20.0.1
: 172.20.255.255 mask:255.255.0.0 up
broadcast RUNNING multicast mtu:1500 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:500
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
[Root@lfs ~]#
You can now start Qemu to configure the virtual machine network parameters, just like VMware host-only:
IP and tun0 the same network segment, Gateway for tun0 IP remaining parameters and tun0 the same, DNS and host the same:
tun0:ip:172.20.0.1 broadcast:172.20.255.255 netmask:255.255.0.0
qemu:ip:172.20.0.100 broadcast:172.20.255.255 netmask:255.255.0.0 gateway:172.20.0.1
Like Host-only, this only implements the virtual machine <----> communication between the hosts, but also need to set up Router,nat to connect to the Internet
[Root@lfs ~] #echo 1 >/proc/sys/net/ipv4/ip_forward
[root@lfs ~] #iptables-t nat-a postrouting-o eth0-s 172.20.0 .0/24-j Masquerade
[Root@lfs ~]#
OK, Virtual machine <---> Host virtual machine <----> Internet communication is complete.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.