Dynamically Allocating Client IP Addresses via DHCP
Problem
You want to configure your router to be a DHCP server and allocate dynamic IP addresses to client workstations.
Solution
The following set of configuration commands allows the router to dynamically allocate IP addresses to client workstations:
Router1#configure terminal Enter configuration commands, one per line. End with CNTL/Z. Router1(config)#service dhcp Router1(config)#ip dhcp pool 172.25.1.0/24 Router1(dhcp-config)#network 172.25.1.0 255.255.255.0 Router1(dhcp-config)#default-router 172.25.1.1 Router1(dhcp-config)#exit Router1(config)#ip dhcp excluded-address 172.25.1.1 172.25.1.50 Router1(config)#ip dhcp excluded-address 172.25.1.200 172.25.1.255 Router1(config)#end Router1#
Discussion
Cisco incorporated DHCP server functionality starting in IOS Version 12.0(1)T. This allows routers to dynamically allocate IP addresses to client workstations without needing a centralized DHCP server.
Providing DHCP services from a router has some interesting advantages over using a central server. First, distributing the DHCP functionality into the access routers of a large network reduces the risk of a server configuration problem affecting the entire corporate network. Second, maintaining DHCP services within each remote branch router reduces the utilization over expensive WAN links. Third, when a WAN link fails, workstations are still able to function on the local segment. And fourth, the router based DHCP services can easily be administered by your network engineers via one common interface, the IOS prompt.
In our example, we have configured the router to dynamically allocate IP addresses for the subnet 172.25.1.0/24. First, we define a range of IP addresses using the network command. Then we need to exclude some addresses from the dynamic range with the ip dhcp exclude-address command. If you do not exclude a range of addresses, then the router will assign IP addresses from the entire subnet (254 addresses in total). But in most networks, at least some of the addresses are reserved for other purposes, such as servers or network equipment.
At a minimum, you should exclude the router's own IP address to prevent the DHCP service from trying to assign it elsewhere, causing a conflict. Many network engineers allocate the lower portion of the subnet to static devices such as routers, servers, printers, and other devices that do not support DHCP. In our example we reserved the first 50 addresses for statically addressed devices (172.25.1.1 172.25.1.50). We also chose to reserve addresses from 172.25.1.200 to 172.25.1.255 for future use, and to show that you can reserve multiple blocks of addresses.
When DHCP is enabled, the router will allocate IP addresses dynamically by binding them to device MAC addresses in the configured pool. IP addresses are allocated for a defined period of time called a lease, which we will discuss further in Recipe 20.6. You can view the address bindings with the show ip dhcp binding command as follows:
Router1#show ip dhcp binding IP address Hardware address Lease expiration Type 172.25.1.51 0100.0103.85e9.87 Apr 10 2003 08:55 PM Automatic 172.25.1.52 0100.50da.2a5e.a2 Apr 10 2003 09:00 PM Automatic 172.25.1.53 0100.0103.ea1b.ed Apr 10 2003 08:58 PM Automatic Router1#
You will notice in this output that the MAC addresses look a little bit funny, because they have two extra hex digits (one extra octet) at the start. We will explain this in Recipe 20.7.
The router maintains an internal database of assigned IP addresses and their associated MAC addresses. In Recipe 20.8, we will show how to write this database to a remote server for backup purposes.
Note also that the example dynamically configures DCHP clients with the default router setting using the default-router command. We will discuss these DHCP options in detail in Recipe 20.5.
See Also
Recipe 20.5; Recipe 20.6; Recipe 20.7; Recipe 20.8; Recipe 20.9