DHCP:动态主机配置协议(Dynamic Host Configuration Protocal)。
DHCP协议用于自动管理局域网内主机的IP地址、子网掩码、网管地址及DNS地址等参数。可以有效第提高IP地址的利用率,提升配置效率。
DHCP是一种基于UDP协议且仅限于在局域网使用的网络协议,主要用于大型的局域网环境或者存在较多移动办公设备的局域网环境中。
DHCP应用场景:
DHCP用于大型局域网环境,比如100台主机,可以实现DHCP自动分配网络参数。
较多移动办公设备的局域网环境,实现自动的配置网络参数。
DHCP的工作模式为服务器/客户端。
以下实验以PC1为服务器,PC2、PC3为客户端。
1、在服务器中配置dhcpd服务程序
[root@PC1 ~]# yum install dhcp
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.2.5-27.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
dhcp x86_64 12:4.2.5-27.el7 rhel7 506 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 506 k
Installed size: 1.4 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 12:dhcp-4.2.5-27.el7.x86_64 1/1
rhel7/productid | 1.6 kB 00:00
Verifying : 12:dhcp-4.2.5-27.el7.x86_64 1/1
Installed:
dhcp.x86_64 12:4.2.5-27.el7
Complete!
2、关闭服务器vmware 虚拟机自带DHCP功能,点左上角编辑→虚拟机网络编辑器
3、将服务器ip地址设为192.168.10.1
[root@PC1 ~]# ifconfig | head -n 5
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.1 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe66:37f7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:66:37:f7 txqueuelen 1000 (Ethernet)
RX packets 445 bytes 105095 (102.6 KiB)
4、修改服务器中DHCP服务的配置文件
[root@PC1 ~]# vim /etc/dhcp/dhcpd.conf
ddns-update-style none;
ignore client-updates;
subnet 192.168.10.0 netmask 255.255.255.0 { ## 指定客户端作用域
range 192.168.10.100 192.168.10.200; ## 指定客户端IP范围
option subnet-mask 255.255.255.0; ## 指定客户端子网掩码
option routers 192.168.10.1; ## 指定客户端网管地址
option domain-name "linuxprobe.com";
option domain-name-servers 192.168.10.1; ## 指定客户端DNS地址
default-lease-time 21600;
max-lease-time 43200;
}
5、查看PC2和PC3的网络参数
[root@PC2 Desktop]# ifconfig | head -n 3
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:25:bb:3e txqueuelen 1000 (Ethernet)
RX packets 2 bytes 684 (684.0 B)
[root@PC3 Desktop]# ifconfig | head -n 3
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:7d:e7:11 txqueuelen 1000 (Ethernet)
RX packets 1 bytes 342 (342.0 B)
6、修改PC2和PC3的网卡配置文件,设置为开启启动
[root@PC2 Desktop]# cd /etc/sysconfig/network-scripts/
[root@PC2 network-scripts]# vim ifcfg-eno16777728
HWADDR=00:0C:29:25:BB:3E
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777728
UUID=a0c2aa6b-48e1-4822-a67c-151155cc14bd
ONBOOT=yes
[root@PC3 network-scripts]# vim ifcfg-eno16777728
[root@PC3 Desktop]# cd /etc/sysconfig/network-scripts/
HWADDR=00:0C:29:7D:E7:11
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777728
UUID=39fd8d2f-8276-4732-9207-1bf1942f91f9
ONBOOT=yes
7、 在服务器中重启DHCP服务,DHCP服务的服务名称为dhcpd
[root@PC1 ~]# systemctl start dhcpd
8、在PC2和PC3客户机中重启网卡、检查网络参数
[root@PC2 network-scripts]# systemctl restart network
[root@PC2 network-scripts]# ifconfig | head -n 3
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.100 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link>
[root@PC3 network-scripts]# ifconfig | head -n 3
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.102 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe7d:e711 prefixlen 64 scopeid 0x20<link>
以上实验说明已经实现了DHCP的自动分配网络参数的服务。