Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP...

19
Department of Computer Engineering, SKNCOE, Pune Assignment No. (A-5) Title DHCP server Roll No. Class T.E. Date Subject Programming Lab IV Signature

Transcript of Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP...

Page 1: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

Assignment

No.(A-5)

Title DHCP server

Roll No.

Class T.E.

Date

Subject Programming Lab IV

Signature

Page 2: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

Assignment no: 5(A4)

Title: DHCP server.

Problem Statement:Installing and configure DHCP server and write a program (C++\Python\Java) to install thesoftware on remote machine.

Prerequisites:Basics of Networking, DHCP details etc.

Objectives:1] To learn concept of DHCP protocol working.2] To study the Installation and configuration of DHCP server

Tools:Operating System: Fedora19.Programming Language: C++\Python\Java.Additional Tool: DHCP server setup

Theory:

WHAT IS DHCP?

Anyone with a basic knowledge of computer networking knows that in order for two hostscommunicate on the same network using TCP/IP model, both hosts need to have an uniqueIP address. There are two ways on how a network host can obtain an IP address.

One way is to manually configure network interface and assign an IP address by hand. Thisis called static configuration which means that host's IP address will never change untilchanged manually again by user or system administrator. If company's network includesmore than 1000 hosts this job of setting up each individual host with static IP address willbecome tiresome and more importantly inefficient.

Another way how to set host's IP address and this is also a solution to the above issue wouldbe to have some way to automatically assign an IP address to any host on the network uponthe request. This is where DHCP (Dynamic Host Configuration Protocol) comes handy.

The DHCP protocol lets a DHCP client to lease network configuration parameters such as anIP address. In fact parameters to lease are not limited to IP address only and they alsoinclude:

Page 3: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

IP addresses and network masks

Domain Names servers ( DNS )

Default Gateways

WINS servers

Syslog hosts

Proxy servers

NTP servers

X Font servers

Syslog hosts

