时间:2022-10-11 12:11:29 | 栏目:Linux | 点击:次
1.手动设置工作量大且容易冲突
2.用DHCP可以减少工作量和避免地址冲突
1.为相同网段的电脑分配IP地址―DHCP
2.跨网段为另一网段的电脑分配IP地址―DHCP中继
1.减少管理员的工作量
2.避免输入错误的可能
3.避免IP地址冲突
4.当更改IP地址段时,不需要重新配置每个用户的IP地址
5.提高了IP地址的利用率
6.方便客户端的配置
以找工作为例:
DHCP Discover――客户端用来寻找DHCP服务器的报文
我向公司投简历来找工作
DHCP Offer――DHCP服务器用来响应DHCP Discover报文的报文。该报文携带了各种配置信息,例如IP地址,DNS等
公司收到我的求职简历后给我发了一份offer
DHCP Request――客户端请求配置确认,或者续借租期
我收到offer之后跟公司确认
DHCP ACK――服务器对Request报文的确认响应
公司说我ok没问题,可以过来上班 ,我找到工作了
①自动分配:
当DHCP客户机第一次成功地从DHCP服务器获取到一个IP地址后,就永久的使用这个IP地址
②手动分配:
由DHCP服务器管理员专门指定IP地址
③动态分配:
当DHCP客户机第一次从DHCP服务器获得IP地址后,并非永久的使用该地址,而是在每次使用完后,DHCP客户机就会释放这个IP地址,供其他客户机使用
主配置文件:/etc/dhcpd.conf
执行程序:/usr/sbin/dhcpd、usr/sbin/dhcrelay
ddns-update-style interim; #全局配置参数 ...... subnet 192.168.0.0 netmask 255.255.255.0 { #subnet以上的都是全局配置参数;网段声明(网段、子网掩码、默认网关地址) option routers 192.168.0.1; #配置选项 ...... default-lease-time 21600; #配置参数 host ns { ...... fixed-address 207.175.42.254; #主机声明(固定一个IP给主机用的) } }
range参数:设置用于分配的IP地址池
option subnet-mask参数:设置客户机的子网掩码
option routers参数:设置客户机的默认网关地址
实验拓扑图如下
注:配置完“Cloud”之后才能用串口线连接
<Huawei>undo terminal monitor <Huawei>system-view [L2-SW1]user-interface console 0 [L2-SW1-ui-console0]idle-timeout 0 0 [L2-SW1-ui-console0]quit [L2-SW1]vlan batch 10 20 100 [L2-SW1]interface Ethernet 0/0/1 [L2-SW1-Ethernet0/0/1]port link-type access [L2-SW1-Ethernet0/0/1]port default vlan 10 [L2-SW1-Ethernet0/0/1]quit [L2-SW1]interface Ethernet 0/0/2 [L2-SW1-Ethernet0/0/2]port link-type access [L2-SW1-Ethernet0/0/2]port default vlan 20 [L2-SW1-Ethernet0/0/2]quit [L2-SW1]interface Ethernet 0/0/3 [L2-SW1-Ethernet0/0/3]port link-type access [L2-SW1-Ethernet0/0/3]port default vlan 100 [L2-SW1-Ethernet0/0/3]quit [L2-SW1]interface Ethernet 0/0/4 [L2-SW1-Ethernet0/0/4]port link-type access [L2-SW1-Ethernet0/0/4]port default vlan 100 [L2-SW1-Ethernet0/0/4]quit [L2-SW1]interface GigabitEthernet0/0/1 [L2-SW1-GigabitEthernet0/0/1]port link-type trunk [L2-SW1-GigabitEthernet0/0/1]port trunk allow-pass vlan 10 20 100 [L2-SW1-GigabitEthernet0/0/1]quit
<Huawei>undo terminal monitor <Huawei>system-view [Huawei]sysname L3-SW2 [L3-SW2]user-interface console 0 [L3-SW2-ui-console0]idle-timeout 0 0 [L3-SW2-ui-console0]quit [L3-SW2]vlan batch 10 20 100 [L3-SW2]interface GigabitEthernet0/0/1 [L3-SW2-GigabitEthernet0/0/1]port link-type trunk [L3-SW2-GigabitEthernet0/0/1]port trunk allow-pass vlan 10 20 100 [L3-SW2-GigabitEthernet0/0/1]quit [L3-SW2]interface Vlanif 10 [L3-SW2-Vlanif10]ip address 192.168.10.1 255.255.255.0 [SW2-Vlanif20]un sh [L3-SW2]interface Vlanif 20 [L3-SW2-Vlanif20]ip address 192.168.20.1 255.255.255.0 [SW2-Vlanif20]un sh [L3-SW2]interface Vlanif 100 [L3-SW2-Vlanif100]ip address 192.168.100.1 255.255.255.0 [SW2-Vlanif20]un sh [SW2-Vlanif100]int g0/0/1 [SW2-GigabitEthernet0/0/1]port link-type trunk [SW2-GigabitEthernet0/0/1]port trunk allow-pass vlan all
[L3-SW2]dhcp enable ##开启DHCP功能 [L3-SW2]interface Vlanif 10 ##配置虚接口ip,选择dhcp中继模式,请求指向dhcp服务器ip [L3-SW2-Vlanif10]dhcp select relay [L3-SW2-Vlanif10]dhcp relay server-ip 192.168.100.100 [L3-SW2-Vlanif10]quit [L3-SW2]interface Vlanif 20 [L3-SW2-Vlanif20]dhcp select relay [L3-SW2-Vlanif20]dhcp relay server-ip 192.168.100.100 [L3-SW2-Vlanif20]quit [L3-SW2]interface Vlanif 100 [L3-SW2-Vlanif100]dhcp select relay [L3-SW2-Vlanif100]dhcp relay server-ip 192.168.100.100 [L3-SW2-Vlanif100]quit
[root@xjj ~]# yum -y install dhcp 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile 软件包 12:dhcp-4.2.5-58.el7.centos.x86_64 已安装并且是最新版本 无须任何处理 [root@xjj ~]# cd /etc/dhcp/ ##dhcp的配置文件是/etc/dhcp/dhcpd.conf [root@xjj dhcp]# ls dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts [root@xjj dhcp]# less dhcpd.conf ##查看一下内容 [root@xjj ~]# cd /usr/share/doc/dhcp-4.2.5/ 到该目录下去寻找dhcp配置模板 [root@xjj dhcp-4.2.5]# ls ##“dhcpd.conf.example” dhcpd6.conf.example dhcpd.conf.example ldap [root@xjj dhcp-4.2.5]# cp dhcpd.conf.example /etc/dhcp/dhcpd.conf ##将模板覆盖到/etc/dhcp/dhcpd.conf中 cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y [root@xjj ~]# cd /etc/dhcp/ ##回到/etc/dhcp目录下 [root@xjj dhcp]# ls dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts [root@xjj dhcp]# less dhcpd.conf ##查看配置文件并进行下一步编辑
编辑dhcp配置文件
[root@xjj dhcp]# systemctl stop firewalld se[root@jj dhcp]# setenforce 0 ##记得重启一下网卡! [root@xjj dhcp]# systemctl restart dhcpd [root@xjj dhcp]# ping 192.168.100.1 ...略 [root@xjj dhcp]# ping 192.168.10.1 ...略 [root@xjj dhcp]# ping 192.168.20.1 ...略
pc1和pc2成功!!!