Guide | DHCP (Dynamic Host Configuration Protocol) is used to automatically assign IP addresses to PCs and other network devices so that they can communicate. It uses port 67 of the UDP protocol and UDP port 68 for the client. The DHCP operation is divided into four stages: server discovery, IP lease quotation, IP lease request and IP lease confirmation. These stages are usually abbreviated as DORA and are used for discovery, provision, request and confirmation. |
System environment
Hundreds 8
Install DHCP
The address of the DHCP server is: 192.168.43.254/24, the default gateway is: 192.168.43.2/24, the DHCP server will automatically assign IP addresses to other devices in the network 192.168.43.0/24.
The following is the address information of the DHCP server:
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160
Install the DHCP service below:
[root@localhost ~]# dnf -y install dhcp-server
Configure DHCP service
The main configuration file of the DHCP server is /etc/dhcp/dhcpd.conf
. By default, there are only comments in the file, suggesting that you can refer to the /usr/share/doc/dhcp-server/dhcpd.conf.example
file to modify the configuration.
Provide the configuration information directly below, paste the configuration information into the dhcpd.conf file, and modify the address and other information:
[root@localhost ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp-server/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;
subnet 192.168.43.0 netmask 255.255.255.0 {
range 192.168.43.101 192.168.43.200;
option routers 192.168.43.2;
option subnet-mask 255.255.255.0;
option domain-name-servers 192.168.43.2;
}
default-lease-time
Reserve an IP address for a specific device for 10 minutes (600 seconds)
max-lease-time
The IP address is reserved for up to 2 hours (7200 seconds).
subnet
Partly defines the DHCP configuration of the 192.168.43.0/24 network.
range
Part of the definition of the assignable IP address, the range is 192.168.43.101-192.168.43.200.
routers
Part defines the address of the default gateway.
subnet-mask
The part defines the subnet mask that will be assigned to each host.
domain-name-servers
The part defines the DNS server address that will be assigned to each host.
After the configuration is complete, you can use the following command to start the dhcp service:
[root@localhost ~]# systemctl enable dhcpd && systemctl start dhcpd
Created symlink /etc/systemd/system/multi-user.target.wants/dhcpd.service → /usr/lib/systemd/system/dhcpd.service.
Check whether the service is running below:
[root@localhost ~]# systemctl status dhcpd
Configure firewall
If you turn on the firewall, you need to let go of the dhcp service in the firewall.
[root@localhost ~]# firewall-cmd --permanent --add-service=dhcp
success
[root@localhost ~]# firewall-cmd --reload
success
Client test
Obtained the address
on the client : Check on the client whether it is the address assigned by the DHCP server 192.168.43.254:
[root@localhost ~]# nmcli connection show ens33 |grep -i 'dhcp4.option\[4\]'
DHCP4.OPTION[4]: dhcp_server_identifier = 192.168.43.254
You can see that the dhcp server identifier is 192.168.43.254.
Set reserved IP
First, you need to know the network card address of the server that needs to set the reserved ip address. Use the client to ip link
view the mac address of the network card:
[root@localhost ~]# ip link
Next, edit the /etc/dhcp/dhcpd.conf
configuration file on the dhcp server , add the following content, and the bound address is 192.168.43.150:
host server {
hardware ethernet 00:0c:29:99:ee:d9;
fixed-address 192.168.43.150;
}
Restart the dhcp service:
[root@localhost dhcp]# systemctl restart dhcpd
Next, on the client side, restart the network service to see if the fixed address is obtained.
You can see that the address has been obtained.
to sum up
In this question, I learned how to configure a DHCP server on Centos8. We have seen that the host automatically obtains an IP address from a DHCP server, and we have also seen how to bind an IP address to a specific computer using a MAC address. Linux should be learned like this