Jump to content
Sign in to follow this  
hybrid

Set up a simple DHCP server for a small network

Recommended Posts

A while back, an upgrade to the firmware of the router on my local network (which I wanted to apply to enable WPA2 encryption for the wireless connection) starting causing some strange issues with clients connecting to the network with DHCP. They would get addresses which would work, but when the addresses were attempted to be renewed, the DHCP server in the router would refuse and the client would effectively be booted off the network and unable to access anything.

 

So I decided to set up a DHCP server on my web server machine and switch off the built-in DHCP server on the router. My server would now handle the assignment of addresses, and hopefully not suffer from this annoying bug!

 

So, here's how to configure a basic DHCP server for a local network with dhcpd on CentOS 5.3. This guide should work for pretty much any distribution, though, just the way you install the dhcpd package may be different and start and stop the server may differ somewhat.

 

Install dhcpd

 

First, you need to install the DHCP server package. As root, run:

 

yum install dhcp

 

Just one more thing before we get cracking -- make sure that the server where you are installing this DHCP server uses a static IP address. It obviously doesn't make any sense for it to get its IP address from itself. I'm assuming you have a machine here that is designated as a server that is already set up manually to connect to your local network with a static IP address.

 

Set up the configuration file

 

You will need to know the local IP address range of your local network and which addresses you want to be assigned via DHCP. If you have some machines that are assigned static IP addresses, make sure these addresses fall outside the scope of your DHCP address range, or you could create conflicts.

 

In this example, I am going to assign IP addresses from 192.168.1.64 through 192.168.1.250 as DHCP addresses. When a machine comes on to the network, it will be assigned an address in this range by my new DHCP server. The router IP address on this network is 192.168.1.1.

 

Open the /etc/dhcpd.conf file in your favourite editor, while running as root. Make your file look something like this:

 

#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#

ddns-update-style none;
ignore client-updates;
# these two options are for allowing clients to tell
# update a DNS server when DHCP clients want a DNS hostname.
# I'm switching this off -- I don't need it and it's out of
# the scope of this tutorial!

option domain-name-servers 216.146.35.35, 216.146.36.36;
# place your DNS servers here -- you will probably want to retrieve
# the addresses of the DNS servers you currently use from your router.
# Here I'm using the public DNS servers of the Dyn Internet Guide public
# DNS service. You may prefer your ISP's servers, or OpenDNS or Google Public
# DNS or another service.

default-lease-time 172800; # one day
max-lease-time 604800; # seven days
# you should be fine with these, unless you specifically want to change them!

authoritative;

subnet 192.168.1.0 netmask 255.255.255.0 {
# place your network's IP information here, you should be able to retrieve from
# your router.

 range                    192.168.1.64 192.168.1.250;
 # the range of addresses to be used by DHCP clients
 option routers           192.168.1.1;
 # the IP of your router, in order to get traffic out to the internet!
 option subnet-mask       255.255.255.0
 # again, retrieve from your current settings
 option broadcast-address 192.168.1.255
 # usually the last address on your network, again look at your current settings


}

 

You will obviously need to substitute into that example file the IP addresses on your network, including the subnet, your desired IP address range for DHCP users, the router and your preferred DNS servers. As the comments say, you should be able to retrieve most of this information from your current network setup; so it will probably be on the pages of your router's configuration web page.

 

When you have customised that file with your information, save the file and quit your editor. The DHCP server should now be ready for action.

 

Start the DHCP server and set to run on reboot

 

Now, stop any existing DHCP server you might have enabled on your router. You will probably have to go to the configuration web page of your router and disable the DHCP server option. Two DHCP servers on the same network will likely conflict unless set up properly to coexist!

 

Note that while there is no DHCP server on your network, any machine that joins won't be able to get an IP address and therefore will have trouble doing anything. Best do this at a quiet time when no-one is likely to be switching on or restarting their computer or otherwise reconnecting to your network.

 

Now switch on your new DHCP server if it isn't already. As root:

 

/etc/init.d/dhcpd start

 

You may want to use something 'Repair Connection' or a forced network refresh on one of your client computers to test that the new DHCP server is giving out addresses properly. Check the network properties on that machine and you should notice it has an address in the right range. To make sure that your router and DNS server addresses are right, connect to the internet and verify you can access your favourite websites.

 

If something goes wrong, use:

 

/etc/init.d/dhcpd stop
chkconfig dhcpd off

 

to turn off your DHCP server (and prevent it from running again on reboot) and then you can re-enable the router's DHCP server option and be back at square one.

 

If all seems good and your new DHCP server is giving out the right addresses, you can set dhcpd to run at startup:

 

chkconfig dhcpd on

 

From now on, your new DHCP server on Linux will be responsible for dishing out network addresses to clients.

 

Just remember that this machine where the DHCP server is installed must be switched on in order for the network to function properly. This machine ideally should be left on all the time (or at least all the time that your router is switched on), so that computers on the network can get access to the network properly.

 

It is also a good idea to know how to configure one of your machines with a static IP address, so that if all else fails, you can log back into the router and switch on its DHCP server option again.

  • Like 1

Share this post


Link to post
Share on other sites

great stuff Peter, thanks for sharing :)

Share this post


Link to post
Share on other sites
Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...