Configure DHCP server on Linux

                          Summary                           

I. Presentation

II. Installing the DHCP server

A. Configuring the DHCP server

B. Testing DHCP

III. Fixed addressing with mac addresses

A. Configuration

B. Fixed addressing test

IV. DHCP Relay

A. Installation of DHCP relay

B. Configuring the DHCP Relay

C. DHCP Relay Testing

 


 

II. Presentation

A DHCP server can automatically provide an IP configuration to a machine, for example computers, smartphones, network printers, basically anyone who can be connected to a network. This IP configuration is composed of:

  • an IP address
  • a network mask
  • a bridge
  • a DNS address

When a machine without an IP configuration connects to the network, the network broadcasts DHCP DISCOVER to ask if a DHCP server exists. The DHCP server responds with a DHCP OFFER and starts giving first parameters, the machine sends a DHCP REQUEST request and then the DHCP server sends a DHCP ACK to provide the IP configuration.

The IP configuration can be provided with a lease. This lease indicates that such machine will have such IP for a duration of 7 days for example. At the deadline, if the machine is still on the network, it keeps the same IP for a new lease of 7 days. If after 2 days the machine leaves the network, the IP it used will be available for other machines at the end of the lease.

A single DHCP server can provide IP configurations to several different networks, for this we must use the DHCP relay, we will set up this system in the second part of the tutorial.

The DHCP server provides IPs that are in an available IP range, for example 192.168.1.50 to 192.168.1.100.

Prerequisites:

  • a Debian machine with a fixed IP.
  • No other active DHCP server to avoid conflicts.
  • A client machine (eg Debian) without IP configuration to test.
  • An Internet connection.

 

II. Installing the DHCP server

 

We start with an update of the system packages:

apt-get update

We will now install the following package:

apt-get install dhcp3-server

 

A. Configuring the DHCP server :

 

As you can imagine, installing the package is not enough to run our DHCP server, there are small configurations to do. We will go to the following file:

nano / etc / default / isc-dhcp-server

At the end of this file find the following line:

INTERFACES = ""

We will indicate which network interface we will use, if your DHCP server is not on a router, then there is normally only one network card that is eth0:

INTERFACES = "eth0"

We save and close this file. We now have a last file to configure, it is in this one that we will indicate the IP configurations to provide:

nano /etc/dhcp/dhcpd.conf

In this file we can find many explanations and examples in English. We copy the text below at the very end of the file:

# Our configuration for 172.18.0.0 network
subnet 172.18.0.0 netmask 255.255.0.0 {
range 172.18.0.20 172.18.0.30;
option domain-name-servers 8.8.8.8;
option domain-name "network.lan";
option routers 172.18.0.1;
option broadcast-address 172.18.255.255;
default-lease-time 600;
max-lease-time 7200;

}

 

So for explanations:

  • The first line is a comment, so it is not used for anything to indicate on which network we will set.
  • The second line indicates the network as well as the network mask.
  • The third line indicates the rank of IP addresses provided.
  • The fourth line, we indicate the DNS. Here having no DNS, I put the address of a DNS of Google.
  • The fifth line is the name of our domain.
  • The sixth line shows the address of our gateway.
  • The seventh line shows the broadcast address of the network.
  • And finally the last 2 lines, are for the lease. The default lease and the maximum lease are indicated. This lease is expressed in seconds.

We save this file and leave it then restart the DHCP server service:

service isc-dhcp-server restart

The configuration is now complete, we will be able to go to the test phase.

 

B. DHCP test :

 

For the test, we will just start a Debian machine. We will then tell the machine to go into DHCP instead of using a fixed IP.
For this we go to the network card configuration file:

nano /etc/network/interfaces

We modified this file in order to have the following lines:

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp
On enregistre et on quitte.
On tape ensuite les commandes suivantes :
ifdown eth0
ifup eth0

We do an ifconfig to get the information on the maps. We now see that we have in eth0:

