学习日记——(路由与交换技术)DHCP(动态主机配置协议)

一、相关知识

 

DHCP(动态主机配置协议)对客户机动态分配TCP/IP信息

优点:

  • 减少管理员的工作量
  • 减少输入错误的可能性
  • 避免IP冲突
  • 当网络变更网络段时,不需要重新配置每台计算机的IP
  • 计算机移动不需要重新配置IP
  • 提高了IP地址的利用率

工作过程

注:当有多台服务器时,客户机将优先选用第一个响应它的服务器

  •  当DHCP客户机租期达50%时:重新更新租约,客户机发送DHCPRequest包
  • 当DHCP客户机租期达87.5%时:进入重新申请状态,客户机发送DHCPDiscover包
  • 使用ipconfig/renew 命令向DHCP服务器发送DHCPRequest包。如果DHCP服务器没有响应,客户机将继续使用当前的配置。
  • 使用ipconfig/release命令:客户机的TCP/IP联络停止(IP 0.0.0.0 子网掩码:0.0.0.0),服务器将释放的IP分配给其他客户机使用。

DHCP是基于Client/Server工作模式,DHCP服务器为需要动态配置的主机分配IP地址和主机配置参数。

DHCP有三种机制分配IP地址:

  1. 自动分配:DHCP给客户端分配永久性的IP地址
  2. 动态分配:DHCP给客户机分配过一段时间就会过期的IP地址(或客户端可以自动释放IP地址) 默认(缺省)的租约期限是1天。
  3. 手工配置:由网络管理员配置指定的IP地址

三种地址分配方式中,只有动态分配可以重复使用客户端不需要的地址

二、相关例子

1、路由作DHCP服务器

步骤

1、打开DHCP服务

2、将路由器的接口配置地址并打开

3、配置DHCP协议

拓扑结构:

代码:

Router>en

Router#conf t

Enter configuration commands, one per line. End with CNTL/Z.

Router(config)#host R1

R1(config)#se

R1(config)#ser

R1(config)#service dhc

R1(config)#service dhcp  开启DHCP服务

R1(config)#INT

R1(config)#INTerface g

配置路由器

R1(config)#INTerface gigabitEthernet 0/0

R1(config-if)#ip add

R1(config-if)#ip address 192.168.1.254 255.255.255.0

R1(config-if)#no sh

R1(config-if)#no shutdown

R1(config-if)#

%LINK-5-CHANGED: Interface GigabitEthernet0/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet0/0, changed state to up

R1(config-if)#e

R1(config-if)#exit

R1(config)#ip dh

R1(config)#ip dhcp po

R1(config)#ip dhcp pool SYJ   建一个DHCP 地址池 (SYJ是名字)

R1(dhcp-config)#net

R1(dhcp-config)#network 192.168.1.0 255.255.255.0 给这个地址池划分范围

R1(dhcp-config)#de

R1(dhcp-config)#default-router 192.168.1.254  设置网关地址

R1(dhcp-config)#dns

R1(dhcp-config)#dns-server 192.168.1.1 DNS域名地址

R1(dhcp-config)#e

R1(dhcp-config)#exit

R1(config)#ip dh

R1(config)#ip dhcp ex

把一些不能用的IP地址排除

R1(config)#ip dhcp excluded-address 192.168.1.1 

R1(config)#ip dhcp excluded-address 192.168.1.2 192.168.1.10

R1(config)#ip dhcp excluded-address 192.168.1.254

R1(config)#ex

R1(config)#exit

R1#

%SYS-5-CONFIG_I: Configured from console by console

R1#

 结果:PC0用DHCP协议自动获取IP地址


2、二层交换机作DHCP服务器

拓扑结构:


相应代码:

Switch>en

Switch#conf t

Enter configuration commands, one per line. End with CNTL/Z.

Switch(config)#host

Switch(config)#hostname S1

划分VLAN

S1(config)#vlan 10

S1(config-vlan)#e

S1(config-vlan)#exit

S1(config)#vlan 20

S1(config-vlan)#e

S1(config-vlan)#exit

S1(config)#in

将4个接口划分到相应的VLAN

S1(config)#interface f

S1(config)#interface fastEthernet 0/1

S1(config-if)#sw

S1(config-if)#switchport mo

S1(config-if)#switchport mode a

S1(config-if)#switchport mode access

S1(config-if)#sw

S1(config-if)#switchport a

S1(config-if)#switchport access vlan 10

S1(config-if)#e

S1(config-if)#exit

S1(config)#int

S1(config)#interface f

S1(config)#interface fastEthernet 0/2

S1(config-if)#sw

S1(config-if)#switchport mo

S1(config-if)#switchport mode ac

S1(config-if)#switchport mode access

S1(config-if)#sw

S1(config-if)#switchport ac

S1(config-if)#switchport access vlan 10

S1(config-if)#e

S1(config-if)#exit

S1(config)#int