Each host set to obtain an IP address dynamically will upon boot send a DHCP request overthe network ( by definition this is a broadcast of all 1's ) to discover whether there is a DHCPserver available on the network and consequently ask for an network configuration. DHCPclient is then obligated to maintain a communication with DHCP server and renew its IPaddress regularly as dictated by IP address's lease time expiry. In case that DHCP client failsto renew its IP address (disconnection, client is turned off and etc.) its IP address expires andDHCP server is free to lease this IP address to another DHCP client.

DHCP server keeps a record of all leased IP addresses and stores them into a file calleddhcpd.leases which can be found in /var/lib/dhcp directory ( location of this file may varydepending on Linux system in use ). Having such a file allows DHCP server to keep track ofall IP address leases even after its reboot or power failure.

Here are some advantages of having a DHCP server connected to network:

No IP address conflicts. DHCP can guarantee that all hosts on the network will have uniqueIP address. DHCP server keeps a record of all IP addresses assigned and cross reference themwith host's MAC addresses.

Based on the MAC address DHCP allows for a fixed parameter configuration for a specifichost

Efficiency with minimum local client configuration

DHCP protocol:

The Dynamic Host Configuration Protocol (DHCP)is a client/server protocol designed toprovide the four pieces of information for a diskless computer or a computer that is bootedfor the first time. DHCP is a successor to BOOTP and is backward compatible with it.Although BOOTP is considered deprecated, there may be some systems that may still useBOOTP for host configuration. The part of the discussion in this chapter that does not dealwith the dynamic aspect of DHCP can also be applied to BOOTP.

Page 4: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

DHCP operation

The DHCP client and server can either be on the same network or on different networks.

Same Network

The operation is described as follows:

The DHCP server issues a passive open command on UDP port number 67 and waitsfor a client.

A booted client issues an active open command on port number 68 (this number will beexplained later). The message is encapsulated in a UDP user datagram, using the destinationport number 67 and the source port number 68. The UDP user datagram, in turn, isencapsulated in an IP datagram. The reader may ask how a client can send an IP datagramwhen it knows neither its own IP address (the source address) nor the server’s IP address (thedestination address). The client uses all 0s as the source address and all 1s as the destinationaddress.

The server responds with either a broadcast or a unicast message using UDP source portnumber 67 and destination port number 68. The response can be unicast because the serverknows the IP address of the client. It also knows the physical address of the client, whichmeans it does not need the services of ARP for logical to physical address mapping. However,some systems do not allow the bypassing of ARP, resulting in the use of the broadcastaddress.

Page 5: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

DHCP message formats:

The DHCP employs a connectionless service model, using the User Datagram Protocol(UDP). It is implemented with two UDP port numbers for its operations which are the sameas for the BOOTP protocol. UDP port number 67 is the destination port of a server, and UDPport number 68 is used by the client.

DHCP operations fall into four phases: server discovery, IP lease offer, IP request, and IPlease acknowledgment. These stages are often abbreviated as DORA for discovery, offer,request, and acknowledgment.

The DHCP operation begins with clients broadcasting a request. If the client and server areon different subnets, a DHCP Helper or DHCP Relay Agent may be used. Clients requestingrenewal of an existing lease may communicate directly via UDP unicast, since the clientalready has an established IP address at that point.

DHCP discovery

The client broadcasts messages on the network subnet using the destination address255.255.255.255 or the specific subnet broadcast address. A DHCP client may also requestits last-known IP address. If the client remains connected to the same network, the servermay grant the request. Otherwise, it depends whether the server is set up as authoritative ornot. An authoritative server denies the request, causing the client to issue a new request. Anon-authoritative server simply ignores the request, leading to an implementation-dependenttimeout for the client to expire the request and ask for a new IP address.

Page 6: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

Page 7: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

DHCP offer

When a DHCP server receives a DHCPDISCOVER message from a client, which is an IPaddress lease request, the server reserves an IP address for the client and makes a lease offerby sending a DHCPOFFER message to the client. This message contains the client's MACaddress, the IP address that the server is offering, the subnet mask, the lease duration, and theIP address of the DHCP server making the offer.

The server determines the configuration based on the client's hardware address as specified inthe CHADDR (client hardware address) field. Here the server, 192.168.1.1, specifies theclient's IP address in the YIADDR (your IP address) field.

Page 8: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

Page 9: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

DHCP request

In response to the DHCP offer, the client replies with a DHCP request, broadcast to theserver, requesting the offered address. A client can receive DHCP offers from multipleservers, but it will accept only one DHCP offer. Based on required server identificationoption in the request and broadcast messaging, servers are informed whose offer the clienthas acceptedWhen other DHCP servers receive this message, they withdraw any offers thatthey might have made to the client and return the offered address to the pool of availableaddresses.

Page 10: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

Page 11: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

DHCP acknowledgement

When the DHCP server receives the DHCPREQUEST message from the client, the33configuration process enters its final phase. The acknowledgement phase involves sendinga DHCPACK packet to the client. This packet includes the lease duration and any otherconfiguration information that the client might have requested. At this point, the IPconfiguration process is completed.

The protocol expects the DHCP client to configure its network interface with the negotiatedparameters.

After the client obtains an IP address, it should probe the newly received address (e.g. withARP Address Resolution Protocol) to prevent address conflicts caused by overlappingaddress pools of DHCP servers.

Page 12: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

Page 13: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

DHCP SERVER INSTALLATION

Standard DHCP server implementation available in various Linux distributions is an Opensource version maintained by ISC (Internet System Consortium). There are currently 3 majorversions 2, 3, 4 where version 3 supports backup servers, and version 4 supports IPv6. Thisarticle deals only with ISC DHCP v3.

Use the following commands to install DHCP on your Linux server:

Debian and Ubuntu:

# apt-get install dhcp3-server

Redhat & Fedora:

# yum install dhcp

BASIC DHCP CONFIGURATION

By default DHCP server configuration does not include any subnets on which DHCP servershould lease IP addresses. Therefore, depends on your Linux system you may get a followingerror message when you attempt to start DHCP with default dhcpd.conf configuration file.

Starting ISC DHCP server: dhcpdcheck syslog for diagnostics. ... failed!

Checking a log files and particularly /var/log/syslog reveals this problem in more detail:

No subnet declaration for eth0 (some IP address).

As it happens very often your server may be connected to multiple network subnets. In orderto start DHCP server at least one subnet must be defined in DHCP configuration file/etc/dhcp/dhcpd.conf.

Installing the dhcp package creates a file, /etc/dhcp/dhcpd.conf, which is merely an emptyconfiguration file. As root, issue the following command:

~]# cat /etc/dhcp/dhcpd.conf

