DHCP客户端Linux配置及测试

2019-06-03 20:44:16 6670

在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.landui.com/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.

1

2

3

4

5

6

7

8

9

10

11

12

13

2)退出调试模式

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


[root@pc205 ~]# dhclient -d eth0  

.. ..

bound to 192.168.4.28 -- renewal in 2728 seconds.

^C  //按Ctrl+c快捷键

[root@pc205 ~]#

1

2

3

4

5

步骤二:调整网卡配置的方式来验证DHCP服务

1)修改网卡配置文件,启用BOOTPROTO=dhcp

BOOTPROTO表示启动网络接口的协议或方式,如果设为none、auto,通常需要在配置中手动指定IP地址等参数;而改成dhcp的话,表示查找DHCP服务器并申请分配可用的IP地址等参数。


[root@pc205 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

.. ..

BOOTPROTO=dhcp                            //采用DHCP地址获取方式

1

2

3

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

重启network服务:


[root@pc205 ~]# service network restart   

正在关闭接口 eth0:                                        [确定]

关闭环回接口:                                             [确定]

弹出环回接口:                                             [确定]

弹出界面 eth0: 

正在决定 eth0 的 IP 信息...完成。    [确定]

1

2

3

4

5

6

确认已获得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)

1

2

3

4

5

6

7

8

9

确认已获得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

1

2

3

4

5

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


[root@pc205 ~]# cat /etc/resolv.conf 

; generated by /sbin/dhclient-script

search tedu.cn

nameserver 192.168.4.5

1

2

3

4

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地址

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

客户端查看已获取的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;

}

--------------------- 

作者:吾昂王 

来源:CSDN 

原文:https://www.landui.com/Win_Le/article/details/90346926 

版权声明:本文为博主原创文章,转载请附上博文链接!


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: