This chapter shows how to setup a DHCP server to automatically issue IP addresses to clients inside a network.
This chapter assumes you have knowledge of:
Basic Linux command line skills, as covered in Chapter 4, are assumed. You will need to be able to:
All of the practical tasks in this chapter can be completed on a single Linux computer. Most of the demonstrations use a single Linux computer, specifically node1 in virtnet (Chapter 3). Although virtnet is not required, if you do use it, as only a single computer is necessary, topology 1 is appropriate (or in fact any topology—just use a single node).
When an operating system is installed on a computer and the computer first setup (by, for example, the network administrator), the IP address and other relevant network information (such as DNS servers, subnet mask) can be manually entered. In Ubuntu, commands like ifconfig can be used to do this.
But with manual configuration, if any network information changes, the network administrator must then go to each computer to make the changes. For example in a university network with 100’s or even 1000’s of computers, the task of manually configuring each computer if, for example, the DNS server IP address changes, would be enormous!
Therefore, in practice there are ways to automatically configure a computers network information. The most used method is called Dynamic Host Configuration Protocol or DHCP1 . The basic process using DHCP is as follows:
The information assigned to the host by the DHCP Server has a lifetime. This is called a lease—for example, the host “leases” an IP address for 1 day. Before the lease expires, the DHCP Client will typically renew the lease. In this way, if a change of configuration information (such as DNS server) is needed, the network adminsitrator simply modifies the DHCP Server—the DHCP Clients in each host will retrieve the updated information from the DHCP Server.
Many computers now use DHCP to obtain an IP address, so the computer user does not need to worry about configuring their own IP address. For example, when you connect to a university network with your laptop, typically you do not configure an IP address—DHCP is used.
If you are only interested in learning about DHCP, then you may not need to install a server, since there is probably one already in your subnet. You may skip this section and see how to use a DHCP client in Section 14.4, or monitor and existing server in Section 14.5. If you do need to install a DHCP server, then read on.
First identify which computer to install the DHCP server on. Often it is a router in a subnet, although it may be a host. On that computer, install the ISC DHCP server:
network@server:~$ sudo apt install isc-dhcp-server
An important part of configuring a DHCP server is telling it which network interface to use. For example, if the server is running on a computer with two network interfaces (e.g. a router), then normally the DHCP server will allocate IP addresses via one interface to computers on an internal network, but not the other interface connected to an external network.
For this demonstration, let’s assume the internal interface is eth2 and the external interface is eth1. We need to setup the DHCP server to allocate addresses via eth2. This is done by editing the file /etc/default/isc-dhcp-server with your favourite text editor (ensure you use sudo). Once the file is open, edit the line specifying INTERFACES, ensuring the internal interface is listed:
INTERFACES="eth2"
Now we need to specify the range of IP addresses to allocate to hosts on the internal subnet. This is done in the configuration file /etc/dhcp/dhcpd.conf. Read through the comments in the file and change values as desired. Below we will present an example file, with comments removed for brevity, in three parts:
The global/default settings are below. As we do not have DNS servers, you may remove the domain-name-servers option. The parts changed are in bold.
ddns-update-style none;
option domain-name "example.com";
#option domain-name-servers ns1.example.org, ns2.example.org;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
The next part is to allocate IP addresses to all hosts in a subnet. This example allocates addresses from the range 192.168.2.100 to 192.168.2.200 to hosts in the subnet.
subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.100 192.168.2.200;
option domain-name "example.com";
option subnet-mask 255.255.255.0;
option routers 192.168.2.2;
option broadcast-address 192.168.2.255;
default-lease-time 600;
max-lease-time 7200;
}
Although the above settings are sufficient, in some cases we would like to allocate a specific address to a specific computer. For example, normal hosts can receive any IP from the range 100-200, but a server should get a fixed address. One method to achieve this is to give specify mappings based on MAC addresses. The following allocates 192.168.2.50 to a server with MAC address 08:00:27:d6:04:07. The name web-server is just for information, while the hardware Ethernet address must be obtained from the server, e.g. using ifconfig.
group {
option routers 192.168.2.2;
option broadcast-address 192.168.2.255;
host web-server {
hardware ethernet 08:00:27:d6:04:07;
fixed-address 192.168.2.50;
}
}
You can add more host declarations as needed. The above example illustrates the main parts of the DHCP server configuration. You may change the example to optimise for your network. For example, it is not necessary to include the default-least-time in the subnet declaration if it is the same as the global declaration. For more explanation of the syntax and options in dhcpd.conf, see the man page:
network@server:~$ man dhcpd.conf
To apply the configuration changes you need to restart the DHCP server. However it is a good idea to check the configuration file for errors first. A syntax check can be performed as:
network@server:~$ dhcpd -t
If there are any syntax errors (e.g. missing semi-colons) then this will report them, including the line number.
Once the configuration file is correct, restart the DHCP server:
network@server:~$ sudo systemctl restart isc-dhcp-server
Then check the status—it should be active (running):
network@server:~$ sudo systemctl status isc-dhcp-server
To get your computer to obtain IP addresses from a DHCP server when it boots, you need to edit the /etc/network/interfaces file. Section 9.11.4 gives an example of editing this file. We need to make sure the interface uses DHCP, rather being static. The general format for specifying an interface to use DHCP is
auto INTERFACE
iface INTERFACE inet dhcp
For example, if your computer has interface eth1:
auto eth1
iface eth1 inet dhcp
The next time the machine boots, it will use the DHCP client to get an IP address from the DHCP server (rather than statically assign an address). However if you don’t want to perform a reboot then you can trigger the DHCP client software to run manually using the command dhclient. First release any existing DHCP leases and then request a new one:
network@node:~$ sudo dhclient -r eth1
network@node:~$ sudo dhclient eth1
Make sure you specify the correct interface for your computer.
Now use ifconfig to view the new IP address.
By default, the DHCP server logs output into the system log /var/log/syslog. You can view the DHCP messages with:
network@server:~$ grep "DHCP" /var/log/syslog
DHCP leases are stored in text files. They contain information about the lease duration and owner and are stored in /var/lib/dhcp/dhcpd.leases.
The man pages provide useful information about file formats, e.g.
man dhcpd
man dhcpd.conf
man interfaces
man dhclient
man dhcpd.leases
Many websites provide examples and explanations. Some are from Ubuntu, OSTechNix, HowToForge, and TechMint.