dns setting for Debian 12 using nmcli (#4407)

This commit is contained in:
openoms 2024-02-10 18:03:18 +00:00 committed by GitHub
parent 4719fbd443
commit e98bb8d3c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,9 +2,9 @@
# command info # command info
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "-help" ]; then
echo "config script to set a the DNS server that should be used" echo "config script to set a the DNS server that should be used"
echo "internet.dns.sh [DNS-SERVER|test|off]" echo "internet.dns.sh [DNS-SERVER|test|off]"
exit 1 exit 1
fi fi
# 1. parameter # 1. parameter
@ -14,7 +14,7 @@ DNSSERVER="$1"
NODIALOG="$2" NODIALOG="$2"
# just if auto reboot is needed after dialog # just if auto reboot is needed after dialog
autoreboot=0 autoreboot=0
if [ "${DNSSERVER}" = "off" ]; then if [ "${DNSSERVER}" = "off" ]; then
# setting DNS address # setting DNS address
@ -22,7 +22,7 @@ if [ "${DNSSERVER}" = "off" ]; then
sudo /home/admin/config.scripts/blitz.conf.sh delete "static domain_name_servers" /etc/dhcpcd.conf sudo /home/admin/config.scripts/blitz.conf.sh delete "static domain_name_servers" /etc/dhcpcd.conf
/home/admin/config.scripts/blitz.conf.sh delete dnsServer /home/admin/config.scripts/blitz.conf.sh delete dnsServer
echo "# OK - needs reboot to activate" echo "# OK - needs reboot to activate"
echo "" echo
exit 0 exit 0
fi fi
@ -44,39 +44,71 @@ Do you want to set the fixed DNS 1.1.1.1 by Cloudflare (they claim they provide
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "# SETTING 1.1.1.1" echo "# SETTING 1.1.1.1"
DNSSERVER="1.1.1.1" DNSSERVER="1.1.1.1"
# for IPv6: DNSSERVER="2606:4700:4700::1111"
autoreboot=1 autoreboot=1
else else
echo "# Ignoring DNS-Test fail" echo "# Ignoring DNS-Test fail"
fi fi
fi fi
echo "The DNS server you want to set is: ${DNSSERVER}" echo "The DNS server you want to set is: ${DNSSERVER}"
# checking parameter # checking parameter
if [[ $DNSSERVER =~ ^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$ ]]; then if [[ $DNSSERVER =~ ^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$ ]]; then
echo "# OK IPv6" echo "# OK ipv6"
DNSTYPE=ipv6
elif [[ $DNSSERVER =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then elif [[ $DNSSERVER =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then
echo "# OK IPv4" echo "# OK ipv4"
DNSTYPE=ipv4
else else
echo "error='not an IPv4 or IPv6 address'" echo "error='not an IPv4 or IPv6 address'"
exit 1 exit 1
fi fi
echo "" echo
dnsconfFile="/etc/dhcpcd.conf" # check if /etc/dhcpcd.conf or /etc/dhcp/dhcpd.conf exists
isUbuntu=$(cat /etc/os-release 2>/dev/null | grep -c 'Ubuntu')
if [ ${isUbuntu} -gt 0 ]; then if sudo test -f /etc/dhcpcd.conf || sudo test -f /etc/dhcp/dhcpd.conf; then
echo "# adapting dhcpd.conf path for ubuntu" dnsconfFile="/etc/dhcpcd.conf"
dnsconfFile="/etc/dhcp/dhcpd.conf" if grep Ubuntu /etc/os-release; then
echo "# adapting dhcpd.conf path for ubuntu"
dnsconfFile="/etc/dhcp/dhcpd.conf"
fi
# setting DNS address
echo "# Setting DNS server ${DNSSERVER} in ${dnsconfFile} ..."
sudo /home/admin/config.scripts/blitz.conf.sh set "static domain_name_servers" "${DNSSERVER}" "${dnsconfFile}"
echo "# OK"
echo
else
# Get a list of all active Ethernet and Wi-Fi connections
ACTIVE_CONNECTIONS=$(nmcli -t -f TYPE,NAME con show --active | grep -E 'ethernet|wireless' | cut -d: -f2)
for CON in $ACTIVE_CONNECTIONS; do
if [[ -n "$CON" && "$CON" != "-" ]]; then
echo "Setting ${DNSTYPE} DNS $DNSSERVER for $CON..."
# Set the DNS servers for this connection
if sudo nmcli con mod "$CON" $DNSTYPE.dns "$DNSSERVER" &&
sudo nmcli con mod "$CON" $DNSTYPE.ignore-auto-dns yes &&
sudo nmcli con mod "$CON" $DNSTYPE.method auto; then
# if set successfully restart the connection to apply changes
sudo nmcli con down "$CON"
sudo nmcli con up "$CON"
echo "${DNSTYPE} DNS set for $CON."
else
echo "Error: Failed to set DNS for $CON. It might not be an active connection."
fi
else
echo "Skipping invalid or inactive connection name: $CON"
fi
done
echo "DNS settings updated for all active Ethernet and Wi-Fi connections."
# show the DNS setting
nmcli dev show | grep DNS
fi fi
# setting DNS address
echo "# Setting DNS server in /etc/dhcpcd.conf ..."
sudo /home/admin/config.scripts/blitz.conf.sh set "static domain_name_servers" "${DNSSERVER}" /etc/dhcpcd.conf
echo "# OK"
echo ""
# make sure entry in raspiblitz.conf exists # make sure entry in raspiblitz.conf exists
/home/admin/config.scripts/blitz.conf.sh set dnsServer "${DNSSERVER}" /home/admin/config.scripts/blitz.conf.sh set dnsServer "${DNSSERVER}"
echo "# OK" echo "# OK"
@ -85,4 +117,4 @@ echo ""
echo "# DNS Server is set - needs reboot to get active" echo "# DNS Server is set - needs reboot to get active"
if [ ${autoreboot} -eq 1 ]; then if [ ${autoreboot} -eq 1 ]; then
sudo shutdown -r now sudo shutdown -r now
fi fi