Sie sind auf Seite 1von 13

DHCP

DHCP stands for Dynamic Host Configuration Protocol.


DHCP is used to control the network configuration of a host through a remote
server.
DHCP functionality comes installed as a default feature in most of the
contemporary operating systems.
DHCP is an excellent alternative to the time-consuming manual configuration
of network settings on a host or a network device.
DHCP works on a client-server model. Being a protocol, it has its own set of
messages that are exchanged between client and server.
The protocol used for dhcp is UDP and the port number used is 67

HOW DHCP WORKS?


1. DHCPDISCOVER
It is a DHCP message that marks the beginning of a DHCP interaction between
client and server. This message is sent by a client (host or device connected to a
network) that is connected to a local subnet. Its a broadcast message that uses
255.255.255.255 as destination IP address while the source IP address is 0.0.0.0
2. DHCPOFFER
It is DHCP message that is sent in response to DHCPDISCOVER by a DHCP
server to DHCP client. This message contains the network configuration settings
for the client that sent the DHCPDISCOVER message.
3. DHCPREQUEST
This DHCP message is sent in response to DHCPOFFER indicating that the client
has accepted the network configuration sent in DHCPOFFER message from the
server.

4. DHCPACK
This message is sent by the DHCP server in response to DHCPREQUEST recieved
from the client. This message marks the end of the process that started with
DHCPDISCOVER. The DHCPACK message is nothing but an acknowledgement
by the DHCP server that authorizes the DHCP client to start using the network
configuration it received from the DHCP server earlier.
The steps mentioned below assume that DHCP functionality is enabled by default
on the client side.
Here are the steps :
Step 1: When the client computer (or device) boots up or is connected to a
network, a DHCPDISCOVER message is sent from the client to the server. As
there is no network configuration information on the client so the message is sent
with 0.0.0.0 as source address and 255.255.255.255 as destination address. If the
DHCP server is on local subnet then it directly receives the message or in case it is
on different subnet then a relay agent connected on clients subnet is used to pass
on the request to DHCP server.

Step 2: When the DHCP server receives the DHCPDISCOVER request message
then it replies with a DHCPOFFER message. As already explained, this message
contains all the network configuration settings required by the client. For example,
the yaddr field of the message will contain the IP address to be assigned to client.
Similarly the the subnet mask and gateway information is filled in the options
field. Also, the server fills in the client MAC address in the chaddr field. This
message is sent as a broadcast (255.255.255.255) message for the client to receive
it directly.

Step 3: The client forms a DHCPREQUEST message in reply to DHCPOFFER


message and sends it to the server indicating it wants to accept the network
configuration sent in the DHCPOFFER message. If there were multiple DHCP
servers that received DHCPDISCOVER then client could receive multiple
DHCPOFFER messages. But, the client replies to only one of the messages by
populating the server identification field with the IP address of a particular DHCP
server. All the messages from other DHCP servers are implicitly declined.

Step 4: Once the server receives DHCPREQUEST from the client, it sends the
DHCPACK message indicating that now the client is allowed to use the IP address
assigned to it. The client enters the bound state during this step.
Configuring a DHCP Server
1.First, install the package as root:
# yum install dhcp
Installing the dhcp package creates a file, /etc/dhcp/dhcpd.conf, which is merely
an empty configuration file:
The sample configuration
version/dhcpd.conf.sample.

file

can

be

found

at

/usr/share/doc/dhcp-

DHCP also uses the file /var/lib/dhcpd/dhcpd.leases to store the client lease
database

2. Configuration file
The first step in configuring a DHCP server is to create the configuration file that
stores the network information for the clients.
There are two types of statements in the configuration file.
Parameters State how to perform a task, whether to perform a task, or
what network configuration options to send to the client.

Declarations Describe the topology of the network, describe the clients,


provide addresses for the clients, or apply a group of parameters to a group
of declarations.

The parameters that start with the keyword option are referred to as options.
These options control DHCP options; whereas, parameters configure values
that are not optional or control how the DHCP server behaves.
Parameters (including options) declared before a section enclosed in curly
brackets ({ }) are considered global parameters.

The Configuration File /etc/dhcpd.conf


default-lease-time 600;
# 10 minutes
max-lease-time 7200;
# 2 hours
option domain-name "example.com";
option domain-name-servers 192.168.1.116;
option broadcast-address 192.168.2.255;
option routers 192.168.2.1;
option subnet-mask 255.255.255.0;
subnet 192.168.2.0 netmask 255.255.255.0
{
range 192.168.2.10 192.168.2.20;
range 192.168.2.100 192.168.2.200;
}

The sample file can be divided into three sections.


The first one defines how many seconds an IP address is leased to a requesting
client by default (default-lease-time) before it should apply for renewal. This
section also includes a statement of the maximum period for which a machine
may keep an IP address assigned by the DHCP server without applying for
renewal (max-lease-time).
In the second part, some basic network parameters are defined on a global
level:
The line option domain-name defines the default domain of your network.
With the entry option domain-name-servers, specify up to three values for
the DNS servers used to resolve IP addresses into hostnames and vice
versa. Ideally, configure a name server on your machine or somewhere
else in your network before setting up DHCP. That name server should also
define a hostname for each dynamic address and vice versa.

The line option broadcast-address defines the broadcast address the


requesting client should use.
With option routers, set where the server should send data packets
that cannot be delivered to a host on the local network.
With option subnet-mask, specify the netmask assigned to clients.
The last section of the file defines a network, including a subnet mask.
To finish, specify the address range that the DHCP daemon should use
to assign IP addresses to interested clients. In Example clients may be
given any address between 192.168.2.10 and 192.168.2.20 as well
as 192.168.2.100 and 192.168.2.200.

Clients with Fixed IP Addresses

DHCP can also be used to assign a predefined, static address to a specific client.

To identify a client configured with a static address, dhcpd uses the hardware
ddress (which is a globally unique, fixed numerical code consisting of six octet
airs) for the identification of all network devices (for example, 00:30:6E:08:EC:80

host jupiter {
hardware ethernet 00:30:6E:08:EC:80;
fixed-address 192.168.2.100;
}
The name of the respective client (host hostname, here jupiter) is entered in the
irst line and the MAC address in the second line. a client with a network card
aving the MAC address 00:30:6E:08:EC:80 is assigned the IP address
92.168.2.100 and the hostname jupiter automatically.

Starting and Stopping the Server


When the DHCP server is started for the first time, it fails unless the
dhcpd.leases file exists. Use the command touch
/var/lib/dhcpd/dhcpd.leases to create the file if it does not exist.
To start the DHCP service, use the following command:
#systemctl start dhcpd.service
To stop the DHCP server, type:
#systemctl stop dhcpd.service
By default, the DHCP service does not start at boot time.
To configure the daemon to start automatically at boot time, run:
#systemctl enable dhcpd.service

Configuring DHCP Client


To configure a DHCP client manually, modify the /etc/sysconfig/network file to
enable networking and the configuration file for each network device in the
/etc/sysconfig/network-scripts directory. In this directory, each device should have
a configuration file named ifcfg-eth0, where eth0 is the network device name.
The /etc/sysconfig/network file should contain the following line:
NETWORKING=yes
The NETWORKING variable must be set to yes if you want networking to start
at boot time.
The /etc/sysconfig/network-scripts/ifcfg-eth0 file should contain the following lines:
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes