Tuesday 12 June 2012

Installation and Configuration of Linux DHCP Server




For a cable modem or a DSL connection, the service provider dynamically assigns the IP address to your PC. When you install a DSL or a home cable router between your home network and your modem, your PC will get its IP address from the home router during boot up. A Linux system can be set up as a DHCP server and used in place of the router.
DHCP is not installed by default on your Linux system. It has to be installed by gaining root privileges:
su -
You will be prompted for the root password and you can install DHCP by the command:
yum install dhcp
Once all the dependencies are satisfied, the installation will complete.


Start the DHCP Server
You will need root privileges for enabling, starting, stopping or restarting the dhcpd service:
systemctl enable dhcpd.service
Once enabled, the dhcpd services can be started, stopped and restarted with:
systemctl start dhcpd.service
systemctl stop dhcpd.service
systemctl restart dhcpd.service
or with the use of the following commands if systemctl command is not available:
service dhcpd start
service dhcpd stop
service dhcpd restart
To determine whether dhcpd is running on your system, you can seek its status:
systemctl status dhcpd.service
Another way of knowing if dhcpd is running is to use the 'service' command:
service dhcpd status
Note that dhcpd has to be configured to start automatically on next reboot.

Configuring the Linux DHCP Server
Depending on the version of the Linux installation you are currently running, the configuration file may reside either in /etc/dhcpd or/etc/dhcpd3 directories.
When you install the DHCP package, a skeleton configuration file and a sample configuration file are created. Both are quite extensive, and the skeleton configuration file has most of its commands deactivated with # at the beginning. The sample configuration file can be found in the location /usr/share/doc/dhcp*/dhcpd.conf.sample.
When the dhcpd.conf file is created, a subnet section is generated for each of the interfaces present on your Linux system; this is very important. Following is a small part of the dhcp.conf file:
ddns-update-style interim
ignore client-updates
subnet 192.168.1.0 netmask 255.255.255.0 {
   # The range of IP addresses the server
   # will issue to DHCP enabled PC clients
   # booting up on the network
   range 192.168.1.201 192.168.1.220;
   # Set the amount of time in seconds that
   # a client may keep the IP address
  default-lease-time 86400;
  max-lease-time 86400;
   # Set the default gateway to be used by
   # the PC clients
   option routers 192.168.1.1;
   # Don't forward DHCP requests from this
   # NIC interface to any other NIC
   # interfaces
   option ip-forwarding off;
   # Set the broadcast address and subnet mask
   # to be used by the DHCP clients
  option broadcast-address 192.168.1.255;
  option subnet-mask 255.255.255.0;
   # Set the NTP server to be used by the
   # DHCP clients
  option ntp-servers 192.168.1.100;
   # Set the DNS server to be used by the
   # DHCP clients
  option domain-name-servers 192.168.1.100;
   # If you specify a WINS server for your Windows clients,
   # you need to include the following option in the dhcpd.conf file:
  option netbios-name-servers 192.168.1.100;
   # You can also assign specific IP addresses based on the clients'
   # ethernet MAC address as follows (Host's name is "laser-printer":
  host laser-printer {
      hardware ethernet 08:00:2b:4c:59:23;
     fixed-address 192.168.1.222;
   }
}
#
# List an unused interface here
#
subnet 192.168.2.0 netmask 255.255.255.0 {
}
The IP addresses will need to be changed to meet the ranges suitable to your network. There are other option statements that can be used to configure the DHCP. As you can see, some of the resources such as printers, which need fixed IP addresses, are given the specific IP address based on the NIC MAC address of the device.
For more information, you may read the relevant man pages:
man dhcp-options


Routing with a DHCP Server
When a PC with DHCP configuration boots, it requests for the IP address from the DHCP server. For this, it sends a standard DHCP request packet to the DHCP server with a source IP address of 255.255.255.255. A route has to be added to this 255.255.255.255 address so that the DHCP server knows on which interface it has to send the reply. This is done by adding the route information to the/etc/sysconfig/network-scripts/route-eth0 file, assuming the route is to be added to the eth0 interface:
#
# File /etc/sysconfig/network-scripts/route-eth0
#
255.255.255.255/32 dev eth0
After defining the interface for the DHCP routing, it has to be further ensured that your DHCP server listens only to that interface and to no other. For this the /etc/sysconfig/dhcpd file has to be edited and the preferred interface added to the DHCPDARGS variable. If the interface is to be eth0 following are the changes that need to be made:
# File: /etc/sysconfig/dhcpd


DHCPDARGS=eth0


Testing the DHCP
Using the netstat command along with the -au option will show the list of interfaces listening on the bootp or DHCP UDP port:
netstat -au  | grep bootp
will result in the following:
udp     0         0 192.168.1.100:bootps         *:*
Additionally, a check on the /var/log/messages file will show the defined interfaces used from the time the dhcpd daemon was started:
Feb  24 17:22:44 Linux-64 dhcpd: Listening on LPF/eth0/00:e0:18:5c:d8:41/192.168.1.0/24
Feb  24 17:22:44 Linux-64 dhcpd: Sending on  LPF/eth0/00:e0:18:5c:d8:41/192.168.1.0/24
This confirms the DHCP Service has been installed with success and operating correctly.

No comments:

Post a Comment