Install a DHCP Server with a Shell Script (Linux Automation — Part II)
Three days ago, I did a tutorial on how to automate a DNS server on a Linux machine. Today’s tutorial will be on automating a DHCP server.
This tutorial will not require as many configurations as the DNS server, so it should be easier.
It’ll only require knowledge of how subnetting works and I will offer some for those who need to refresh their memory.
This tutorial series will be broken down into explanations of each section of the scripts, screenshots of terminal commands, before-and-after screenshots and the complete scripts.
The script starts off with the obligatory shebang and an OS update.
Some of the commands used in the previous tutorial will be used here.
You will need the domain name, the fully qualified domain/FQDN (hostname + domain name) and the network interface.
What’s next is the most important part of the tutorial: the subnetting. If this is not configured correctly, then the DHCP service will not start and will generate errors.
You can configure the range of your hosts’ IP addresses, based on your subnet address, server’s IP address, subnet mask, broadcast address and default gateway.
Above are my server’s IP address, subnet mask, broadcast address and default gateway.
I will be configuring my host range, such that it is 1 greater than my server’s IP address and 1 less than my broadcast address, in order to avoid IP conflicts.
As well, the subnet address is the first address in a subnet, so my subnet address’s last octet will be 0.
This section of the script installs the installation package for the DHCP server (finally!), copies an example file and overwrites it as the DHCP server’s configuration file, which is empty by default.
The example file is /usr/share/doc/dhcp-[DHCP yum version]/dhcpd.conf.example.
The DHCP configuration file is /etc/dhcp/dhcpd.conf.
This section configures the DNS settings that were assigned as variables earlier (the domain name and FQDN).
This section makes the system the official DHCP server by removing the “#” symbol from the beginning of line 18, where it says “authoritative”.
This section comments out the sections of the configuration file that will not be needed.
The for-loops in this section loop through the lines in the file that need to be commented out and put the “#” symbol at the beginning of each line.
This section inserts the subnet information (subnet address, host range, default gateway and broadcast address) into the file.
This section configures a firewall rule that permits DHCP traffic.
This section, which is the final section, starts the DHCP software, enables to run during bootup and prints the status of the software.
This is the output of the script in my system. The red error messages you see are about the configurations for my virtual network interface, which are not needed. So, ignore that. :) Below is the entire script:
#!/bin/bash
# Update the OS.
yum install -y update# Assign the domain name and FQDN to variables.
domain_name=$(hostname | cut -d’.’ -f2–3)
fqdomain_name=$(hostname)# List the available network interfaces.
net_int=$(ip -o link show | awk -F’: ‘ ‘{print $2}’)
echo $net_intecho ‘Enter the network interface to configure the DNS server with: ‘
read -r “net_int_name”# Assign the subnet IP addresses to variables.
net_int_ip=$(ifconfig $net_int_name | awk -F’ ‘ ‘FNR == 2 {print $2}’)oct_1=$(expr $net_int_ip | cut -d”.” -f1)
oct_2=$(expr $net_int_ip | cut -d”.” -f2)
oct_3=$(expr $net_int_ip | cut -d”.” -f3)
oct_4=$(expr $net_int_ip | cut -d”.” -f4)subnet_add=”${oct_1}.${oct_2}.${oct_3}.0"
subnet_mask=$(ifconfig $net_int_name | awk -F’ ‘ ‘FNR == 2 {print $4}’)
broadcast_add=$(ifconfig $net_int_name | awk -F’ ‘ ‘FNR == 2 {print $6}’)
default_gateway=$(ip route | grep default | awk -F’ ‘ ‘{print $3}’)ba_oct_1=$(expr $broadcast_add | cut -d”.” -f1)
ba_oct_2=$(expr $broadcast_add | cut -d”.” -f2)
ba_oct_3=$(expr $broadcast_add | cut -d”.” -f3)
ba_oct_4=$(expr $broadcast_add | cut -d”.” -f4)first_host=”${oct_1}.${oct_2}.${oct_3}.1"
last_host=”${ba_oct_1}.${ba_oct_2}.${ba_oct_3}.$(expr $ba_oct_4–1)”# Install the package for the DHCP server.
yum install -y dhcp# Assign files to variables.
dhcp_copy=”/usr/share/doc/”
dhcp_file=”/etc/dhcp/dhcpd.conf”# Configure the DHCP server’s configuration file.
cd $dhcp_copy
copied_file_name=$(ls | grep ‘dhcp’ | head -1)
copied_file_path=$(readlink -f ${copied_file_name}/dhcpd.conf.example)cp -R ${copied_file_path} ${dhcp_file}# Enter the DNS server info in the DHCP server config file.
sed -i -e “7s/example.org/${domain_name}/” $dhcp_file
sed -i -e “8s/ns1.example.org, ns2.example.org/${fqdomain_name}/” $dhcp_file# Make the DHCP server the official DHCP server by un-commenting the “authoritative” directive.
sed -i “18s/^#//” $dhcp_file# Comment out the 10.152.87.0/24 subnet.
for i in $(seq 27 28)
do
sed -i “${i}s/^/#/” $dhcp_file
done# Comment out the 10.254.239.0/27 subnet.
for i in $(seq 32 35)
do
sed -i “${i}s/^/#/” $dhcp_file
done# Comment out the 10.254.239.32/27 subnet
for i in $(seq 40 44)
do
sed -i “${i}s/^/#/” $dhcp_file
done# Comment out the “passacaglia” host statement
for i in $(seq 62 66)
do
sed -i “${i}s/^/#/” $dhcp_file
done# Comment out the “fantasia” host statement
for i in $(seq 75 78)
do
sed -i “${i}s/^/#/” $dhcp_file
done# Comment out the “foo” class
for i in $(seq 85 87)
do
sed -i “${i}s/^/#/” $dhcp_file
done# Comment out the “shared-network 224–29” subnets
for i in $(seq 89 104)
do
sed -i “${i}s/^/#/” $dhcp_file
done# Configure the subnet
sed -i -e “47s/10.5.5.0/${subnet_add}/;47s/255.255.255.224/${subnet_mask}/” $dhcp_file
sed -i -e “48s/10.5.5.26/${first_host}/;48s/10.5.5.30/${last_host}/” $dhcp_file
sed -i -e “49s/ns1.internal.example.org/${fqdomain_name}/” $dhcp_file
sed -i -e “50s/internal.example.org/${domain_name}/” $dhcp_file
sed -i -e “51s/10.5.5.1/${default_gateway}/” $dhcp_file
sed -i -e “52s/10.5.5.31/${broadcast_add}/” $dhcp_file# Enable a firewall rule that permits DHCP traffic.
firewall_array=(‘ — add-service=dhcp — permanent — zone=public’ ‘ — reload’ ‘ — list-all’)
for i in ${firewall_array[@]}
do
firewall-cmd $i
done# Start, enable and view the status of the DHCP server
dhcp_array=(‘enable’ ‘start’ ‘ — no-pager status’)
for i in ${dhcp_array[@]}
do
systemctl $i dhcpd
done
View Part I of this tutorial series on DNS server automation.
View Part III of this tutorial series on blog and web server automation.