S1(config)#interface fastEthernet 0/3

S1(config-if)#switchport mode access

S1(config-if)#switchport access vlan 20

S1(config-if)#e

S1(config-if)#exit

S1(config)#interface fastEthernet 0/4

S1(config-if)#switchport mode access

S1(config-if)#switchport access vlan 20

S1(config-if)#e

S1(config-if)#exit

VLAN 表

S1(config)#do show vlan brief

VLAN Name Status Ports

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

1 default active Fa0/5, Fa0/6, Fa0/7, Fa0/8

Fa0/9, Fa0/10, Fa0/11, Fa0/12

Fa0/13, Fa0/14, Fa0/15, Fa0/16

Fa0/17, Fa0/18, Fa0/19, Fa0/20

Fa0/21, Fa0/22, Fa0/23, Fa0/24

Gig0/1, Gig0/2

10 VLAN0010 active Fa0/1, Fa0/2

20 VLAN0020 active Fa0/3, Fa0/4

1002 fddi-default active

1003 token-ring-default active

1004 fddinet-default active

1005 trnet-default active

S1(config)#int

打开VLAN ,配置相应的地址

S1(config)#interface vlan 10

S1(config-if)#

%LINK-5-CHANGED: Interface Vlan10, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan10, changed state to up

S1(config-if)#ip add

S1(config-if)#ip address 192.168.10.1 255.255.255.0

S1(config-if)#no sh

S1(config-if)#no shutdown

S1(config-if)#e

S1(config-if)#exit

S1(config)#interface vlan 20

S1(config-if)#

%LINK-5-CHANGED: Interface Vlan20, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface Vlan20, changed state to up

S1(config-if)#ip address 192.168.20.1 255.255.255.0

S1(config-if)#no sh

S1(config-if)#no shutdown

S1(config-if)#e

S1(config-if)#exit

S1(config)#ser

S1(config)#service DH

S1(config)#service DHcp  打开DHCP服务

S1(config)#ip dh

S1(config)#ip dhcp poo

S1(config)#ip dhcp pool vlan10 创建DHCP地址池

S1(dhcp-config)#net

S1(dhcp-config)#network 192.168.10.0 255.255.255.0  给地址池划分一个网段

S1(dhcp-config)#de

S1(dhcp-config)#default-router 192.168.10.1 配置网关

S1(dhcp-config)#dn

S1(dhcp-config)#dns-server 1.1.1.1 域名服务

S1(dhcp-config)#ex

S1(dhcp-config)#exit

S1(config)#ip

S1(config)#ip dh

S1(config)#ip dhcp po

S1(config)#ip dhcp pool vlan20

S1(dhcp-config)#net

S1(dhcp-config)#network 192.168.20.0 255.255.255.0

S1(dhcp-config)#de

S1(dhcp-config)#default-router 192.168.20.1

S1(dhcp-config)#dn

S1(dhcp-config)#dns-server 2.2.2.2

S1(dhcp-config)#ex

S1(dhcp-config)#exit

S1(config)#ip ah

S1(config)#ip dh

S1(config)#ip dhcp ex

排除掉不用的IP地址

S1(config)#ip dhcp excluded-address 192.168.10.1

S1(config)#ip dhcp excluded-address 192.168.10.2 192.168.10.100

S1(config)#ip dhcp excluded-address 192.168.20.1

S1(config)#ip dhcp excluded-address 192.168.20.2 192.168.20.200

S1(config)#ex

S1(config)#exit

S1#

%SYS-5-CONFIG_I: Configured from console by console


结果:


 DHCP中继

拓扑结构:

相关代码

R1:

Router>en

Router#conf t

Enter configuration commands, one per line. End with CNTL/Z.

Router(config)#host

Router(config)#hostname R1

R1(config)#int g

配置接口IP地址

R1(config)#int gigabitEthernet 0/0

R1(config-if)#ip add

R1(config-if)#ip address 172.16.1.1 255.255.255.0

R1(config-if)#no sh

R1(config-if)#no shutdown

R1(config-if)#

%LINK-5-CHANGED: Interface GigabitEthernet0/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet0/0, changed state to up

R1(config-if)#e

R1(config-if)#exit

R1(config)#int gigabitEthernet 0/1

R1(config-if)#ip address 172.16.12.1 255.255.255.0

R1(config-if)#no shutdown

R1(config-if)#

%LINK-5-CHANGED: Interface GigabitEthernet0/1, changed state to up

R1(config-if)#e

R1(config-if)#exit

R1(config)#

%LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet0/1, changed state to up

R1(config)#route

配置RIP路由

R1(config)#router rip

R1(config-router)#net

R1(config-router)#network 172.16.1.0

R1(config-router)#network 172.16.12.0

R1(config-router)#ver

R1(config-router)#version 2

R1(config-router)#end

R1#

% Unknown command or computer name, or unable to find computer address

R1#show ip ro

配置后的路由表

