Installing DHCP server and adding static routes to IP leases for CentOS

1. Install dhcp server:

 [root@linux~]# yum install dhcp -y 

2. Designate ethernet port to the server. In this example, **eth0** is the designated eth port:

 [root@linux ~]# vi /etc/sysconfig/dhcpd
# Command line options here
DHCPDARGS=eth0 

3. Copy the following dhcpd.conf file to **/etc/dhcp/** by first creating a new dhcpd.conf file. This sample file is the most basic file to get your DHCP server up and running. It basically sets default route to 10.21.25.1, DHCP IP ranges from 10.21.25.10- 10.21.25.20 with a default lease time of 1 hour. Modify as you wish:

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

# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

default-lease-time 3600;
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;

# Push static routes with ISC DHCP server. Uncomment before using static routes
#  option rfc3442-classless-static-routes code 121 = array of integer 8;
#  option ms-classless-static-routes code 249 = array of integer 8;

subnet 10.21.25.0 netmask 255.255.255.0 {
  option routers 10.21.25.1;

# Push static routes with ISC DHCP server to leases tables.
# Example below sets 10.21.25.0 255.255.255.0 10.21.25.1 10.21.25.x
# and 0.0.0.0 0.0.0.0 10.21.25.1 10.21.25.x
#  option rfc3442-classless-static-routes 24, 10, 21, 25, 10, 21, 25, 1, 0, 10, 21, 25, 1;
#  option ms-classless-static-routes 24, 10, 21, 25, 10, 21, 25, 1, 0, 10, 21, 25, 1;

# set leases ip range
  range 10.21.25.10 10.21.25.20;
}

4. start your DHCP server:

 [root@linux ~]# service dhcpd start
Starting dhcpd:                                            [  OK  ]

… you are done! *Remember to restart your dhcpd service for every changes you make to the dhcpd.conf file.*

Once your client is connected to your DHCP server, your should see something like this on your client PC:
![ip.JPG](/site_media/media/1701a3b0085f1.JPG)

You can also check that your server is serving IP properly by typing:

 [root@linux ~]# cat /var/lib/dhcpd/dhcpd.leases

In the following result example, it shows that I have 2 clients connected, one network card is given an IP of 10.21.25.10 and the other, 10.21.25.11. I did a **ipconfig /release myNIC** and **ipconfig /renew myNIC** on my Windows 7 PC, so you notice that there are 2 instances of myNIC lease.

# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.1.1-P1

lease 10.21.25.10 {
  starts 3 2014/07/09 21:09:10;
  ends 3 2014/07/09 22:09:10;
  tstp 3 2014/07/09 22:09:10;
  cltt 3 2014/07/09 21:09:10;
  binding state free;
  hardware ethernet 00:ab:4f:da:ac:9f;
  uid "\001\000\036O\332\254\234";
}
lease 10.21.25.11 {
  starts 4 2014/07/10 17:20:46;
  ends 4 2014/07/10 18:20:46;
  tstp 4 2014/07/10 18:20:46;
  cltt 4 2014/07/10 17:20:46;
  binding state active;
  next binding state free;
  hardware ethernet bc:30:5b:65:3c:71;
  uid "\001\2740[\211<q";
  client-hostname "myNIC";
}
server-duid "\000\001\000\001\033O=T\000\023r\372\320?";

lease 10.21.25.11 {
  starts 4 2014/07/10 17:23:14;
  ends 4 2014/07/10 18:23:14;
  cltt 4 2014/07/10 17:23:14;
  binding state active;
  next binding state free;
  hardware ethernet bc:30:5b:65:3c:71;
  uid "\001\2740[\211<q";
  client-hostname "myNIC";
}

**Set Static Routes to your leased IP**

To set static route to your leased IP, uncomment the following lines in the dhcpd.conf file I showed above. the **option rfc3442…** line is for Linux-based systems, and the **option ms…** line is for MS-based systems. Examples given should help you with the modification of the lines:

# Push static routes with ISC DHCP server. Uncomment before using static routes, and DON'T MODIFY ANYTHING HERE
  option rfc3442-classless-static-routes code 121 = array of integer 8;
  option ms-classless-static-routes code 249 = array of integer 8;

subnet 10.21.25.0 netmask 255.255.255.0 {
  option routers 10.21.25.1;

# Push static routes with ISC DHCP server to leases tables.
# Example below sets 10.21.25.0 255.255.255.0 10.21.25.1 10.21.25.x
# and 0.0.0.0 0.0.0.0 10.21.25.1 10.21.25.x
  option rfc3442-classless-static-routes 24, 10, 21, 25, 10, 21, 25, 1, 0, 10, 21, 25, 1;
  option ms-classless-static-routes 24, 10, 21, 25, 10, 21, 25, 1, 0, 10, 21, 25, 1;

![route.JPG](/site_media/media/b0c8c4d4085e1.JPG)

**Start DHCP server on boot**

Do this so that your DHCP server would start automatically:

 [root@linux ~]# chkconfig --add dhcpd 

Check that it is added to your bootup list:

 [root@linux ~]# chkconfig --list 

Turn it on!

 [root@linux ~]# chkconfig dhcpd on

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s