diff --git "a/21 \344\270\201\346\235\250\346\202\246/20250425 DHCP\345\212\250\346\200\201\351\205\215\347\275\256.md" "b/21 \344\270\201\346\235\250\346\202\246/20250425 DHCP\345\212\250\346\200\201\351\205\215\347\275\256.md" new file mode 100644 index 0000000000000000000000000000000000000000..c514315236d50d5e4f48d89a111157993039e66c --- /dev/null +++ "b/21 \344\270\201\346\235\250\346\202\246/20250425 DHCP\345\212\250\346\200\201\351\205\215\347\275\256.md" @@ -0,0 +1,123 @@ +### 20250426 DHCP动态配置 + +### DHCP的作用 + +- **自动分配IP**:避免手动配置的麻烦和冲突 +- **集中管理**:统一分配IP、子网掩码、网关、DNS等 +- **提高效率**:动态回收和重用IP,适用于移动设备多的环境 + +##### 核心语法 + +```cmd +#路由器里进入全局配置模式 +Router> enable +Router# configure terminal + +! 创建DHCP地址池 +Router(config)# ip dhcp pool LAN_POOL(名字随便起) + +! 配置网络地址和子网掩码 +Router(dhcp-config)# network 192.168.1.0 255.255.255.0 + +! 配置默认网关 +Router(dhcp-config)# default-router 192.168.1.1 + +! 配置DNS服务器 +Router(dhcp-config)# dns-server 8.8.8.8 + +! 配置租约时间(可选,默认为1天) +Router(dhcp-config)# lease 7 [非所有版本的模拟器支持] + +! 退出DHCP配置模式 +Router(dhcp-config)# exit + +! 排除不分配的IP地址范围 ,比如网关 +Router(config)# ip dhcp excluded-address 192.168.1.100 192.168.1.254 +``` + +**然后** + +```cmd +# 要选择该端口 +Router(config)#int g0/0 +# 配置端口IP和开启 +Router(config-if)#ip address 192.168.10.254 255.255.255.0 +Router(config-if)#no shutdown +``` + +### 使用DHCP服务器 + +配置DHCP服务器 + +``` +DHCp->开->名称->网关->DNS服务器->起始IP->子网掩码->数量->添加 +``` + +### DHCP中继模式 + +通过路由的DHCP中继模式,可以跨网连接DHCP服务器,为不同的局域网提供自动分配 + +```cmd +# 配置DHCP中继(当DHCP服务器不在本地网络时) +Router(config)# interface gigabitethernet0/0 +Router(config-if)# ip helper-address 192.168.3.2 //转发给真正的DHCP服务 +``` + +# 练习 + +```cmd +配置路由器 +Switch(config)#int f0/3 +Switch(config-if)#switchport mode trunk + +配置DHCP服务器 +Router(config)#int g0/0/0 +Router(config-if)#ip address 192.168.10.254 255.255.255.0 +Router(config-if)#no shutdown +Router(config)#ip dhcp pool ip10 //创建DHCP地址池 +Router(dhcp-config)#network 192.168.10.0 255.255.255.0 //配置地址和掩码 +Router(dhcp-config)#default-router 192.168.10.254 //配置默认网关 +Router(dhcp-config)#dns-server 2.2.2.2 //配置DNS服务器 +``` + + + +```cmd +左边路由 +int f0/3 +Switch(config-if)#switchport mode trunk +右边路由 +int f0/3 +Switch(config-if)#switchport mode trunk + +配路由器(两边的端口配好网关IP) +Router(config)#int f0/0 +Router(config-if)#ip address 192.168.10.254 255.255.255.0 +Router(config-if)#no shutdown + +Router(config)#int f1/0 +Router(config-if)#ip address 192.168.20.254 255.255.255.0 +Router(config-if)#no shutdown + +把这个路由器当做中继路由 +Router(config)#int f0/0 +Router(config-if)#ip helper-address 172.16.100.100 //指向 DHCP 服务器 + +Router(config)#int f1/0 +Router(config-if)#ip helper-address 172.16.100.100 //指向 DHCP 服务器 + +连接 172.16.100.0/24(DHCP 服务器所在网络) +Router(config)#int f0/1 +Router(config-if)#ip address 172.16.100.254 255.255.255.0 +Router(config-if)#no shutdown + +然后配置DHCP服务器 +# 把192.168.10.254和192.168.20.254配置进去 + +最后别忘了给服务器配置IP地址、子网掩码、网关 +172.16.100.100 +255.255.0.0 +172.16.100.254 + +然后就能自动分配IP,大功告成! +``` \ No newline at end of file