Set up a DHCP server on Debian 9?

0
476

One of the main tasks of any sysadmin is to perform network services. Some of these services range from the most complex to the simplest. However, the DHCP protocol is a basic service in a network but it is one of the most important. Therefore, today I will show you to set up a DHCP server on Debian 9.

DHCP (Dynamic Host Configuration Protocol) is a client-server network protocol that automatically assigns IP addresses within a set range. The DHCP server assigns IP addresses as they become free or unused by other members of the network.

To achieve its goal a DHCP server can assign IP addresses as follows:

  • Manual or static assignment: assigns an IP address to a specific machine.
  • Automatic assignment: assigns an IP address to a client machine the first time it makes the request to the DHCP server and until the client releases it.
  • And the third way to assign an IP address by a DHCP server is dynamic assignment.

So, let’s start.

0. What I will do

First of all, I’ll explain to you what I’m going to do in this post.

Obviously, the first step is to install the DHCP network service. Then, I will teach you how to work with the systemd service.

At the end of this, we will define a range of IP addresses to be assigned automatically and finally, I will teach you how to configure the DHCP service to assign static addresses to the network nodes.

So this is the menu.

1. Install DHCP package

Installing DHCP is something anyone can do because it is a service that is in the official repositories of most Linux distributions. In this case, the tutorial is developed on Debian 9, so a command is enough to install it:

:~$ su
:~# apt install isc-dhcp-server
1.- Install dhcp package
1.- Install DHCP package

Next, it is a good idea to enable DHCP to start at the system booting. To do that type:

:~# systemctl enable isc-dhcp-server

So, let’s configure DHCP.

2. Configure the DHCP server

It’s really easy to set up at least a basic DHCP server. All its configuration is in the file /etc/dhcp/dhcpd.conf. So, you have to edit it. But first, back up the file.

:~# cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
:~# nano /etc/dhcp/dhcp.conf

When you open the file for the first time, you will see that it has loaded by default some species of configuration templates that you can modify to your liking.

For example, suppose the IP address of this computer is 192.168.250.1 and you need to assign IP addresses from 192.168.250.101 to 192.168.250.254, then the configuration would look like this.

subnet 192.168.0.0 netmask 255.255.0.0 { 
        range 192.168.250.101 192.168.250.254; 
        option subnet-mask 255.255.0.0; 
        option broadcast-address 192.168.255.255; 
}
2.- Some configuration
2.- Some configuration

I remind you that these data are fictitious and only show an example of the configuration. You must replace them with the appropriate ones.

Before restarting the service, open this file /etc/default/isc-dhcp-server and add your network interface on INTERFACESV4 section.

:~# nano /etc/default/isc-dhcp-server
3.- Adding your network interface
3.- Adding your network interface

NOTE: If you don’t know what your network interface is, you can do it by running the ip or ifconfig command.

4.- Using the IP command
4.- Using the IP command

So, you can restart the service.

:~# systemctl restart isc-dhcp-server

Assigning a static IP

It is possible to assign a static address to one or more hosts within the network. To do this, create a file in /etc/dhcpd/hosts and place the following.

:~# nano /etc/dhcp/hosts
host nombre_host {
hardware ethernet [MAC];
fixed-addres [IP];
}

I mean, it has to stay that way:

host angelosradar {
        hardware ethernet E1:3E:49:1C:BF:34;
        fixed-address 192.168.250.55;
}
5.- Setting a static IP
5.- Setting a static IP

And that’s it.

NOTE: Only one DHCP server must exist in each network segment. If you have a router or ADSL modem, it is most likely that they have the DHCP service. In order for these steps to work for you, you must disconnect from them. Or disable the DHCP service on them.

Conclusion

A DHCP server is a simple thing to do, however, it is good to have knowledge about your network to do it correctly.

LEAVE A REPLY

Please enter your comment!
Please enter your name here