Last active
August 5, 2021 13:32
-
-
Save tecknoh19/344b4c88cca313a3b108575051547aac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
# This script was ran on Ubuntu 20.04. | |
# Check for root | |
if [ "$EUID" -ne 0 ] | |
then | |
echo "Please run as root" | |
echo "USAGE: sudo build-dnsmasq-server.sh" | |
exit | |
fi | |
# Checking / installing net-tools | |
echo -e "\nInstalling Net Tools\n" | |
apt-get install net-tools -y | |
echo "\nNet Tools installed / updated.\n" | |
# systemd.resolved handler | |
echo -e "\nStopping systemd.resolved\n" | |
systemctl stop systemd-resolved | |
echo -e "systemd.resolved stopped. Preventing autostart\n" | |
update-rc.d systemd-resolved disable | |
echo -e "systemd.resolved stopped from autostarting at boot" | |
# Install dnsmasq | |
echo -e "Installing DNSMASQ\n" | |
apt-get install dnsmasq -y | |
echo -e "DNSMASQ installed / updated\n\n" | |
# Obtaining Server IP | |
ip_address=$(/sbin/ifconfig | sed -n '2 p' | awk '{print $2}') | |
host_name=$(hostname) | |
echo "Determined IP Address to be: " $ip_address | |
# Create hosts file base | |
hostData=" | |
127.0.0.1 localhost\n | |
$ip_address $host_name\n | |
\n | |
# The following lines are desirable for IPv6 capable hosts \n | |
::1 ip6-localhost ip6-loopback \n | |
fe00::0 ip6-localnet \n | |
ff00::0 ip6-mcastprefix \n | |
ff02::1 ip6-allnodes \n | |
ff02::2 ip6-allrouters \n | |
" | |
mv /etc/hosts /etc/hosts.bak | |
echo -e "Removing existing hosts file at /etc/hosts\n" | |
rm -rf /etc/hosts | |
echo -e "Creating new hosts file at /etc/hosts\n" | |
> /etc/hosts | |
echo -e $hostData > /etc/hosts | |
echo -e "Hosts file creation successful\n\n" | |
# Create new resolv.conf | |
echo -e "Updating /etc/resolv.conf" | |
mv /etc/resolv.conf /etc/resolv.conf.bak | |
> /etc/resolv.conf | |
echo "nameserver 127.0.0.1" > /etc/resolv.conf | |
echo -e "Updated: resolv.conf\n" | |
# Backup original dnsmasq config | |
echo -e "Backing original dnsmasq configuration /etc/dnsmasq.conf to /etc/dnsmasq.conf.bak\n" | |
mv etc/dnsmasq.conf /etc/dnsmasq.conf.bak | |
echo -e "Configuration backed up.\n" | |
echo -e "Creating new dnsmasq.conf in /etc\n" | |
dnsmasqconf=" | |
# Global settings\n | |
domain-needed \n | |
bogus-priv \n | |
no-resolv \n | |
expand-hosts \n | |
filterwin2k \n\n | |
# Upstream nameservers\n | |
server=8.8.4.4 \n | |
server=8.8.8.8 \n\n | |
# domain name \n | |
domain=home.network \n | |
local=/home.network/ \n\n | |
listen-address=127.0.0.1 \n | |
listen-address=$ip_address \n" | |
echo -e $dnsmasqconf > /etc/dnsmasq.conf | |
echo -e "/etc/dnsmasq.conf created\n" | |
systemctl restart dnsmasq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment