Wednesday, 8 August 2012

DHCP Server

                The Dynamic Host Configuration Protocol (DHCP) is a network configuration protocol for hosts on Internet Protocol (IP) networks. Computers that are connected to IP networks must be configured before they can communicate with other hosts. The most essential information needed is an IP address, and a default route and routing prefix. DHCP eliminates the manual task by a network administrator. It also provides a central database of devices that are connected to the network and eliminates duplicate resource assignments.
           In addition to IP addresses, DHCP also provides other configuration information, particularly the IP addresses of local Domain Name Server (DNS), network boot servers, or other service hosts. Let's see how to install  and configure dhcp server in a centos 5 or redhat el5 system.
Applicability
Dynamic Host Configuration Protocol is a way to administer network parameter assignment at a single DHCP server, or a group of such servers arranged in a fault-tolerant manner.
Even in a network which has a few machines, Dynamic Host Configuration Protocol is useful because a machine can be added to the local network with little effort. Even for servers whose addresses rarely change, DHCP is recommended for setting their addresses, so if the servers need to be readdressed (RFC2071), the changes need to be made in as few places as possible. For devices, such as routers and firewalls, that should not use DHCP, it can be useful to put Trivial File Transfer Protocol (TFTP) or SSH servers on the same machine that runs DHCP, again to centralize administration.
DHCP is also useful for directly assigning addresses to servers and desktop machines, and, through a Point-to-Point Protocol (PPP) proxy, for dialup and broadband on-demand hosts, as well as for residential Network address translation (NAT) gateways and routers. DHCP is usually not appropriate for infrastructure such as non-edge routers and DNS servers.
DHCP emerged as a standard protocol in October 1993 as defined in RFC 1531, succeeding the BOOTP protocol. The next RFC was 2131, released in 1997. The current DHCP definition can be found in RFC 2131, while a proposed standard for DHCP over IPv6 (DHCPv6) can be found in RFC 3315.
Technical details
Schema of a typical DHCP session DHCP uses the same two IANA assigned ports as BOOTP: 67/udp for the server side, and 68/udp for the client side.
DHCP operations fall into four basic phases. These phases are IP discovery, IP lease offer, IP request, and IP lease acknowledgement.
After the client obtained an IP address, the client may  start an address resolution (ARP) query to prevent IP
conflicts caused by address pool overlapping of DHCP servers.
DHCP communication.

DHCP discovery
The client broadcasts on the physical subnet to find available servers. Network administrators can configure
a local router to forward DHCP packets to a DHCP server on a different subnet. This client-implementation creates a UDP packet with the broadcast destination of 255.255.255.255 or subnet broadcast address.
A client can also request its last-known IP address (in the example below, 192.168.1.100). If the client is still in a network where this IP is valid, the server might grant the request. Otherwise, it depends whether the server is set up as authoritative or not. An authoritative server will deny the request, making the client ask for a new IP immediately. A non-authoritative server simply ignores the request, leading to an implementation dependent time out for the client to give up on the request and ask for a new IP address.
DHCP offers
When a DHCP server receives an IP lease request from a client, it extends an IP lease offer. This is done by reserving an IP address for the client and sending a DHCPOFFER message across the network to the client. This message contains the client's MAC address, followed by the IP address that the server is offering, the subnet mask, the lease duration, and the IP address of the DHCP server making the offer.
The server determines the configuration, based on the client's hardware address as specified in the CHADDR field. Here the server, 192.168.1.1, specifies the IP address in the YIADDR field.
DHCP requests
When the client PC receives an IP lease offer, it must tell all the other DHCP servers that it has accepted an offer. To do this, the client broadcasts a DHCPREQUEST message containing the IP address of the server that made the offer. When the other DHCP servers receive this message, they withdraw any offers that they might have made to the client. They then return the address that they had reserved for the client back to the pool of valid addresses that they can offer to another computer. Any number of DHCP servers can respond to an IP lease request, but the client can only accept one offer per network interface card.
DHCP acknowledgement
When the DHCP server receives the DHCPREQUEST message from the client, it initiates the final phase of the configuration process. This acknowledgement phase involves sending a DHCPACK packet to the client. This packet includes the lease duration and any other configuration information that the client might have requested. At this point, the IP configuration process is complete.
The server acknowledges the request and sends the acknowledgement to the client. The system as a whole expects the client to configure its network interface with the supplied options.
Setting Up a simple DHCP Server
Here we will set the dhcp server for the network 192.168.137.0/24
Network 192.168.137.0/24

