Using DHCP to Dynamically Configure Router IP Addresses

Using DHCP to Dynamically Configure Router IP Addresses

Problem

You want the router to obtain its IP addressing information dynamically.

Solution

The ip address dhcp configuration command allows the router to obtain the address information for an interface dynamically:

Router1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#interface FastEthernet0/1
Router1(config-if)#ip address dhcp
Router1(config-if)#end
Router1#
Interface FastEthernet0/1 assigned DHCP address 172.25.1.57, mask 255.255.255.0
Router1#

Prior to Release 12.2(8)T, the ip address dhcp command was only supported on Ethernet interfaces.


Discussion

Cisco started to include DHCP client functionality in IOS Version 12.1(2)T. This allows routers to obtain interface IP address information via DHCP. While we don't recommend using dynamic addressing for routers in an internal network, this can be extremely useful for routers that connect to the Internet through an ISP. It is increasingly common for service providers to use DHCP to give address information to allocate information to client devices.

When an interface on the router is configured as a DHCP client like this, it is able to dynamically learn its IP address, and netmask, via DHCP. In addition, the router also learns its TFTP server address, NETBIOS nameserver, vendor-specific information, static routes, domain name, DNS servers, and default router information.

Beginning with IOS Version 12.3(8)T, you can control which options are requested using the ip dhcp client request command. By default, all options are requested unless you explicitly disable them:

Router1#configure terminal 
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#interface FastEthernet0/1
Router1(config-if)#no ip dhcp client request dns-nameserver
Router1(config-if)#end
Router1#

In this example, we've disabled the router from requesting DNS servers from the DHCP server. All other options will be requested as normal. The following keywords can be manually disabled using the no ip dhcp client request command: tftp-server-address, netbios-nameserver, vendor-specific, static-route, domain-name, dns-nameserver, or router.

In the following screen capture, the router has learned its default route via DHCP. Notice that the router displays this DHCP route as a static route and assigns it an administrative distance of 254. This ensures that the DHCP-learned default address is the absolute last possible route, and any other static or dynamic routes will take precedence:

Router1#show ip route
Codes: C - connected, S - static, I - IGRP, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP
i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area
* - candidate default, U - per-user static route, o - ODR
P - periodic downloaded static route

Gateway of last resort is 172.25.1.1 to network 0.0.0.0

172.25.0.0/24 is subnetted, 1 subnets
C 172.25.1.0 is directly connected, FastEthernet0/1
S* 0.0.0.0/0 [254/0] via 172.25.1.1
Router1#

In the ISP situation, the end devices will also need domain name and DNS server information. You can see this information with the show host command. This example shows a domain name and DNS server information learned via DHCP:

Router1#show host
Default domain is oreilly.com
Name/address lookup uses domain service
Name servers are 255.255.255.255, 172.25.1.1

Host Port Flags Age Type Address(es)
www.oreilly.com None (temp, OK) 0 IP 192.168.22.57
Router1#

Notice that the router dynamically learned about the domain name and name server information via DHCP. DHCP-learned information will not overwrite statically configured information. For example, if you manually configure the router with a domain name, the router will quietly ignore the one it learns through DHCP. The router will simply add any name servers that it learns through DHCP to the static list of manually configured name servers.

The show ip interface command tells you that the router learned IP address from DHCP:

Router1#show ip interface
FastEthernet0/1 is up, line protocol is up
Internet address is 172.25.1.57/24
Broadcast address is 255.255.255.255
Address determined by DHCP
MTU is 1500 bytes

Beginning with IOS Version 12.3(4)T, Cisco added the ability to release and renew DHCP leases via the privilege command prompt. To release a DHCP-obtained IP address using the release dhcp command:

Router1#release dhcp FastEthernet0/1
Router1#

To renew a DHCP lease and retain an IP address, use the renew dhcp command:

Router1#renew dhcp FastEthernet0/1
Router1#

To view the DHCP lease information, use the show dhcp lease command:

Router1#show dhcp lease          
Temp IP addr: 172.25.1.57 for peer on Interface: FastEthernet0/1
Temp sub net mask: 255.255.255.0
DHCP Lease server: 10.1.1.1, state: 3 Bound
DHCP transaction id: B69
Lease: 432000 secs, Renewal: 216000 secs, Rebind: 378000 secs
Temp default-gateway addr: 172.25.1.1
Next timer fires after: 2d11h
Retry count: 0 Client-ID: cisco-000e.8424.4e71-Fa0/1
Client-ID hex dump: 636973636F2D303030652E383432342E
346537312D4661302F31
Hostname: Router1
Router1#

Notice that the output shows the assigned IP address, net mask, DHCP server, lease duration/renewal/rebind times, the assigned default gateway, and the duration until the next lease renewal (next timer fires after).

Although controlling your router addresses from a centralized DHCP server might seem like a good idea, in general we don't recommend it. Routers are the core architecture of a network and should never rely on an external server to obtain IP addressing. Unless a DHCP server is available on every segment, the router needs a DHCP proxy, which is usually another router with a hardcoded IP address. In disaster scenarios when many routers fail simultaneously, it can be extremely difficult to bootstrap the network back into operation.

So, except for specific circumstances, like connecting to an ISP, where the router is at the edge of the network, we strongly discourage using this DHCP client functionality.