linux系统中部署DHCP服务并验证
DHCP(Dynamic Host Configuration Protocal)工作模式为服务器/客户端;
DHCP主要用于自动为客户端分配网卡信息,比如自动分配IP、网关等。
准备三台虚拟机,主机名分别为PC1、PC2和PC3,均为RHEL7,PC1作为服务器,PC2和PC3作为客户端。
1、查看服务器PC1系统版本,ip信息
[root@PC1 ~]# cat /etc/redhat-release ## 查看系统版本
Red Hat Enterprise Linux Server release 7.0 (Maipo)
[root@PC1 ~]# ifconfig | head -n 3 ## 查看ip地址
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.255 broadcast 192.168.10.10
inet6 fe80::20c:29ff:fe66:37f7 prefixlen 64 scopeid 0x20<link>
2、安装DHCP服务(前提是已经配置好yum仓库)
[root@PC1 ~]# yum install dhcp
Loaded plugins: langpacks, product-id, subscription-manager
Is this ok [y/d/N]: y
Downloading packages:
………………
Installed:
dhcp.x86_64 12:4.2.5-27.el7
Complete!
3、DHCP配置文件位置
[root@PC1 dhcp]# pwd
/etc/dhcp
[root@PC1 dhcp]# ls
dhclient.d dhcpd6.conf dhcpd.conf
4、DHCP配置文件中常见参数作用
5、修改配置文件
[root@PC1 dhcp]# vim /etc/dhcp/dhcpd.conf ## ip地址范围为50-150
ddns-update-style none;
ignore client-updates;
subnet 192.168.10.0 netmask 255.255.255.0 {
range 192.168.10.50 192.168.10.150;
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;
default-lease-time 21600;
max-lease-time 43200;
}
各参数意义:
6、重启服务器PC1的DHCP服务,是配置文件生效,并设定开机自动启动服务
[root@PC1 dhcp]# systemctl restart dhcpd
[root@PC1 dhcp]# systemctl enable dhcpd
ln -s '/usr/lib/systemd/system/dhcpd.service' '/etc/systemd/system/multi-user.target.wants/dhcpd.service'
[root@PC1 dhcp]# systemctl status dhcpd | head -n 5 ## 查看服务状态
dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled)
Active: active (running) since Wed 2020-12-02 18:47:16 CST; 2min 10s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
7、开启客户机PC2进行验证
[root@PC2 Desktop]# ifconfig | head -n 3 ## 查看当前ip,未分配
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:25:bb:3e txqueuelen 1000 (Ethernet)
RX packets 22 bytes 1320 (1.2 KiB)
[root@PC2 Desktop]# vim /etc/sysconfig/network-scripts/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@PC2 Desktop]# systemctl restart network ## 重启网卡
[root@PC2 Desktop]# ifconfig | head -n 3 ## 再次查看ip
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.51 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link>
8、开启客户机PC3进行验证
[root@PC3 Desktop]# ifconfig | head -n 3 ## 查看当前ip
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:7d:e7:11 txqueuelen 1000 (Ethernet)
RX packets 11 bytes 660 (660.0 B)
[root@PC3 Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777728 ## 编辑网卡配置文件,设为开机自动启动
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
[root@PC3 Desktop]# systemctl restart network ## 重启网卡
[root@PC3 Desktop]# ifconfig | head -n 3 ## 查看ip
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.52 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe7d:e711 prefixlen 64 scopeid 0x20<link>
总结:
- 服务器与客户机无需特殊处理,可以实现自动关联
- 客户机重启网卡,才可以实现自动被分配IP