Client's ip range        192.168.137.150 - 192.168.137.250
Gateway 192.168.137.1
Bcast 192.168.137.255
DNS servers  8.8.8.8 and 8.8.4.4

The package name is dhcp. We will install usign yum.
[root@server ~]# yum install dhcp
[root@server ~]# rpm -q dhcp
dhcp-3.0.5-13.el5
[root@server ~]#
/etc/dhcpd.conf - is the  main configuration file

/var/lib/dhcpd  - Lease directory
/var/lib/dhcpd/dhcpd.leases - IPV4 Leases

The default dhcp configuration file will be a reference to the sample file.
[root@server ~]# cat /etc/dhcpd.conf
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#[root@server ~]#

We will copy the sample file and edit it.
root@server ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.sample /etc/dhcpd.conf
root@server ~]# cat  /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;

subnet 192.168.137.0 netmask 255.255.255.0 {
        option routers                  192.168.137.1;
        option subnet-mask              255.255.255.0;
        option domain-name              "lap.work";
        option domain-name-servers      8.8.8.8, 8.8.4.4;
        range dynamic-bootp 192.168.137.150 192.168.137.250;
        default-lease-time 21600;
        max-lease-time 43200;
}

Check the service and start it.
[root@server ~]# /etc/init.d/dhcpd status
dhcpd is stopped
[root@server ~]# /etc/init.d/dhcpd start
Starting dhcpd:                                            [  OK  ]
[root@server ~]# chkconfig dhcpd on

Now from the client machine we can set the network settings on the eth0 device to dhcp and restart the network.

DHCP works in DORA format

Client sends DHCPDISCOVER (D)
Server sends DHCPOFFER (O)
Client sends DHCPREQUEST (R)
Server sends DHCPACK (A)

Now on taling the /var/log/messages on dhcp server we can see that all this happens while we restart the network on client
[root@server ~]# tail -f /var/log/messages
Feb 27 22:50:09 server dhcpd: DHCPDISCOVER from 00:0c:29:8d:16:93 via eth0

Feb 27 22:50:10 server dhcpd: DHCPOFFER on 192.168.137.250 to 00:0c:29:8d:16:93 via eth0

Feb 27 22:50:10 server dhcpd: DHCPREQUEST for 192.168.137.250 (192.168.137.100) from 00:0c:29:8d:16:93 via eth0

Feb 27 22:50:10 server dhcpd: DHCPACK on 192.168.137.250 to 00:0c:29:8d:16:93 via eth0

The lease file at the server side is stored at
[root@server ~]# cat /var/lib/dhcpd/dhcpd.leases
# All times in this file are in UTC (GMT), not your local timezone.   This is
# not a bug, so please don't ask about it.   There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature.   If this is inconvenient or confusing to you, we sincerely
# apologize.   Seriously, though - don't ask.
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-V3.0.5-RedHat
lease 192.168.137.250 {
  starts 1 2012/02/27 17:04:49;
  ends 1 2012/02/27 23:04:49;
  binding state active;
  next binding state free;
  hardware ethernet 00:0c:29:8d:16:93;
}
[root@server ~]#
If you want you can make a separate log file for dhcp add this line
log-facility local8;

