DHCP (Dynamic Host Configuration Protocol) is a service running on port number 67 which assigns IP addresses to computers in a network. Setting up a DHCP server requires you to specify a range of IP addresses which will be assigned to the computers on the network, a gateway address which is the address of the router or modem and a DNS server address which will be used to resolve hostnames. Doing this in Linux is a simple matter of installing an application and editing a .conf file.
Install the DHCP RPM
Before installing make sure it isn’t already installed. Go to the command line terminal and type the command rpm -qa dhcp* it shouldn’t display anything. If it displays dhcpclient or something similar it this you don’t have the server version installed. If you just see dhcp-<version-number>.el5 then you have it installed already so skip this step and move to the next part.
If you don’t have it installed you can install it through the repository if you have them configured properly. Type yum list dhcp* and the dhcp.i386 package will be listed under available packages. To install it type yum install dhcp.i386 and the package will be downloaded from the repository (if it a HTTP/FTP repository) and installed. If you have the RPM file for dhcp you can install by navigating to the folder containing the RPM file and issue the command rpm -ivh dhcp-<version-details>.rpm now the dhcp service will be installed. To check if it has been successfully installed type rpm -qa dhcp this will list the installed packages named dhcp
Copy and rename dhcpd.conf to /etc
After installation a file named dhcpd.conf is created inside the /etc directory this is the configuration file for the DHCP service. But if you open it might contain a line telling you to see /usr/share/doc/dhcp*/dhcpd.conf.sample this is the location of a sample file which has to be copied to renamed to the /etc directory. To do both in one command type cp /usr/share/doc/dhcp-<version-number>/dhcpd.conf.sample /etc/dhcpd.conf it will ask whether you what to overwrite the dhcpd.conf located inside /etc type y to do it.
Edit the /etc/dhcpd.conf file
Logged as a root user type vi /etc/dhcpd.conf press i to enter into insert mode, you’ll have to configure the following lines.
subnet 192.168.0.0 netmask 255.255.255.0
option routers 192.168.0.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 192.168.1.1;
range dynamic-bootp 192.168.0.128 192.168.0.254;
default-lease-time 21600;
max-lease-time 43200;
On line number 1 set the subnet for the domain to match the IP class you are going to use. Set the IP address of the router/gateway on your network and set the subnet mask IP. Note that unlike windows which autocompletes the subnet mask according to the IP address entered Linux requires you to manually specify it so make sure you specify the correct subnet mask according to the class of the IP address. The option domain-name-servers is where you should enter the DNS servers for your domain. If you wish to enter more than one DNS server separate each IP address with commas. The range dynamic-bootp is the place to specify the range of IP addresses that should be assigned to the devices on the network. The starting IP and the ending IP should be separated by a space. The final setting is the default-lease-time and the max-lease-time which should be specified in seconds. This tells the devices on the network after what time duration the IP address should be renewed. Press ESC then type :wq to save the file and exit.
Assign a static IP to the DHCP server
The DHCP server should be assigned a static IP so that it can serve to the requests of the clients. From the command line type vi /etc/sysconfig/network-scripts/ifcfg-eth0 and enter values for IPADDR, GATEWAY, NETMASK and DNS1. Save the file and restart the network service by typing service network restart for the changes to take place. type ifconfig to see whether the changes have been applied.
Start and test the DHCP server
To start the DHCP daemon type service dhcpd start to make it start along with the OS type chkconfig dhcpd on. Now power on another machine on the network and see whether IP/Netmask/Gateway/DNS Addresses are assigned properly by typing ipconfig if the client runs windows or ifconfig on Linux.
Assign a fixed IP to certain machines using DHCP
You may sometimes require certain computers on your network to be assigned specific IP addresses. This can be done by specifying the computer’s hostname and MAC address. To find out the hostname type hostname on the command line. To find out the MAC address type ipconfig /all if you’re on windows or ifconfig for Linux. Then add the following code to the end of the /etc/dhcpd.conf file.
host <hostname> {
hardware ethernet <MAC address>;
fixed-address <IP Address>;
}
Replace <hostname>, <MAC Address>, <IP Address> with required computer’s hostname, MAC Address and IP address respectively. Keep adding this line for as many computers you’d like to specify a fixed IP address. Restart the DHCP daemon by typing service dhcpd restart now you’ll see the changes.
If the DHCP service starts but IP is not assigned to any computers it may be because a firewall running on the Linux DHCP Server is blocking port 67. Specify rules for allowing incoming requests to post 67 and it should work properly.
Leave a Reply