步骤一:构建DHCP服务器
1)使用yum安装dhcp软件包
[root@svr5 ~]# yum -y install dhcp
[root@svr5 ~]# rpm -q dhcp
dhcp-4.1.1-38.P1.el6.x86_64
2)修改DHCP服务配置
直接编辑/etc/dhcp/dhcpd.conf配置文件,设置内容参考下列操作:
[root@svr5 ~]# vim /etc/dhcp/dhcpd.conf
default-lease-time 7200; //默认租期时间
max-lease-time 14400; //最大租期时间
subnet 192.168.4.0 netmask 255.255.255.0 { //定义分配地址段
option routers 192.168.4.1; //为客户机指定网关地址
option domain-name-servers 192.168.4.5; //为客户机设置DNS地址
range 192.168.4.28 192.168.4.54; //分配地址列表
range 192.168.4.128 192.168.4.200; //分配地址列表
}
3)启动dhcpd服务,并且设置为开机自动运行
[root@svr5 ~]# service dhcpd restart
正在启动 dhcpd: [确定]
[root@svr5 ~]# chkconfig dhcpd on
确认已监听UDP 67端口:
[root@svr5 ~]# netstat -anptu|grep dhcpd
udp 0 0 0.0.0.0:67 0.0.0.0:* 34788/dhcpd
DHCP客户端配置及测试
步骤一:使用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;
}