so makes the dhcpd.conf
root@server ~]# cat  /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
subnet 192.168.137.0 netmask 255.255.255.0 {
        option routers                  192.168.137.1;
        option subnet-mask              255.255.255.0;
        option domain-name              "lap.work";
        option domain-name-servers      8.8.8.8, 8.8.4.4;
 range dynamic-bootp 192.168.137.150 192.168.137.250;
        default-lease-time 21600;
        max-lease-time 43200;
log-facility local8;
}
[root@server ~]#
Restart the dhcpd service
touch the file /var/log/dhcpd.log
and in /etc/syslog.conf
add the line
local8.*       /var/log/dhcpd.log
and restart syslog servce
            In client machine. It gets the ip 192.168.137.250  which is in the range we specified.
[root@server ~]# ifconfig
eth0   Link encap:Ethernet  HWaddr 00:0C:29:8D:16:93
   inet addr:192.168.137.250  Bcast:192.168.137.255  Mask:255.255.255.0
    inet6 addr: fe80::20c:29ff:fe8d:1693/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
    RX packets:361 errors:0 dropped:0 overruns:0 frame:0
    TX packets:544 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:39256 (38.3 KiB)  TX bytes:130376 (127.3 KiB)
    Interrupt:75 Base address:0x2000
And also the nameserver details

[root@server ~]# cat /etc/resolv.conf

; generated by /sbin/dhclient-script
search lap.work
nameserver 8.8.8.8
nameserver 8.8.4.4
[root@server ~]#

The lease file at the client is
[root@server ~]# cat /var/lib/dhclient/dhclient-eth0.leases
lease {
  interface "eth0";
  fixed-address 192.168.137.250;
  option subnet-mask 255.255.255.0;
  option routers 192.168.137.1;
  option dhcp-lease-time 21600;
  option dhcp-message-type 5;
  option domain-name-servers 8.8.8.8,8.8.4.4;
  option dhcp-server-identifier 192.168.137.100;
  option domain-name "lap.work";
  renew 1 2012/2/27 19:37:49;
  rebind 1 2012/2/27 22:34:52;
  expire 1 2012/2/27 23:19:52;
}
[root@server ~]# 












DHCP Architecture
The DHCP architecture consists of DHCP clients, DHCP servers, and DHCP relay agents on a network. The clients
interact with servers using DHCP messages in a DHCP conversation to obtain and renew IP address leases. A DHCP client is any network-enabled device that supports the ability to communicate with a DHCP server in compliance with RFC 2131, for the purpose of obtaining dynamic leased IP configuration and related optional information. 
Automatic IP Configuration
DHCP supports Automatic Private IP Addressing (APIPA), which enables computers running Windows 2000, Windows XP, and Windows Server 2003 to configure an IP address and subnet mask if a DHCP server is unavailable
at system startup and the Automatic private IP address Alternate Configuration setting is selected.
1. The DHCP client attempts to locate a DHCP server and obtain an IP address and configuration.
2. If a DHCP server cannot be found or does not respond after one minute, the DHCP client checks the settings
on the Alternate Configuration tab of the properties of the  TCP/IP protocol.
     If Automatic private IP address is selected, the DHCP client auto-configures its IP address and subnet mask by using a selected address from the Microsoft-reserved Class B network, 169.254.0.0, with the subnet mask 255.255.0.0. The DHCP client tests for an address conflict to ensure that the IP address is not in use on the network.
3. When the DHCP client succeeds in self-selecting an address, it configures its network interface with the IP
address. The client then continues to check for a DHCP server in the background every five minutes. If a
DHCP server responds, the DHCP client abandons its self-selected IP address and uses the address offered
by the DHCP server (and any other DHCP option information that the server provides) to update its IP
configuration settings.
DHCP Server Responsibilities
The DHCP servers maintain scopes, reservations, and options as set by the administrator.
Scopes
A scope must be properly defined and activated before DHCP clients can use the DHCP server for automatic TCP/IP configuration. A DHCP scope is an administrative collection of IP addresses and TCP/IP configuration parameters that are available for lease to DHCP clients of a specific subnet. The network administrator creates a scope for each Subnet.
Lease Durations
When a scope is created, the lease duration is set to eight days by default. However there are situations when the administrator might want to change the lease duration. The following are examples of adjusting the lease duration due to individual network consideration:

