1.关闭防火墙和SELinux
[root@ser5 /]# service iptables stop
[root@ser5 /]# vim /etc/selinux/config
SELINUX=permissive(将 enforcing改为permissive)保存退出
2.准备两台虚拟机,一台作为服务器,一台作为客户机,配置两个主机的IP地址,使服务器和客户机能够通信
服务器:
[root@ser5 /]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0c:29:a9:a0:68
TYPE=Ethernet
UUID=65d53814-182d-44ec-a0df-a0712c196aeb
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.4.5
NETMASK=255.255.255.0
IPV6INIT=no
USERCTL=no
客户机:
[root@pc205 /]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0c:29:fe:4b:36
TYPE=Ethernet
UUID=65d53814-182d-44ec-a0df-a0712c196aeb
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.4.205
NETMASK=255.255.255.0
IPV6INIT=no
USERCTL=no
3.在服务器上安装dhcp服务
[root@ser5 /]# yum -y install dhcp
[root@ser5 /]# rpm -q dhcp
dhcp-4.1.1-38.P1.el6.x86_64
确保dhcp服务成功安装
4.修改服务上dhcp的配置文件
[root@ser5 /]# vim /etc/dhcp/dhcpd.conf
default-lease-time 600; #默认租约时间600s
max-lease-time 7200; #最大租约时间7200s
subnet 192.168.4.0 netmask 255.255.255.0{ #设置IP地址的作用域是192.168.4.0网段
option routers 192.168.4.1; #设置分配的网关
option domain-name "tarena.com"; #设置默认搜索域
option domain-name-servers 202.106.0.20,8.8.8.8; #设置DNS地址
range 192.168.4.28 192.168.4.54; #分配的IP地址范围
range 192.168.4.128 192.168.4.200;
}
host fantasia { #保留地址,给特定的主机分配IP地址
hardware ethernet 00:0C:29:FE:4B:36;
fixed-address 192.168.4.110;
}
注:在修改配置的文件的时候可以打开/usr/share/doc/dhcp*/dhcpd.conf.sample参考配置格式,同时也可以通过man dhcpd.conf查看配置文件的帮助手册
5.重启服务并设置开机自启动
[root@ser5 /]# service dhcpd restart
[root@ser5 /]# chkconfig dhcpd on
注:如果服务启动失败,一定是配置文件出错,可以去检查文档,或者去通过查看命令 less /var/log/messages 查看系统日志找出错误所在
Aug 12 19:25:18 ser5 dhcpd: /etc/dhcp/dhcpd.conf line 13: semicolon expected.
Aug 12 19:25:18 ser5 dhcpd: range
Aug 12 19:25:18 ser5 dhcpd: ^
Aug 12 19:25:18 ser5 dhcpd: Configuration file errors encountered -- exiting
Aug 12 19:25:18 ser5 dhcpd:
Aug 12 19:25:18 ser5 dhcpd: This version of ISC DHCP is based on the release available
Aug 12 19:25:18 ser5 dhcpd: on ftp.isc.org. Features have been added and other changes
可知道错误在配置文件的第13行左右
更改配置文件后重新启动服务
6.通过客户机验证
[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:fe:4b:36
Sending on LPF/eth0/00:0c:29:fe:4b:36
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 7 (xid=0x5bef1d5e)
DHCPOFFER from 192.168.4.5
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x5bef1d5e)
DHCPACK from 192.168.4.5 (xid=0x5bef1d5e)
bound to 192.168.4.110 -- renewal in 289 seconds.
通过该命令动态验证客户机获取IP地址的全过程,通过DHCP获得的地址是192.168.4.110
7.查看租约信息
服务器:
[root@ser5 /]# 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
lease 192.168.4.28 {
starts 2 2014/08/12 06:50:35;
ends 2 2014/08/12 07:00:35;
tstp 2 2014/08/12 07:00:35;
cltt 2 2014/08/12 06:50:35;
binding state free;
hardware ethernet 00:0c:29:fe:4b:36;
}
server-duid "\000\001\000\001\033|u\227\000\014)\251\240h";
可以查看到该服务器出租出去的IP地址信息
客户机:
[root@pc205 /]# cat /var/lib/dhclient/dhclient-ethN.leases
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:fe:4b:36
Sending on LPF/eth0/00:0c:29:fe:4b:36
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 7 (xid=0x5bef1d5e)
DHCPOFFER from 192.168.4.5
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x5bef1d5e)
DHCPACK from 192.168.4.5 (xid=0x5bef1d5e)
bound to 192.168.4.110 -- renewal in 289 seconds.
^C
查看已获取的IP租约