服务端:sishen_63 IP:192.168.1.63
客户端:sishen_64 IP:192.168.1.64
此外,因为本实验实在虚拟机中做的,所以对虚拟机还要做如下设置:
服务端和客户端均选择Vmnet2,这样做可以排除局域网中的DHCP服务器对本实验造成的影响
YUM一键安装,有兴趣的朋友可以RPM或者源码安装
[root@sishen_63~15:17# 23]#yum install -y dhcp #前提是配置好本地YUM源或网络YUM源,本实验采用本地YUM源
安装完成DHCP后,会在/usr/share/doc/dhcp-4.1.1/目录下生成一个模板文件
[root@sishen_63~15:17# 23]#ls /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
将该模板文件拷贝到/etc/dhcp目录下,替换掉空的dhcpd.conf文件。
[root@sishen_63~15:19# 24]#cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
cp: overwrite `/etc/dhcp/dhcpd.conf'? y
[root@sishen_63~15:21# 25]#ll !$
ll /etc/dhcp/dhcpd.conf
-rw-r--r-- 1 root root 3262 Sep 12 15:21 /etc/dhcp/dhcpd.conf
接下来启动服务并查看端口
[root@sishen_63~15:21# 26]#service dhcpd start
Starting dhcpd: [FAILED]
咦!失败了,这是因为默认的配置文件dhcpd.conf中都是10.17.224.0网段的IP地址,而我们的实验环境是192.168.1.63所以接下来还要修改配置文件。
[root@sishen_63~15:28# 28]#vim /etc/dhcp/dhcpd.conf #大约在47行左右,修改为如下内容
# A slightly different configuration for an internal subnet.
subnet 192.168.0.0 netmask 255.255.255.0 { (1)
range 192.168.0.100 192.168.0.200; (2)
option domain-name-servers 192.168.0.1; (3)
option domain-name "sishen_63"; (4)
option routers 192.168.0.1; (5)
option broadcast-address 192.168.0.255; (6)
default-lease-time 600; (7)
max-lease-time 7200; (8)
}
说明:
(1) 声明子网;子网掩码
(2)IP地址起始范围,即可提供的IP地址范围
(3)为客户端指明DNS服务器的IP地址
(4)为客户端指定DNS服务器主机名
(5)为客户端指定网关
(6)为客户端指定广播地址
(7)默认租约时间,以秒为单位
(8)最大租约时间
服务端启动dhcpd服务
[root@sishen_63 ~]# service dhcpd start
Starting dhcpd: [ OK ]
客户端确保ifcfg-eth0配置文件中的BOOTPROTO=dhcp,之后重启网卡服务
[root@sishen_64 ~]# ifconfig | grep 'inet'
inet addr:192.168.0.200 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe11:5a8c/64 Scope:Link
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
[root@sishen_64 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
查看DNS服务器
[root@sishen_64 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
domain sishen_63
search sishen_63
nameserver 192.168.0.1
查看租约数据库文件
[root@sishen_63 ~]# 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 " 00 01 00 01!JN236 00 14)302(1";
lease 192.168.0.200 {
starts 2 2017/09/12 07:52:35;
ends 2 2017/09/12 08:02:35;
cltt 2 2017/09/12 07:52:35;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:11:5a:8c;
client-hostname "sishen_64";
}
lease 192.168.0.200 {
starts 2 2017/09/12 07:54:53;
ends 2 2017/09/12 08:04:53;
cltt 2 2017/09/12 07:54:53;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:11:5a:8c;
client-hostname "sishen_64";
}
OK,大功告成!!!
拓展练习 IP地址绑定
在DHCP 中的IP 地址绑定用于给客户端分配固定IP 地址。比如服务器需要使用固定IP 地址就可以使用IP 地址绑定,通过MAC 地址与IP 地址的对应关系为指定的物理地址计算机分配固定IP地址。整个配置过程需要用到 host 声明和hardware、fixed-address 参数。
(1)host 主机名 {......}
作用:用于定义保留地址
(2)hardware 类型 硬件地址
作用:定义网络接口类型和硬件地址。常用类型为以太网(ethernet),地址为MAC 地址。
(3)fixed-address IP 地址
作用:定义DHCP 客户端指定的IP 地址。
只需将配置文件稍作修改即可,在subnet内加一段如下红色字体内容:
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.100 192.168.0.200;
option domain-name-servers 192.168.0.1;
option domain-name "sishen_63";
option routers 192.168.0.1;
option broadcast-address 192.168.0.255;
default-lease-time 600;
max-lease-time 7200;
host sishen_1{
hardware ethernet 00:0C:29:C2:28:31;
fixed-address 192.168.0.254;
}
}
注意:必须在subnet内
服务端重启dhcpd服务
[root@sishen_63 ~]# service dhcpd restart
Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
客户端重启网卡服务
[root@sishen_64 ~]# ifconfig | grep 'inet'
inet addr:192.168.0.200 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe11:5a8c/64 Scope:Link
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host