Tuesday, October 5, 2010

How to Install and Configure DHCP Server in Ubuntu Server

A DHCP Server assigns IP addresses to client computers. This is very often used in enterprise networks to reduce configuration efforts. All IP addresses of all computers are stored in a database that resides on a server machine.

A DHCP server can provide configuration settings using two methods

Address Pool

This method entails defining a pool (sometimes also called a range or scope) of IP addresses from which DHCP clients are supplied their configuration properties dynamically and on a fist come first serve basis. When a DHCP client is no longer on the network for a specified period, the configuration is expired and released back to the address pool for use by other DHCP Clients.

MAC Address

This method entails using DHCP to identify the unique hardware address of each network card connected to the network and then continually supplying a constant configuration each time the DHCP client makes a request to the DHCP server using that network device.

Install DHCP server in ubuntu

sudo apt-get install dhcp3-server

This will complete the installation.

Configuring DHCP server

If you have two network cards in your ubuntu server you need to select which interface you want to use for DHCP server listening.By default it listens to eth0.

You can change this by editing /etc/default/dhcp3-server file

sudo vi /etc/default/dhcp3-server

Find this line

INTERFACES=”eth0″

Replace with the following line

INTERFACES=”eth1″

Save and exit.This is optional.

Next you need to make a backup copy of /etc/dhcp3/dhcpd.conf file

cp /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.back

Edit /etc/dhcp3/dhcpd.conf file using the following command

sudo vi /etc/dhcp3/dhcpd.conf

Using address pool method

You need to change the following sections in /etc/dhcp3/dhcpd.conf file

default-lease-time 600;
max-lease-time 7200;

option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option routers 192.168.1.254;
option domain-name-servers 192.168.1.1, 192.168.1.2;
option domain-name “yourdomainname.com”;

subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.10 192.168.1.200;
}

save and exit the file

This will result in the DHCP server giving a client an IP address from the range 192.168.1.10-192.168.1.200 . It will lease an IP address for 600 seconds if the client doesn’t ask for a specific time frame. Otherwise the maximum (allowed) lease will be 7200 seconds. The server will also “advise” the client that it should use 255.255.255.0 as its subnet mask, 192.168.1.255 as its broadcast address, 192.168.1.254 as the router/gateway and 192.168.1.1 and 192.168.1.2 as its DNS servers.

Using MAC address method

This method is you can reserver some of the machines or all the machines with fixed ip address.In the following example i am using fixed ip address for server1,server2,printer1 and printer2

default-lease-time 600;
max-lease-time 7200;

option subnet-mask 255.255.255.0;
option broadcast-address 192.168.1.255;
option routers 192.168.1.254;
option domain-name-servers 192.168.1.1, 192.168.1.2;
option domain-name “yourdomainname.com”;

subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.10 192.168.1.200;
}

host server1 {
hardware ethernet 00:1b:63:ef:db:54;
fixed-address 192.168.1.20;
}

host server2 {
hardware ethernet 00:0a:95:b4:d4:b0;
fixed-address 192.168.1.21;
}

host printer1 {
hardware ethernet 00:16:cb:aa:2a:cd;
fixed-address 192.168.1.22;
}

host printer2 {
hardware ethernet 00:0a:95:f5:8f:b3;
fixed-address 192.168.1.23;
}

Now you need to restart dhcp server using the following command

sudo /etc/init.d/dhcp3-server restart

Configure Ubuntu DHCP Client

If you want to configure your ubuntu desktop as DHCP client following this procedure

You need to open /etc/network/interfaces file

sudo vi /etc/network/interfaces

make sure you have the following lines (eth0 is an example)

auto lo eth0
iface eth0 inet dhcp
iface lo inet loopback

Save and exit the file

You need to restart networking services using the following command

sudo /etc/init.d/networking restart

How to find DHCP server IP address

You need to use the following commands

sudo dhclient

or

tail -n 15 /var/lib/dhcp3/dhclient.*.leases

Enjoy ubuntu... ;)

No comments:

kunkun-laptop .... ;)