Sunday 14 April 2013

How to Setup a DHCP Server on Red Hat/Centos 6.x


How to Setup a DHCP Server on Red Hat/Centos 6.x

DHCP stands for (Host Configuration Protocol). It is used to assign IP addresses automatically at boot time to network clients.
There are basically two ways of assigning IPs to Hosts or devices on any given network; manually or by using an automated method (DHCP Server).

This is my opinion based on past experience.
I think if your network is relatively small, between 10-50 hosts/devices, I would recommend using static IPs instead of a DHCP server. There are pros and cons in doing so.

For example if you need resiliency for your network you will need to start to think about redundancy for your DHCP server. There are options, but things start to become unnecessary complex. I know some sys admins; they use DHCP for everything, including servers. I just think it is not a good idea.

Package requirement
      ·         dhcp.x86_64
      ·         dhcp-common.x86_64

Installing packages
[root@centos63 ~]# yum install dhcp.x86_64 dhcp-common.x86_64 –y

Start-up DHCP service at boot time
[root@centos63 ~]# chkconfig --level 2345 dhcpd on
 
Use the sample configuration to start with

[root@centos63 ~]# vi /etc/dhcp/dhcpd.conf

Note: Copy and paste the lines below:

# Sample DHCP generated by Renato de Oliveira
# http://ukaying.blogspot.co.uk
# option definitions (Domain Name)
option domain-name "adlinux.int";

# Lease time and expiration
default-lease-time 600;
max-lease-time 7200;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# DHCP netwok and mask decalaration
subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.100 192.168.1.160;
  option routers 192.168.1.1;
  option domain-name-servers "192.168.1.22";
  option domain-name "adlinux.int";
}

# Fixed IP addresses can also be specified for hosts.  
host linadws01 {
  hardware ethernet 00:0c:29:eb:dc:90;
  fixed-address 192.168.1.160;
}

Note: Make sure you change this file to suit your needs. For example, remember to change Domain Name, IP Addresses, and Name Servers etc.

Start the DHCP Server Service
[root@centos63 ~]# service dhcpd start

Note: If the service fails to start, check /var/log/messages, it will give you a very good idea on what is wrong. Most the time it is syntax or typos.

Testing

On another Linux Server, change the file /etc/sysconfig/network-scripts/ifcfg-eth0 to use DHCP, see below:

[root@centos63 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=”eth0”
BOOTPROTO=dhcp
ONBOOT=yes

Reboot the Client

 
Watch the logs on the server

[root@centos63 ~]# tail –f /var/log/messages

You should see an output similar to the one below:

Apr 14 17:26:44 centos63 dhcpd: DHCPDISCOVER from 00:0c:29:eb:dc:90 via eth0
Apr 14 17:26:44 centos63 dhcpd: DHCPOFFER on 192.168.1.160 to 00:0c:29:eb:dc:90 via eth0
Apr 14 17:26:44 centos63 dhcpd: Dynamic and static leases present for 192.168.1.160.
Apr 14 17:26:44 centos63 dhcpd: Remove host declaration linadws01 or remove 192.168.1.160
Apr 14 17:26:44 centos63 dhcpd: from the dynamic address pool for 192.168.1.0/24
Apr 14 17:26:44 centos63 dhcpd: DHCPREQUEST for 192.168.1.160 (192.168.1.34) from 00:0c:29:eb:dc:90 via eth0
Apr 14 17:26:44 centos63 dhcpd: DHCPACK on 192.168.1.160 to 00:0c:29:eb:dc:90 via eth0

Still on the client, check:

 1.    Its IP address

[root@centos63 ~]# ifconfig

2.    Check its default gateway

[root@centos63 ~]# route –n

3.  Check the /etc/resolv.conf

[root@centos63 ~]#cat /etc/resolv.conf
; generated by /sbin/dhcpclient-script
search adlinux.int
nameserver 192.168.1.22

If you can confirm the results above, then you have successfully setup a DHCP server for your network. If you have problems, just keep an eye on /var/log/messages.

Hope you enjoyed this post.

By Renato de Oliveira  

2 comments:

  1. No subnet declaration for eth0 (no IPv4 addresses).
    Jul 15 11:58:14 server dhcpd: ** Ignoring requests on eth0. If this is not what
    Jul 15 11:58:14 server dhcpd: you want, please write a subnet declaration
    Jul 15 11:58:14 server dhcpd: in your dhcpd.conf file for the network segment
    Jul 15 11:58:14 server dhcpd: to which interface eth0 is attached. **

    ReplyDelete
  2. Hi, I don't have any info about your server or system.
    If you could provide me with:
    1. OS Linux Distribution
    2. How many NICs do you have on your server
    3. can you run: tail -n 50 /var/log/messages and post the whole output

    ReplyDelete