设置方法,这种情况通常是不多见的,我们也强烈不建议这么做,因为路由器作为网络中间设备需要有高度的可管理性以及可靠性,而动态地址是我们的路由器管理变得更加复杂和不稳定。
但是,有一种情况比较适合配置路由器为dhcp客户端,那就是路由器作为局域网(或者说内部网)的边界连接到isp的时候。
用ip address dhcp client-id 命令来配置路由器为DHCP的客户端,由此动态获取ip地址
Router1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#interface Ethernet0
Router1(config-if)#ip address dhcp client-id Ethernet0 //开启DHCP的客户端,以使得该接口动态的从DHCP服务器端获得IP地址
Router1(config-if)#end
Router1#
Interface Ethernet0 assigned DHCP address 192.168.10.1, mask 255.255.255.0
Router1#
下面的配置命令,可以配置路由器为DHCP服务器,用以给DHCP客户端动态分配ip地址
Router1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Router1(config)#service dhcp //开启 DHCP 服务
Router1(config)#ip dhcp pool 192.168.10.0/24 //定义DHCP地址池
Router1(dhcp-config)#network 192.168.10.0 255.255.255.0 // 用network 命令来定义网络地址的范围
Router1(dhcp-config)#default-router 192.168.10.1 //定义要分配的网关地址
Router1(dhcp-config)#exit
Router1(config)#ip dhcp excluded-address 192.168.10.1 192.168.10.50 //该范围内的ip地址不能分配给客户端 (作为其他的用途一些比如说网络服务器,打印机等)
Router1(config)#ip dhcp excluded-address 192.168.10.200 192.168.10.255 //该范围内的ip地址不能分配给客户端 (作为其他的用途一些比如说网络服务器,打印机等)
Router1(config)#end
Router1#
当路由器给客户端动态分配地址后,就会绑定(binding)分配的ip地址以及客户端设备的mac地址信息,保存在路由器的配置中,以便下一次相同的mac地址请求dhcp服务也能够获得同样的ip地址。下面给出的例子是,用show ip dhcp binding 命令显示的 ipbinding的信息。其中Lease expiration 表示该ip地址,客户端还能占有的时间,(当然客户端可以在期满之前再次发送dhcp请求报,事实上dhcp的规范也是有这样的规定的,即在租期还有一半时间的时候就会发出dhcp请求,