DHCP客户端配置及测试

在Linux客户机上测试DHCP服务时,可以临时使用dhclient命令,也可以修改网卡的配置文件。
临时测试:执行“dhclient -d 接口名称”,观察地址获取过程,按Ctrl+c退出。
固定配置:修改网卡配置文件,比如ifcfg-eth0,设置BOOTPROTO=dhcp,然后重启network服务即可生效。

步骤一:使用dhclient工具测试DHCP服务
1)针对eth0网卡执行dhclient调试
如果DHCP服务可用且客户机之间的网络正常,则执行dhclient -d eth0操作后可以观察到DHCP通信获取地址的四个过程:DHCP Discover、DHCP Offer、DHCP Request、DHCP ACK。

[root@pc205 ~]# dhclient -d eth0  
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/eth0/00:0c:29:65:21:3c
Sending on   LPF/eth0/00:0c:29:65:21:3c
Sending on   Socket/fallback
DHCPOFFER from 192.168.4.5
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x73b1002c)
DHCPACK from 192.168.4.5 (xid=0x73b1002c)
bound to 192.168.4.28 -- renewal in 2728 seconds.

2)退出调试模式
若要退出dhclient调试模式,可以按Ctrl+c中断任务:

[root@pc205 ~]# dhclient -d eth0  
.. ..
bound to 192.168.4.28 -- renewal in 2728 seconds.
^C  											//按Ctrl+c快捷键
[root@pc205 ~]#

步骤二:调整网卡配置的方式来验证DHCP服务
1)修改网卡配置文件,启用BOOTPROTO=dhcp
BOOTPROTO表示启动网络接口的协议或方式,如果设为none、auto,通常需要在配置中手动指定IP地址等参数;而改成dhcp的话,表示查找DHCP服务器并申请分配可用的IP地址等参数。

[root@pc205 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 
.. ..
BOOTPROTO=dhcp                            		//采用DHCP地址获取方式

2)重启network服务,并确认地址获取结果
重启network服务:

[root@pc205 ~]# service network restart   
正在关闭接口 eth0:                                        [确定]
关闭环回接口:                                             [确定]
弹出环回接口:                                             [确定]
弹出界面 eth0: 
正在决定 eth0 的 IP 信息...完成。  					  [确定]

确认已获得DHCP服务器分配的IP地址:

[root@pc205 ~]# ifconfig   eth0  
eth0      Link encap:Ethernet  HWaddr 00:0C:29:2D:B8:1D  
          inet addr:192.168.4.28  Bcast:192.168.4.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe2d:b81d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2270 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1525 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:212124 (207.1 KiB)  TX bytes:185843 (181.4 KiB)

确认已获得DHCP服务器分配的默认网关地址:

[root@pc205 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.4.1     0.0.0.0         UG    0      0        0 eth0

确认已获得DHCP服务器分配的DNS服务器地址:

[root@pc205 ~]# cat /etc/resolv.conf 
; generated by /sbin/dhclient-script
search tedu.cn
nameserver 192.168.4.5

3)查看IP租约信息
DHCP服务器端查看地址分配情况:

[root@svr5 ~]# cat /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.1.1-P1

server-duid "\000\001\000\001\034\234\325\020\000\014)-n\232";

lease 192.168.4.28 {                            		//已分配的IP地址
  starts 4 2015/03/19 00:34:01;
  ends 4 2015/03/19 02:34:01;
  cltt 4 2015/03/19 00:34:01;
  binding state active;
  next binding state free;
  hardware ethernet 00:0c:29:2d:b8:1d;  			//客户机MAC地址
}

客户端查看已获取的IP地址租约:

[root@pc205 dhclient]# cat dhclient-eth0.leases
lease {
  interface "eth0"; 
  fixed-address 192.168.4.28;  					//分配的IP地址
  option subnet-mask 255.255.255.0;
  option routers 192.168.4.1;  					//分配的默认网关地址
  option dhcp-lease-time 7200;
  option dhcp-message-type 5;
  option domain-name-servers 192.168.4.5;  //分配的DNS地址
  option dhcp-server-identifier 192.168.4.5;  		//DHCP服务器的IP地址
  option domain-name "tedu.cn";  				//分配的默认搜索域
  renew 4 2015/03/19 01:01:26;
  rebind 4 2015/03/19 01:47:31;
  expire 4 2015/03/19 02:02:31;
}

猜你喜欢

转载自blog.csdn.net/Win_Le/article/details/90346926