This script dynamically updates Unbound DNS server configurations with hostname-to-IP mappings for both IPv4 and IPv6 addresses. It is particularly useful for environments with devices obtaining IPs via DHCP, ensuring hostnames resolve locally without manual updates.
- Automatically extracts and assigns hostnames to IPv4 and IPv6 addresses.
- Skips link-local IPv6 addresses (
fe80::/10
) to avoid unnecessary entries. - Integrates with OPNsense Unbound's template system for persistent configurations.
- Validates and applies configurations using OPNsense's tools.
- OPNsense firewall with Unbound DNS configured.
- Root access to the system.
- Bash shell.
- Clone the repository:
git clone https://github.com/amnsharif/IPv6_Hostnames.git
cd unbound-dynamic-hostname
- Copy the script to /usr/local/bin and make it executable:
sudo cp update_unbound_entries.sh /usr/local/bin/
sudo chmod +x /usr/local/bin/update_unbound_entries.sh
- Run the script to generate the configuration:
sudo bash update_unbound_entries.sh
To automate the process, add a cron job:
sudo crontab -e
Add the following line:
0 * * * * bash /usr/local/bin/update_unbound_entries.sh
This will run the script hourly.
-
Extracts IPv4 and IPv6-to-MAC mappings using arp and ndp commands.
-
Resolves hostnames for each IP address.
-
Filters out link-local (fe80::/10) IPv6 addresses.
-
Writes the hostname-IP mappings into the Unbound template file.
-
Reloads and validates the Unbound configuration.
Adding a new device: Simply ensure the device is connected to the network and has a valid hostname. Run the script to add it to Unbound DNS.
Skipping specific addresses: The script automatically skips link-local IPv6 addresses.
- Error. Permission Denied: Ensure the script is executable and run as root.
sudo chmod +x /usr/local/bin/update_unbound_entries.sh
- Invalid Configuration: Check the Unbound configuration:
configctl unbound check
- No Hostname Found: Verify that the device has a hostname assigned via DHCP.
- No such file or directory: Create the OPNsense template directory and +TARGETS file manually:
mkdir -p /usr/local/opnsense/service/templates/sampleuser/Unbound
echo "sampleuser_dynamic_hosts.conf:/usr/local/etc/unbound.opnsense.d/sampleuser_dynamic_hosts.conf" > /usr/local/opnsense/service/templates/sampleuser/Unbound/+TARGETS
If you find this helpful and would like to contribute, please consider buying me a coffee or visiting my Patreon page