omarchy/bin/omarchy-setup-dns
Jarek c0770fc7a3
Ensure network interfaces respect DNS configuration (#1043)
Implements DHH's requirement to ensure wlan0 and ethernet interfaces
respect the DNS configuration while excluding docker/loop interfaces.

When using Cloudflare or Custom DNS:
- Sets UseDNS=no in DHCPv4 sections
- Sets UseDNS=no in IPv6AcceptRA sections
- Only configures primary interfaces (en*, eth*, wl*)
- Restarts both systemd-networkd and systemd-resolved

When using DHCP:
- Removes UseDNS=no to allow DHCP provided DNS
- Restores default behavior for all interfaces

Script size increased from 58 to 89 lines to properly handle all
network interfaces as requested.
2025-08-24 18:33:32 +02:00

90 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
if [[ -z $1 ]]; then
dns=$(gum choose --height 5 --header "Select DNS provider" Cloudflare DHCP Custom)
else
dns=$1
fi
case "$dns" in
Cloudflare)
sudo tee /etc/systemd/resolved.conf >/dev/null <<'EOF'
[Resolve]
DNS=1.1.1.1#cloudflare-dns.com 1.0.0.1#cloudflare-dns.com
FallbackDNS=8.8.8.8#dns.google 8.8.4.4#dns.google
DNSOverTLS=opportunistic
EOF
# Ensure network interfaces don't override our DNS settings
for file in /etc/systemd/network/*.network; do
[[ -f "$file" ]] || continue
if ! grep -q "^\[DHCPv4\]" "$file"; then continue; fi
# Add UseDNS=no to DHCPv4 section if not present
if ! sed -n '/^\[DHCPv4\]/,/^\[/p' "$file" | grep -q "^UseDNS="; then
sudo sed -i '/^\[DHCPv4\]/a UseDNS=no' "$file"
fi
# Add UseDNS=no to IPv6AcceptRA section if present
if grep -q "^\[IPv6AcceptRA\]" "$file" && ! sed -n '/^\[IPv6AcceptRA\]/,/^\[/p' "$file" | grep -q "^UseDNS="; then
sudo sed -i '/^\[IPv6AcceptRA\]/a UseDNS=no' "$file"
fi
done
sudo systemctl restart systemd-networkd systemd-resolved
;;
DHCP)
sudo tee /etc/systemd/resolved.conf >/dev/null <<'EOF'
[Resolve]
DNS=
FallbackDNS=
DNSOverTLS=no
EOF
# Allow network interfaces to use DHCP DNS
for file in /etc/systemd/network/*.network; do
[[ -f "$file" ]] || continue
sudo sed -i '/^UseDNS=no/d' "$file"
done
sudo systemctl restart systemd-networkd systemd-resolved
;;
Custom)
echo "Enter your DNS servers (space-separated, e.g. '192.168.1.1 1.1.1.1'):"
read -r dns_servers
if [[ -z "$dns_servers" ]]; then
echo "Error: No DNS servers provided."
exit 1
fi
sudo tee /etc/systemd/resolved.conf >/dev/null <<EOF
[Resolve]
DNS=$dns_servers
FallbackDNS=1.1.1.1 8.8.8.8
EOF
# Ensure network interfaces don't override our DNS settings
for file in /etc/systemd/network/*.network; do
[[ -f "$file" ]] || continue
if ! grep -q "^\[DHCPv4\]" "$file"; then continue; fi
# Add UseDNS=no to DHCPv4 section if not present
if ! sed -n '/^\[DHCPv4\]/,/^\[/p' "$file" | grep -q "^UseDNS="; then
sudo sed -i '/^\[DHCPv4\]/a UseDNS=no' "$file"
fi
# Add UseDNS=no to IPv6AcceptRA section if present
if grep -q "^\[IPv6AcceptRA\]" "$file" && ! sed -n '/^\[IPv6AcceptRA\]/,/^\[/p' "$file" | grep -q "^UseDNS="; then
sudo sed -i '/^\[IPv6AcceptRA\]/a UseDNS=no' "$file"
fi
done
sudo systemctl restart systemd-networkd systemd-resolved
;;
esac