#

Page 14: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

# DHCP Server Configuration file.

# see /usr/share/doc/dhcp/dhcpd.conf.example

# see dhcpd.conf(5) man page

#

The example configuration file can be found at /usr/share/doc/dhcp/dhcpd.conf.example. Youshould use this file to help you configure /etc/dhcp/dhcpd.conf, which is explained in detailbelow.

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

Configuration File

The first step in configuring a DHCP server is to create the configuration file that stores thenetwork information for the clients. Use this file to declare options for client systems.

The configuration file can contain extra tabs or blank lines for easier formatting. Keywordsare case-insensitive and lines beginning with a hash sign (#) are considered comments.

There are two types of statements in the configuration file:

Parameters — State how to perform a task, whether to perform a task, or what networkconfiguration options to send to the client.

Declarations — describe the topology of the network, describe the clients, provide addressesfor 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 optionscontrol DHCP options; whereas, parameters configure values that are not optional or controlhow the DHCP server behaves.

Parameters (including options) declared before a section enclosed in curly brackets ({ }) areconsidered global parameters. Global parameters apply to all the sections below it.

Restart the DHCP Daemon for the Changes to Take Effect

If the configuration file is changed, the changes do not take effect until the DHCP daemon isrestarted with the command systemctl restart dhcpd.

NOTE: if your server has access to more than one subnet, DHCP requires all subnets to bedefined even though there isn't immediate intention to enable DHCP service on that subnet.

Below is the simplest example of DHCP configuration file:

subnet 10.1.1.0 netmask 255.255.255.0 {

Page 15: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

range 10.1.1.3 10.1.1.254;

}

subnet 192.168.0.0 netmask 255.255.0.0 {

}

This configuration file instructs DHCP server to listen for DHCP client requests on subnet10.1.1.0 with netmask 255.255.255.0. Furthermore, it will assign IP addresses in range10.1.1.3 - 10.1.1.254. It also defines an empty definition of subnet with network ID192.168.0.0.

Alter above code with your subnet and insert it into /etc/dhcp/dhcpd.conf. When ready restartyour DHCP server with (restart command may vary):

# /etc/init.d/isc-dhcp-server restart

DHCP DEFAULT AND MAX LEASE TIME

At this point we can add to our DHCP configuration another setting and that is to set defaultand max lease time expiry.

default-lease-time is a value in seconds in which a leased IP address expiry will be set to ifDHCP client does not ask for any other specific expiry lease time

max-lease-time is a value in seconds which defines a maximum expiry time for an IP addressleased by DHCP server

default-lease-time 600;

max-lease-time 7200;

subnet 10.1.1.0 netmask 255.255.255.0 {

range 10.1.1.3 10.1.1.254;

}

subnet 192.168.0.0 netmask 255.255.0.0 {

Page 16: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

}

Advantages of DHCP:

DHCP has a number of advantages:

• There is no need to manually configure each client with an IP address.

• You don't need to keep a record of the IP addresses that you have assigned.

• You can automatically assign a new IP address if you move a client to a differentsubnet.

• You can release the IP address of a computer that is offline and reassign the address toanother computer.

• Address duplication is eliminated as DHCP automatically tracks IP addressassignments.

• The DHCP server can detect unauthorised DHCP servers on the network.

Conclusion:

By this assignment we have studied the DHCP protocol concepts and we have installed andconfigured DHCP server.

Page 17: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

CODE:

/* For Dhcp:

Insatll Open ssh server/client service on both machines, host and remote on remotemeachine install dhcp.

From host machine edit dhcpd.conf on remote machine

Inside dhcp we define ip range, subnet mask, default gateway, router and so on.

Run program, enter ip of remote machine, username of remote machine, pass of remotemachine */

import pxsshimport getpasstry:s = pxssh.pxssh()hostname = raw_input('hostname: ')username = raw_input('username: ')password = getpass.getpass('password: ')s.login (hostname, username, password)

s.sendline ('echo "ddns-update-style interim;" >/etc/dhcp/dhcpd.conf') # run a commands.prompt() # match the promptprint s.before # print everything before the prompt.

s.sendline ('echo "authoritative;" >> /etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "default-lease-time 600;" >>/etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "max-lease-time 7200;" >> /etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "subnet 192.168.2.0 netmask 255.255.255.0 { " >>/etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "option subnet-mask 255.255.255.0;" >>/etc/dhcp/dhcpd.conf')s.prompt()print s.before

Page 18: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

s.sendline ('echo "option broadcast-address 192.168.2.255;" >>/etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "option routers 192.168.2.1;" >>/etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "option domain-name-servers 8.8.8.8, 192.168.2.1;">> /etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "range 192.168.2.51 192.168.2.100;" >>/etc/dhcp/dhcpd.conf')s.prompt()print s.before

s.sendline ('echo "}" >> /etc/dhcp/dhcpd.conf')s.prompt()print s.before

# Assign Interfaces to /etc/default/isc-dhcp-server file s.sendline ('echo "INTERFACES=eth0" > /etc/default/isc-dhcp-

server') s.prompt() print s.before

s.sendline ('route add -net 192.168.2.0 netmask 255.255.255.0 gw192.168.2.1')

s.prompt() print s.before

s.sendline ('dhcpd -cf /etc/dhcp/dhcpd.conf -pf/var/run/dhcpd.pid eth0')

s.prompt() print s.before

s.sendline ('/etc/init.d/isc-dhcp-server start') s.prompt() print s.before

s.logout()

except pxssh.ExceptionPxssh, e:print "pxssh failed on login."

Page 19: Assignment - E-STUDYthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/cn_group_a5.pdfThe DHCP employs a connectionless service model, using the User Datagram Protocol (UDP ).

Department of Computer Engineering, SKNCOE, Pune

print str(e)Output:

Client Side

student@student-OptiPlex-3010:~$ sudo python ssh_dhcp.pyhostname: 172.25.24.125username: studentpassword:echo "ddns-update-style interim;" > /etc/dhcp/dhcpd.conf

echo "authoritative;" >> /etc/dhcp/dhcpd.conf

echo "default-lease-time 600;" >> /etc/dhcp/dhcpd.conf

echo "max-lease-time 7200;" >> /etc/dhcp/dhcpd.conf

.conf"subnet 192.168.2.0 netmask 255.255.255.0 { " >> /etc/dhcp/dhcpd

echo "option subnet-mask 255.255.255.0;" >> /etc/dhcp/dhcpd.conf

fcho "option broadcast-address 192.168.2.255;" >> /etc/dhcp/dhcpd.con

echo "option routers 192.168.2.1;" >> /etc/dhcp/dhcpd.conf

dhcpd.confon domain-name-servers 8.8.8.8, 192.168.2.1;" >> /etc/dhcp/

echo "range 192.168.2.51 192.168.2.100;" >> /etc/dhcp/dhcpd.conf

echo "}" >> /etc/dhcp/dhcpd.conf

Server SideHttpd.conf file

ddns-update-style interim;authoritative;default-lease-time 600;max-lease-time 7200;subnet 192.168.2.0 netmask 255.255.255.0 {option subnet-mask 255.255.255.0;option broadcast-address 192.168.2.255;option routers 192.168.2.1;option domain-name-servers 8.8.8.8, 192.168.2.1;range 192.168.2.51 192.168.2.100;}