R1#show ip route

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2

E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area

* - candidate default, U - per-user static route, o - ODR

P - periodic downloaded static route

Gateway of last resort is not set

172.16.0.0/16 is variably subnetted, 5 subnets, 2 masks

C 172.16.1.0/24 is directly connected, GigabitEthernet0/0

L 172.16.1.1/32 is directly connected, GigabitEthernet0/0

R 172.16.2.0/24 [120/1] via 172.16.12.2, 00:00:07, GigabitEthernet0/1

C 172.16.12.0/24 is directly connected, GigabitEthernet0/1

L 172.16.12.1/32 is directly connected, GigabitEthernet0/1

R1#conf t

Enter configuration commands, one per line. End with CNTL/Z.

R1(config)#ser

配置DHCP

R1(config)#service dhcp

R1(config)#ip dhc

R1(config)#ip dhcp pool SYJ

R1(dhcp-config)#net

R1(dhcp-config)#network 172.16.1.0 255.255.255.0

R1(dhcp-config)#de

R1(dhcp-config)#default-router 172.16.1.1

R1(dhcp-config)#dns

R1(dhcp-config)#dns-server 1.1.1.1

R1(dhcp-config)#e

R1(dhcp-config)#exit

R1(config)#%DHCPD-4-PING_CONFLICT: DHCP address conflict: server pinged 172.16.1.1.

R1(config)#ip dhc

R1(config)#ip dhcp poo

R1(config)#ip dhcp pool JTN

R1(dhcp-config)#net

R1(dhcp-config)#network 172.16.2.0 255.255.255.0

R1(dhcp-config)#de

R1(dhcp-config)#default-router 172.16.2.1

R1(dhcp-config)#dns

R1(dhcp-config)#dns-server 2.2.2.2

R1(dhcp-config)#e

R1(dhcp-config)#exit

R1(config)#ip dh

R1(config)#ip dhcp ec

R1(config)#ip dhcp ex

R1(config)#ip dhcp excluded-address 172.16.1.1

R1(config)#ip dhcp excluded-address 172.16.2.1

R1(config)#


R2:

Router>en

Router#conf t

Enter configuration commands, one per line. End with CNTL/Z.

Router(config)#host

Router(config)#hostname R2

R2(config)#int g

配置接口的IP地址

R2(config)#int gigabitEthernet 0/0

R2(config-if)#ip address 172.16.2.1 255.255.255.0

R2(config-if)#no sh

R2(config-if)#no shutdown

R2(config-if)#

%LINK-5-CHANGED: Interface GigabitEthernet0/0, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet0/0, changed state to up

R2(config-if)#e

R2(config-if)#exit

R2(config)#int gigabitEthernet 0/1

R2(config-if)#ip address 172.16.12.2 255.255.255.0

R2(config-if)#no s

R2(config-if)#no sh

R2(config-if)#no shutdown

R2(config-if)#

%LINK-5-CHANGED: Interface GigabitEthernet0/1, changed state to up

%LINEPROTO-5-UPDOWN: Line protocol on Interface GigabitEthernet0/1, changed state to up

R2(config-if)#e

R2(config-if)#exit

配置RIP协议

R2(config)#route r

R2(config)#route rip

R2(config-router)#net

R2(config-router)#network 172.16.2.0

R2(config-router)#network 172.16.12.0

R2(config-router)#ver

R2(config-router)#version 2

R2(config-router)#end

R2#

%SYS-5-CONFIG_I: Configured from console by console

R2#show ip rou

路由表

R2#show ip route

Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP

D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area

N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2

E1 - OSPF external type 1, E2 - OSPF external type 2, E - EGP

i - IS-IS, L1 - IS-IS level-1, L2 - IS-IS level-2, ia - IS-IS inter area

* - candidate default, U - per-user static route, o - ODR

P - periodic downloaded static route

Gateway of last resort is not set

172.16.0.0/16 is variably subnetted, 5 subnets, 2 masks

R 172.16.1.0/24 [120/1] via 172.16.12.1, 00:00:01, GigabitEthernet0/1

C 172.16.2.0/24 is directly connected, GigabitEthernet0/0

L 172.16.2.1/32 is directly connected, GigabitEthernet0/0

C 172.16.12.0/24 is directly connected, GigabitEthernet0/1

L 172.16.12.2/32 is directly connected, GigabitEthernet0/1

R2#conf t

Enter configuration commands, one per line. End with CNTL/Z.

R2(config)#ser

R2(config)#service dhc

R2(config)#service dhcp 一定要开启DHCP服务

R2(config)#int

R2(config)#interface g

R2(config)#interface gigabitEthernet 0/0 进入接口

R2(config-if)#ip help

R2(config-if)#ip helper-address 172.16.12.1 配置DHCP中继

R2(config-if)#e

R2(config-if)#exit

R2(config)#


结果:

 

PC1ping PC4


这篇文章就写到这里啦!

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值