An IP address in the rank that has been defined (logically the first address, so in my case: 172.18.0.20).
A broadcast address in 172.18.255.255.
A mask in 255.255.0.0.
We can ping the gateway that is 172.18.0.1. And finally, we can also ping the site www.twasha.com.

 

II. Installing the DHCP server

 

You may need to tell the DHCP server that this machine will always have such an IP address.

Prerequisites:

  • a functioning DHCP server.
  • a client machine.

A. Configuration :

To do this, just go to the DHCP server configuration file:

nano /etc/dhcp/dhcpd.conf

At the end we add the following lines:

host client1_debian {
hardware ethernet 08:00:27:bb:40:6b;
fixed-address 172.18.0.26;
}

The explanations:

  • the first line indicates the name of the client machine.
  • The second line, we indicate its address mac.
  • The last line, we indicate which IP address it will have permanently.

We save this file and leave it then restart the DHCP server service:

service isc-dhcp-server restart

B. Fixed addressing test :

For the test, we will just start a Debian machine. We will then tell the machine to go into DHCP instead of using a fixed IP.
For this we go to the network card configuration file.

nano /etc/network/interfaces

We modify this file in order to have the following lines:

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

We save and exit then we type the following commands:

ifdown eth0
ifup eth0

We do an ifconfig to get the information on the maps.

We now see that we have in eth0:

  • an address that is the one we defined (172.18.0.26).
  • A broadcast address in 172.18.255.255.
  • A mask in 255.255.0.0.
  • We can ping the gateway that is 172.18.0.1.

And finally, we can also ping the site www.twasha.com.

 

IV. DHCP Relay

 

DHCP Relay allows the DHCP server to provide IP configurations to machines in a network different from its own. Depending on the network where the client machine is located, it will have such IP configuration or other.

Prerequisites:

  • a functioning DHCP server.
  • A router to link the networks.
  • Minimum 2 networks: one for the DHCP server, one for the client machine.
  • An Internet connection.
  • 2 client machines.

A. Installation of DHCP relay

On the router we install the following package:

apt-get install dhcp3-relay

During the installation, we indicate the IP address of the DHCP server:

172.18.0.2

It also indicates the interface where the DHCP relay will listen:

10.0.0.1

For additional options leave empty.

B. Configuring the DHCP Relay

We modify our configuration file on the DHCP server. We indicate our network where we want to provide IP configurations:

 nano /etc/dhcp/dhcpd.conf

At the end, we add the following lines:

# Our configuration for the 10.0.0.0 network
subnet 10.0.0.0 netmask 255.0.0.0 {
range 10.0.0.40 10.0.0.60;
option domain-name-servers 8.8.8.8;
option domain-name "network.lan";
option routers 10.0.0.1;
option broadcast-address 10.255.255.255;
default-lease-time 600;
max-lease-time 7200;
}

We record and we leave. Then finally, we restart the DHCP server service:

 service isc-dhcp-server restart

On the router, restart the relay DHCP service:

 service isc-dhcp-relay restart

C. DHCP Relay Testing :

For the test, we will just start a Debian machine we have already connected to our router, in the good vlan. We will then tell the machine to go into DHCP instead of using a fixed IP. For this we go to the network card configuration file:

 nano / etc / network / interfaces

We modify this file in order to have the following lines:

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

We record and we leave. Then type the following commands:

 ifdown eth0
ifup eth0

We do an ifconfig to get the information on the maps.

We now see that we have in eth0:

  • does not IP address in the rank that has been defined (logically the first address, so in my case: 10.0.0.40).
  • A broadcast address in 10.255.255.255.
  • A mask in 255.0.0.0.
  • We can ping the gateway which is in 10.0.0.1. And finally, we can also ping the site www.twasha.com.

To conclude, in this tutorial we saw:

  • A DHCP server is used to provide an IP configuration to a machine that plugs into the network.
  • This IP configuration works with a lease system.
  • That a DHCP server can always provide the same IP address to a machine thanks to its mac address.
  • That a DHCP server can provide IP configurations to machines that are in different networks of the server.

Leave a Reply

Your email address will not be published. Required fields are marked *