How To Install DHCP Server In CentOS And Ubuntu
What is DHCP?
DHCP stands for Dynamic Host Configuration Protocol.
DHCP is a standardized network protocol used on Internet Protocol
networks for dynamically distributing network configuration parameters,
such as IP addresses for interfaces and services. DHCP Server
can be any server (Linux or Windows) that is used to distribute IP
addresses automatically to the clients in the network. Since, DHCP
Server assigns IP addresses automatically to all systems, a system or
Network administrator need not to assign IP addresses manually to every
single machine in the network. DHCP is opt for system or Network
administrator who is managing thousands of systems.
In
this tutorial, let us see how to install and configure DHCP Server in
CentOS and Ubuntu systems. For the purpose of this tutorial, I will be
using the following three systems:
- CentOS 7 64bit Minimal server (DHCP Server)
- Ubuntu 15.04 64bit Minimal server (DHCP Server)
- Ubuntu 14.04 Desktop (DHCP Client)
A note of warning:
Do not use two or more DHCP servers at the same time in your network.
The client systems might not be able to get IP addresses from the
multiple DHCP servers and it leads to IP address conflict issue. If your
Router or Switch has DHCP feature enabled by default, you need to turn
it off too.
More importantly, you must a assign a static IP address to your DHCP server’s network interface card.
1. Install DHCP Server in CentOS
First let us see how to
install and configure DHCP server in CentOS 7 64bit. The same steps will
work on CentOS 6.x and other older versions.
Log in as root user.
To install DHCP server on CentOS system, run:
yum install dhcp
1.1 Configuration
In CentOS 6.x systems, we have to assign which interface you want your DHCP server to run on in /etc/sysconfig/dhcpd file. In my case, I have only one Interface on my system (eth0), so I assigned eth0.
WARNING:
This file is NOT used anymore in CentOS 7.x systems. If you are here to
restrict what interfaces should dhcpd listen on, be aware that dhcpd
listens *only* on interfaces for which it finds subnet declaration in
dhcpd.conf. It means that explicitly enumerating interfaces also on
command line should not be required in most cases.
If you use CentOS 6.x system, edit file /etc/sysconfig/dhcpd,
vi /etc/sysconfig/dhcpd
Assign the network interface:
# Command line options here
DHCPDARGS=eth0
Save and close the file.
Then, copy the sample dhcp configuration file to /etc/dhcp/ directory.
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
Now, edit dhcpd.conf file,
vi /etc/dhcp/dhcpd.conf
Make the changes as shown below.
Set the domain name and domain-name servers:
[...]
# option definitions common to all supported networks...
option domain-name "unixmen.local";
option domain-name-servers server.unixmen.local;
[...]
If this DHCP server is the official DHCP server for the local network, you should uncomment the following line:
[...]
authoritative;
[...]
Define the sunbet, range of ip addresses, domain and domain name servers like below:
[...]
# A slightly different configuration for an internal subnet.
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.20 192.168.1.30;
option domain-name-servers server.unixmen.local;
option domain-name "unixmen.local";
option routers 192.168.1.1;
option broadcast-address 192.168.1.255;
default-lease-time 600;
max-lease-time 7200;
}
[...]
If you want to assign a fixed
IP address to your client, you should enter it’s MAC id and the IP
address in the following directive. For example, I want to assign a
fixed IP address 192.168.1.15 to my Ubuntu client, hence I modified the following directive as shown below.
[...]
host ubuntu-client {
hardware ethernet 00:22:64:4f:e9:3a;
fixed-address 192.168.1.15;
}
[...]
After making all the changes
you want, save and close the file. Be mindful that if you have another
unused entries on the dhcpd.conf file, comment them. Otherwise, you’ll
have issues while starting dhcpd service.
Now, start the dhcpd service and make it to start automatically on every reboot.
On CentOS 7.x systems:
systemctl enable dhcpd
systemctl start dhcpd
On CentOS 6.x systems:
service dhcpd start
chkconfig dhcpd on
That’s it. Now, jump to the ‘Configure DHCP Clients’ section and configure your clients to get IP addresses automatically from the DHCP server.
Some of you might want to setup DHCP server in Ubuntu systems. If you one of them, then refer the following section.
2. Install DHCP Server in Ubuntu
Let us see how to install and configure DHCP server in Ubuntu 15.04 64 bit server.
To install DHCP server on Ubuntu 15.04, enter the following command:
sudo apt-get install isc-dhcp-server
2.1 Configuration
DHCP
server configuration is not that difficult. First, we have to assign on
what interfaces should the DHCP server (dhcpd) serve DHCP requests. In
my case, I have only one Interface on my system (eth0), so I assigned eth0.
To do that, edit file /etc/default/isc-dhcp-server,
sudo vi /etc/default/isc-dhcp-server
Assign the network interface:
[...]
INTERFACES="eth0"
Save and close the file.
Now, edit dhcpd.conf file,
sudo vi /etc/dhcp/dhcpd.conf
Make the changes as shown below.
Set the domain name and domain-name servers:
[...]
# option definitions common to all supported networks...
option domain-name "unixmen.local";
option domain-name-servers server.unixmen.local;
[...]
If this DHCP server is the official DHCP server for the local network, you should uncomment the following line:
[...]
authoritative;
[...]
Define the sunbet, range of ip addresses, domain and domain name servers like below:
[...]
# A slightly different configuration for an internal subnet.
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.20 192.168.1.30;
option domain-name-servers server.unixmen.local;
option domain-name "unixmen.local";
option routers 192.168.1.1;
option broadcast-address 192.168.1.255;
default-lease-time 600;
max-lease-time 7200;
}
[...]
If you want to assign a fixed
IP address to your client, you should enter it’s MAC id and the IP
address in the following directive. For example, I want to assign a
fixed IP address 192.168.1.15 to my Ubuntu client, therefore I modified the following directive as shown below.
[...]
host ubuntu-client {
hardware ethernet 00:22:64:4f:e9:3a;
fixed-address 192.168.1.15;
}
[...]
After making all the changes you want, save and close the file. Be mindful that if you have unused entries on the dhcpd.conf file, comment all of them. Otherwise, you’ll get issues while starting dhcp service.
Now, restart dhcp service:
In Ubuntu 15.04:
sudo systemctl restart isc-dhcp-server
In Ubuntu 14.04 and older systems:
sudo service isc-dhcp-server restart
Likewise, you can start/stop dhcp service as shown below:
In Ubuntu 15.04 systems:
sudo systemctl start isc-dhcp-server
sudo systemctl stop isc-dhcp-server
In Ubuntu 14.04 and older systems:
sudo service isc-dhcp-server start
sudo service isc-dhcp-server stop
3. Configure DHCP Clients
Now, go to the client configuration network settings and change the IP settings to Automatic (DHCP).
Here is my Lubuntu 14.04 settings:
Restart the network or reboot the client system to get IP address automatically from the DHCp server.
Now, you should see the IP address has been automatically assigned to the clients from the DHCP server.
Run the following command from the client system Terminal:
sudo ifconfig
Sample output:
As you see in the above picture, My Ubuntu 14.04 desktop system which has MAC id 00:22:64:4f:e9:3a got a fixed IP address ( 192.168.1.15 ) from the DHCP server.
That’s it. DHCP server is up and ready.
Cheers!