1.DHCP简介
DHCP(Dynamtic Host Configuraton Protocol,动态主机配置协议)是一个工作在应用层的局域网网络协议,数据传输时使用UDP不可靠谱传输协议工作,通常被应用在大型的局域网络环境中,主要作用是集中的管理、分配网络资源,使网络环境中的主机能动态的获得IP地址、Gatway地址、DNS服务器地址等信息,能够提升地址的使用率。
2.DHCP工作原理(租约四部曲+续租)
3.DHCP 服务搭建
4.DHCP实验部署
具体实验操作:
1.修改配置文件dhcpd.conf ,将其中前几个不用的subnet注释掉;
2.设置一个subnet要分配的网段和子网掩码,声明可用的IP地址,其余用不到的可以暂时不设置;
3.保留最小租约时间和最大租约时间;
[root@oracle dhcp]# vim dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
default-lease-time 600;
max-lease-time 7200;
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
#subnet 10.152.187.0 netmask 255.255.255.0 {
#}
# This is a very basic subnet declaration.
#subnet 10.254.239.0 netmask 255.255.255.224 {
# range 10.254.239.10 10.254.239.20;
# option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
#}
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
#subnet 10.254.239.32 netmask 255.255.255.224 {
# range dynamic-bootp 10.254.239.40 10.254.239.60;
# option broadcast-address 10.254.239.31;
# option routers rtr-239-32-1.example.org;
#}
# A slightly different configuration for an internal subnet.
subnet 192.168.197.0 netmask 255.255.255.0 {
range 192.168.197.20 192.168.197.200;
# option domain-name-servers ns1.internal.example.org;
# option domain-name "internal.example.org";
# option routers 10.5.5.1;
# option broadcast-address 10.5.5.31;
default-lease-time 600;
max-lease-time 7200;
}
4.启动服务,service dhcpd start
5.查看是否启动成功
[root@oracle dhcp]# service dhcpd start
Starting dhcpd: [ OK ]
[root@oracle dhcp]# netstat -tluna
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:55647 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 192.168.197.134:22 192.168.197.1:58794 ESTABLISHED
tcp 0 0 192.168.197.134:22 192.168.197.1:60712 ESTABLISHED
tcp 0 0 192.168.197.134:22 192.168.197.135:63025 ESTABLISHED
tcp 0 0 :::51618 :::* LISTEN
tcp 0 0 :::33060 :::* LISTEN
tcp 0 0 :::3306 :::* LISTEN
tcp 0 0 :::111 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:631 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
udp 0 0 127.0.0.1:1001 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
udp 0 0 0.0.0.0:24840 0.0.0.0:*
udp 0 0 0.0.0.0:67 0.0.0.0:*
udp 0 0 0.0.0.0:838 0.0.0.0:*
udp 0 0 :::111 :::*
udp 0 0 :::36291 :::*
udp 0 0 :::838 :::*
6.验证,将局域网内的另外一台主机B,ip获取方式改成DHCP
7.重启另一台主机B的网卡,service network restart (该命令会重启所有网卡,如果怕影响别的网卡服务,可以使用下面这条语句,只启动特定的网卡)
[root@135 ~]# ifdown eth0;ifup eth0
验证主机B,通过DHCP服务来获取IP的新ip地址(之前是静态设置的ip135,现在变成了129)
[root@135 Desktop]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:C1:E2:78
inet addr:192.168.197.129 Bcast:192.168.197.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fec1:e278/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12562 errors:0 dropped:0 overruns:0 frame:0
TX packets:12696 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1132479 (1.0 MiB) TX bytes:1389920 (1.3 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:5784 errors:0 dropped:0 overruns:0 frame:0
TX packets:5784 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:522236 (509.9 KiB) TX bytes:522236 (509.9 KiB)
8.回到搭建有DHCP服务的主机A上,查看DHCP的服务日志/var/log/messages,进一步了解,DHCP的工作原理(即租约四部曲)。
[root@oracle dhcp]# tail -10 /var/log/messages
Jun 23 06:56:49 oracle dhcpd: Wrote 0 leases to leases file.
Jun 23 06:56:49 oracle dhcpd: Listening on LPF/eth0/00:0c:29:64:b4:6d/192.168.197.0/24
Jun 23 06:56:49 oracle dhcpd: Sending on LPF/eth0/00:0c:29:64:b4:6d/192.168.197.0/24
Jun 23 06:56:49 oracle dhcpd: Sending on Socket/fallback/fallback-net
Jun 23 07:00:31 oracle dhcpd: DHCPDISCOVER from 00:0c:29:c1:e2:78 via eth0
Jun 23 07:00:31 oracle dhcpd: ns1.example.org: temporary name server failure
Jun 23 07:00:31 oracle dhcpd: ns2.example.org: temporary name server failure
Jun 23 07:00:31 oracle dhcpd: DHCPOFFER on 192.168.197.129 to 00:0c:29:c1:e2:78 (135) via eth0
Jun 23 07:00:31 oracle dhcpd: DHCPREQUEST for 192.168.197.129 (192.168.197.134) from 00:0c:29:c1:e2:78 (135) via eth0
Jun 23 07:00:31 oracle dhcpd: DHCPACK on 192.168.197.129 to 00:0c:29:c1:e2:78 (135) via eth0
获取请求:MAC地址为00:0c:29:c1:e2:78的主机发出来的
Jun 23 07:00:31 oracle dhcpd: DHCPDISCOVER from 00:0c:29:c1:e2:78 via eth0
向客户机B发送OFFER
Jun 23 07:00:31 oracle dhcpd: DHCPOFFER on 192.168.197.129 to 00:0c:29:c1:e2:78 (135) via eth0
收到来自客户机B的确认IP消息DHCPREQUEST
Jun 23 07:00:31 oracle dhcpd: DHCPREQUEST for 192.168.197.129 (192.168.197.134) from 00:0c:29:c1:e2:78 (135) via eth0
发送给客户机B的最终租约确认
Jun 23 07:00:31 oracle dhcpd: DHCPACK on 192.168.197.129 to 00:0c:29:c1:e2:78 (135) via eth0
4.2.DHCP的地址保留实验(固定地址分配)
目的:使得客户机B每次利用DHCP服务获取的IP地址是一样的,保持不变的IP
修改配置文件:/etc/dhcp/dhcpd.conf
重启DHCPD服务 :service dhcpd restart
[root@oracle dhcp]# service dhcpd restart
Shutting down dhcpd: [ OK ]
Starting dhcpd: [ OK ]
在客户机上执行 :ifdown eth0;ifup eth0 重启eth0网卡,检查IP地址
4.3超级作用域(同一局域网)
当局域网的主机台数超过一个网段的台数时,多于255了,需要在不改变为B类网段的情况下,添加主机台数,和之前的局域网主机能够通信。在生产环境中只需要利用路由器(路由器有DHCP的功能)的单臂路由的功能,在原来的基础上,添加另一个IP地址,就可以通过路由器,让不同网段的局域网主机进行通信。
下面为模拟实验环境
4.4 DHCP中继
模拟的环境就是,局域网A网段的主机A,通过DHCP服务+一台DHCP中继器,可以将局域网B网段的主机B进行连接,使得DHCP服务可以为不同网段的主机,实现分配IP的功能,依靠的就是DHCP中继服务的能力。
下图为本人粗略根据理解所画的草图,正确性不敢保证