Why to Do This?
Essentially it is a user case that:
- sharing network between a host and a guest
- testing how a host and guest respond to each other
Setup DHCP Server and Share the Networking to Clients Emulated by QEMU
Setup DHCP Server
Add Tap Device
In our case, we use a new tap device to connect the server and client. Let's create the device by:
$ ip tuntap add tap0 mode tap user root
$ ip addr add dev tap0 192.10.0/24
$ ip link set tap0 up
tap0
is the name of your tap device. It could be any name you want.- the subnet
192.10.0/24
is also up to you. Specify a range of IP you are interested in.
If everything is smooth, ip a
should show your new tap device and its status is UP
.
Get the Server Ready
There are different DHCP servers available on Ubuntu. In this case we choose ISC DHCP to run the server.
Firstly install the package:
sudo apt install -y isc-dhcp-server
Then edit the dhcp table so your dhcp server knows how to arrange the IPs:
cat > /etc/dhcp/dhcpd.conf <<EOF
subnet 192.10.0.0 netmask 255.255.255.0 {
authoritative;
range 192.10.0.1 192.10.0.254;
default-lease-time 3600;
max-lease-time 3600;
option subnet-mask 255.255.255.0;
option broadcast-address 192.10.0.255;
option routers 192.10.0.0;
option domain-name-servers 8.8.8.8;
option domain-name "example.com";
}
host your-client {
hardware ethernet <your mac address>;
#fixed-address 192.10.0.100;
option host-name "your-client";
}
EOF
Again, the IP like 192.10.0.0
etc. are something you customize. If you have no idea which value they should be,
you may read more knowledge about basic networking like broadcast address, subnet mask...etc.
Optionally, touch /var/lib/dhcp/dhcpd.leases
only if there is no such file. You can find the descripton of its
manual by man dhcpd.leases
:
When dhcpd is first installed, there is no lease database. However, dhcpd requires that a lease database be present before it will start. To make the initial lease database, just create an empty file called
/var/lib/dhcp/dhcpd.leases. You can do this with:
touch /var/lib/dhcp/dhcpd.leases
Restart the dhcpd by:
sudo systemctl restart isc-dhcp-server.service
and feed the table to the interface tap0
and force the dhcpd runs in foreground for debugging.
dhcpd -f tap0
Troubleshooting
- If you have permission issue of
dhcpd.leases
when running the abovedhcpd -f
command, you can make it work by:
chmod +777 /var/lbi/dhcp/dhcpd.leases
- In some cases that you may not launch the process with
sudo
permission, you may need to make the following tweaks:
chown $(whoami) /etc/dhcp
chown $(whoami) /var/lib/dhcp/
- In dhcp table, this line
fixed-address 192.10.0.100
with the overlapped IP with your subnet is unnecessary. If there is such a line, you will get message in yoursyslog
likeRemove host declaration
. See this email thread.
Set up the Client with QEMU
This Ubuntu Wiki page is very useful. Read it before you move forward.
Edit the -netdev
with the specified option ifname
:
-netdev type=tap,id=net0,ifname=tap0,script=no,downscript=no
Again, note tap0
is the tap device of the host.
Ready and Testing
By monitoring syslog
of the host, you can see the dhcpd begins to get the broadcast of packt from the qemu client
when booting the instance. If you see such log and the guest gets the IP from your subnet range. Your dhcp cluster
is working properly now.
Testing host-name
In the dhcp table, there is an option host-name
. If you want to see if your guest instance a.k.a. the dhcp client
works properly to get the host-name dispatched by the host, edit the content of /etc/hostname
to make the content
is localhost
. Rebooting your guest instance, if the instance fetches dhcp data properly during boot process, you
will see the hostname changes.