--An organization has a large number of IP addresses available and configurations that rarely change. The
administrator increases the lease duration to reduce the frequency of lease renewal exchanges between
clients and the DHCP server. Because the DHCP clients are renewing their leases less frequently, DHCPrelated
network traffic is reduced.
--A limited number of IP addresses are available and client configurations change frequently or clients move
often in or out of the network. The administrator reduces the lease duration. This increases the rate at which
unused addresses are returned to the available address pool for reassignment.
Reservations
You can reserve IP addresses for assignment to specified computers or devices on the network. Reservations
ensure that a specified hardware device on a subnet always receives the same IP address lease. Use reservations for DHCP-enabled devices that must always have the same IP address on your network, such as servers that do not support Domain Name System (DNS) dynamic update.
Interactions between Client and Server
DHCP servers and DHCP clients communicate through a series of DHCP messages. To obtain a lease, the DHCP
client initiates a conversation with a DHCP server using a series of these DHCP messages.
DHCP Messages
The following list includes the eight types of messages that can be sent between DHCP clients and servers. For
more information about the structure and specifics of each of these packets, see “DHCP Message Format” later in
this section.
DHCPDiscover
Broadcast by a DHCP client when it first attempts to connect to the network. The DHCPDiscover message requests
IP address information from a DHCP server.
DHCPOffer
Broadcast by each DHCP server that receives the client DHCPDiscover message and has an IP address configuration to offer to the client. The DHCPOffer message contains an unleased IP address and additional TCP/IP
configuration information, such as the subnet mask and default gateway. More than one DHCP server can respond
with a DHCPOffer message. The client accepts the best offer, which for a Windows DHCP client is the first
DHCPOffer message that it receives.
DHCPRequest
Broadcast by a DHCP client after it selects a DHCPOffer. The DHCPRequest message contains the IP address from
the DHCPOffer that it selected. If the client is renewing or rebinding to a previous lease, this packet might be
unicast directly to the server.
DHCPAck
Broadcast by a DHCP server to a DHCP client acknowledging the DHCPRequest message. At this time, the server also forwards any options. Upon receipt of the DHCPAck, the client can use the leased IP address to participate in the TCP/IP network and complete its system startup. This message is typically broadcast, because the DHCP client does not officially have an IP address that it can use at this point. If the DHCPAck is in response to a DHCPInform, then the message is unicast directly to the host that sent the DHCPInform message.
DHCPNack
Broadcast by a DHCP server to a DHCP client denying the client’s DHCPRequest message. This might occur if the
requested address is incorrect because the client moved to a new subnet or because the DHCP client’s lease has
expired and cannot be renewed.
DHCPDecline
Broadcast by a DHCP client to a DHCP server, informing the server that the offered IP address is declined because
it appears to be in use by another computer.
DHCPRelease
Sent by a DHCP client to a DHCP server, relinquishing an IP address and canceling the remaining lease. This is unicast to the server that provided the lease.
DHCPInform
Sent from a DHCP client to a DHCP server, asking only for additional local configuration parameters; the client
already has a configured IP address. This message type is also used by DHCP servers running Windows
Server 2003 to detect unauthorized DHCP servers.
DHCP Lease Process
A DHCP-enabled client obtains a lease for an IP address from a DHCP server. Before the lease expires, the DHCP
client must renew the lease or obtain a new lease. Leases are retained in the DHCP server database for a period of
time after expiration. By default, this grace period is four hours and cleanup occurs once an hour for a DHCP server
running Windows Server 2003. This protects a clients lease in case the client and server are in different time zones, the internal clocks of the client and server computers are not synchronized, or the client is off the network when the lease expires.
Obtaining a New Lease
A DHCP client initiates a conversation with a DHCP server when it is seeking a new lease, renewing a lease,
rebinding, or restarting. The DHCP conversation consists of a series of DHCP messages passed between the DHCP
client and DHCP servers. The following figure shows an overview of this process when the DHCP server and DHCP
client are on the same subnet.
Client Conflict Detection
Client computers running Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, Windows
Millennium Edition, and Windows 98 automatically check to determine if an IP address is already in use before
using it.
After the DHCP client receives a lease from the DHCP server, the client sends an Address Resolution Protocol (ARP) request to the address that it has been assigned. If a reply to the ARP request is received, the client has detected a conflict and sends a DHCPDecline message to the DHCP server. The DHCP server attaches a BAD_ADDRESS value to the IP address in the scope for the length of the lease. The client then begins the lease process again, and is offered the next available address in the scope.
In Microsoft Windows as DHCP client, you usually make following setup on your network card (TCP/IP) properties to retrieve IP address and other configurations automatically from this server.
When you switch on your computer, it sends a broadcast packet with DHCP request to the network. This packet will be picked up by the server, which subsequently allocates an IP address and other configurations to the computer.
Once the computer is allocated IP address, you can type ipconfig /all on command prompt window to check the network information. It shows you the detail TCP/IP configuration (IP Address, Subnet Mask, Default Gateway, DNS server). It also shows you the DHCP server is 192.168.1.1, which is my network router in this case (the router I’m using can work as DHCP server). The IP address is leased for one day by checking Lease Obtained and Lease Expired information.











