Abdelhamid Er-rajy
3 min readApr 13, 2024
What is dns
  1. The Domain Name System (DNS) is the phonebook of the Internet. Humans access information online through domain names, like nytimes.com or espn.com. Web browsers interact through Internet Protocol (IP) addresses. DNS translates domain names to IP addresses so browsers can load Internet resources.

Dynamic Host Configuration Protocol (DHCP) is a network protocol used to automate the process of configuring devices on IP networks, thus allowing them to use network services such as DNS, NTP, and any communication protocol based on UDP or TCP .

process:

  1. Discovery: Client broadcasts a request for DHCP servers on the network.
  2. Offer: DHCP server responds with an available IP address and configuration details.
  3. Request: Client selects an offered IP address and requests its reservation.
  4. Acknowledgment (Ack): DHCP server confirms the reservation of the requested IP address to the client.

→ Open Terminal

sudo yum install bind* dhcpd*
sudo nano config_dns_dhcp.sh

add this script :

#!/bin/bash
# Function to set up IP address configuration
configure_ip_address() {
echo -e "\e[32mConfiguring IP address...\e[0m"
cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=$ip_address
NETMASK=255.255.255.0
EOF
systemctl restart network
echo -e "\e[32mIP address configured successfully.\e[0m"
}
# Function to set up DNS server configuration
configure_dns_server() {
echo -e "\e[32mConfiguring DNS server...\e[0m"
cat <<EOF > /etc/named.conf
options {
listen-on port 53 { any; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
recursion yes;
};

zone "$domainname" IN {
type master;
file "forward.$domainname";
};

zone "10.168.192.in-addr.arpa" IN {
type master;
file "reverse.$domainname";
};
EOF

mkdir -p /var/named/data
touch /var/named/data/cache_dump.db
touch /var/named/data/named_stats.txt
touch /var/named/data/named_mem_stats.txt

cat <<EOF > /var/named/forward.$domainname
\$TTL 86400
@ IN SOA $hostname.$domainname. root.$domainname. (
2011071001
3600
1800
604800
86400
)
@ IN NS $hostname.$domainname.
@ IN A $ip_address
$hostname IN A $ip_address
www IN A $ip_address
EOF

cat <<EOF > /var/named/reverse.$domainname
\$TTL 86400
@ IN SOA $hostname.$domainname. root.$domainname. (
2011071001
3600
1800
604800
86400
)
@ IN NS $hostname.$domainname.
1 IN PTR $domainname.
EOF

chown -R named:named /var/named/
systemctl start named
systemctl enable named
firewall-cmd --permanent --add-service=dns
firewall-cmd --reload

echo -e "\e[32mDNS server configured successfully.\e[0m"
}

# Function to set up DHCP server configuration
configure_dhcp_server() {
echo -e "\e[32mConfiguring DHCP server...\e[0m"
cat <<EOF > /etc/dhcp/dhcpd.conf
subnet 192.168.10.0 netmask 255.255.255.0 {
range $dhcp_range;
option subnet-mask 255.255.255.0;
option routers $ip_address;
option domain-name-servers $ip_address;
default-lease-time 600;
max-lease-time 7200;
}
EOF

systemctl start dhcpd
systemctl enable dhcpd

echo -e "\e[32mDHCP server configured successfully.\e[0m"
}

# Function to set hostname and update hosts file
configure_hostname() {
echo -e "\e[32mSetting hostname to $hostname...\e[0m"
hostnamectl set-hostname "$hostname"

echo -e "\e[32mUpdating /etc/hosts file...\e[0m"
echo "$ip_address $hostname.$domainname $hostname" >> /etc/hosts
}

# Function to update resolv.conf file
configure_resolv_conf() {
echo -e "\e[32mUpdating /etc/resolv.conf file...\e[0m"
echo "search $domainname" > /etc/resolv.conf
echo "nameserver $ip_address" >> /etc/resolv.conf
}
# Main function
main() {
configure_ip_address
configure_dns_server
configure_dhcp_server
configure_hostname
configure_resolv_conf
echo -e "\e[32mAll configurations completed successfully.\e[0m"
}

# Main execution
read -p "Enter your IP address: " ip_address
read -p "Enter your domain name: " domainname
read -p "Enter your hostname: " hostname
read -p "Enter your DHCP range: " dhcp_range

main
echo -e "\033[38;5;208mRebooting in 3 seconds...\033[0m" && sleep 3 && sudo reboot

saved config file using Ctrl + X

add permission +x

chmod +x config_dns_dhcp.sh

Run script

./config_dns_dhcp.sh
Abdelhamid Er-rajy
Abdelhamid Er-rajy

Written by Abdelhamid Er-rajy

Network & Systems Administrator | IT Support Specialist |

No responses yet