The old driver teaches you to quickly install the DHCP server!

Dynamic Host Configuration Protocol (DHCP)  is a network protocol used to enable a host to automatically assign an IP address and related network configuration from a server. A DHCP server assigns an IP address to a DHCP client on a "lease," usually for as long as the client computer requires a connection or as configured by the DHCP server.

How does DHCP work?

Here is a brief description of how DHCP actually works:

  •   Once a client (a machine configured to use DHCP) connects to the network, it sends a  DHCPDISCOVER  packet to the DHCP server.
  • When the DHCP server receives  the DHCPDISCOVER  request message, it will   reply with a DHCPOFFER packet.
  • Then the client gets  the DHCPOFFER  packet and sends a DHCPREQUEST packet to the server, indicating that it is ready to receive   the network configuration information provided in the DHCPOFFER packet.
  • Finally, after the DHCP server receives the DHCPREQUEST  message  from the client  , it sends a DHCPACK  message, indicating that the client is now allowed to use the IP address assigned to it.

In this article, we will explain how to setup a DHCP server in Ubuntu/Debian  Linux  , we will use the sudo  command to run all the commands to gain root privileges.

Test environment settings

In this step we will use the following test environment.

  • DHCP Server - Ubuntu 16.04
  • DHCP Clients - CentOS 7 and Fedora 25

Step 1: Install DHCP Server in Ubuntu

1. Run the following command to install the DHCP server package, namely  dhcp3-server .

$ sudo apt install isc-dhcp-server

2. After the installation is complete, edit /etc/default/isc-dhcp-server and use the INTERFACES option to define the interface used by DHCPD to respond to DHCP requests.

For example, if you want the DHCPD daemon to listen on eth0, set it as follows:

INTERFACES="eth0"

Also remember to configure a static address for the interface above.

Step 2: Configure DHCP Server in Ubuntu

3. The main file for DHCP configuration is /etc/dhcp/dhcpd.conf, you must fill in all the network information that will be sent to the client.

And there are two different claims defined in the DHCP configuration, which are:

  • parameters - specifies how the task is performed, whether to perform the task, and specifies network configuration options to be sent to the DHCP client.
  • declarations - define network topology, specify clients, provide clients with addresses, or apply a set of parameters to a set of declarations.

4. Now open and modify the main file to define the DHCP server options:

$ sudo vi /etc/dhcp/dhcpd.conf 

Set the following global parameters at the top of the file, which will apply to all declarations below (please specify the values ​​that apply to your situation):

option domain-name "tecmint.lan";
option domain-name-servers ns1.tecmint.lan, ns2.tecmint.lan;
default-lease-time 3600; 
max-lease-time 7200;
authoritative;

5. Now define a subnet, here we set DHCP for 192.168.10.0/24 LAN (please use the parameters that apply to your situation):

subnet 192.168.10.0 netmask 255.255.255.0 {
  option routers                  192.168.10.1;
  option subnet-mask              255.255.255.0;
  option domain-search            "tecmint.lan";
  option domain-name-servers      192.168.10.1;
  range   192.168.10.10   192.168.10.100;
  range   192.168.10.110   192.168.10.200;
}

Step 3: Configure Static Addresses on DHCP Clients

6. To assign a fixed (static) IP to a particular client, you need to explicitly add the MAC address of that machine along with the statically assigned address to the section below.

host centos-node {
  hardware ethernet 00:f0:m4:6y:89:0g;
  fixed-address 192.168.10.105;
}
host fedora-node {
  hardware ethernet 00:4g:8h:13:8h:3a;
  fixed-address 192.168.10.106;
}

Save and close the file.

7. Next, start the DHCP service, and let it start automatically at next boot, as shown below:

------------ SystemD ------------ 
$ sudo systemctl start isc-dhcp-server.service
$ sudo systemctl enable isc-dhcp-server.service
------------ SysVinit ------------ 
$ sudo service isc-dhcp-server.service start
$ sudo service isc-dhcp-server.service enable

8. Next, don’t forget to allow firewall permissions for the DHCP service (DHCP daemon listens on UDP port 67):

$ sudo ufw allow 67/udp
$ sudo ufw reload
$ sudo ufw show

Step 4: Configure the DHCP client

9. At this point, you can configure the client computer to automatically receive an IP address from the DHCP server.

Login to the client and edit the configuration file for the ethernet interface (note the interface name/number):

$ sudo vi /etc/network/interfaces

Define the following options:

car eth0
iface eth0 inet dhcp

Save the file and exit. Restart network services (or reboot the system):

------------ SystemD ------------ 
$ sudo systemctl restart networking
------------ SysVinit ------------
$ sudo service networking restart

Alternatively you can use the GUI to set it up, as shown in the screenshot (in the Fedora 25 desktop) set the method to automatic (DHCP).

At this point, if everything is set up, your clients should automatically receive IP addresses from the DHCP server.

That's it! In this tutorial, we have shown you how to setup DHCP server on Ubuntu/Debian. Share your thoughts in the feedback bar. If you are using a Fedora-based distribution, read How to Setup a DHCP Server in CentOS/RHEL.


About the Author:

Aaron Kili is a Linux and FOSS enthusiast, future Linux SysAdmin and web developer, and current TecMint content creator who loves working with computers and believes in sharing knowledge.

Guess you like

Origin blog.csdn.net/yaxuan88521/article/details/131565175