dhcpd.conf File
You can define your server configuration parameters in the dhcpd.conf file which may be located in the /etc the /etc/dhcpd or /etc/dhcp3 directories depending on your version of Linux.
Note: The skeleton dhcp.conf file that is created when you install the package may vary in its completeness. In Ubuntu / Debian, the skeleton dhcpd.conf file is extensive with most of the commands deactivated with a # sign at the beginning. In Fedora / RedHat / CentOS an extensive sample is also created with activated commands. It is found in the following location which you can always use as a guide.
/usr/share/doc/dhcp*/dhcpd.conf.sample
Note: The dhcpd.conf configuration file formats in Debian / Ubuntu and Redhat / Fedora are identical.
Here is a quick explanation of the dhcpd.conf file: Most importantly, there must be a subnet section for each interface on your Linux box. 

ddns-update-style interim
ignore client-updates

subnet 192.168.1.0 netmask 255.255.255.0 {
   # The range of IP addresses the server
   # will issue to DHCP enabled PC clients
   # booting up on the network
   range 192.168.1.201 192.168.1.220;
   # Set the amount of time in seconds that
   # a client may keep the IP address
  default-lease-time 86400;
  max-lease-time 86400;
   # Set the default gateway to be used by
   # the PC clients
    option routers 192.168.1.1;
   # Don't forward DHCP requests from this
   # NIC interface to any other NIC
   # interfaces
    option ip-forwarding off;
    # Set the broadcast address and subnet mask
   # to be used by the DHCP clients
   option broadcast-address 192.168.1.255;
  option subnet-mask 255.255.255.0;
    # Set the NTP server to be used by the
   # DHCP clients
  option ntp-servers 192.168.1.100;
   # Set the DNS server to be used by the
   # DHCP clients
  option domain-name-servers 192.168.1.100;
   # If you specify a WINS server for your Windows clients,
   # you need to include the following option in the dhcpd.conf file:
  option netbios-name-servers 192.168.1.100;

   # You can also assign specific IP addresses based on the clients'
   # ethernet MAC address as follows (Host's name is "laser-printer":

  host laser-printer {
      hardware ethernet 08:00:2b:4c:59:23;
     fixed-address 192.168.1.222;
   }
}
#
# List an unused interface here
#
subnet 192.168.2.0 netmask 255.255.255.0 {
}






































No comments:

Post a Comment