From 6c66d880684f5e5a5da0a94e1424193d597a2ecb Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Fri, 14 Jun 2019 02:35:57 -0300 Subject: [PATCH 1/2] Adding script to check DNS on laptop Laptop battery is not working, so when the clock is wrong, my DNS server does not works (because of DNSSEC) and it makes the clock to not be able to update properly. So this script changes the DNS to Quad9 and forces update the clock time before changing it back to my DNS server. --- Services/fixDNS.sh | 37 +++++++++++++++++++++++++++++++++++++ Services/testDNS.service | 11 +++++++++++ 2 files changed, 48 insertions(+) create mode 100755 Services/fixDNS.sh create mode 100644 Services/testDNS.service diff --git a/Services/fixDNS.sh b/Services/fixDNS.sh new file mode 100755 index 0000000..51b2e7e --- /dev/null +++ b/Services/fixDNS.sh @@ -0,0 +1,37 @@ +function isDNSWorking() { + dnsWorking=$(dig brunofontes.net | grep "ANSWER: 0") +} +logfile="/var/log/bfontes" + +sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Checking if DNS is working" >> "$logfile" +isDNSWorking + +if [ "$dnsWorking" ]; then + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - DNS not working, hour may be wrong. Trying to fix..." >> "$logfile" + sudo chattr -i /etc/resolv.conf + sudo sed -i 's/127.0.0.2/9.9.9.9/' /etc/resolv.conf 2>>"$logfile" + sudo chattr +i /etc/resolv.conf + + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Forcing time to sync" >> "$logfile" + sudo chronyc online 2>"$logfile" + sudo chronyc -a 'burst 4/4' + i=0 + while [ "$dnsWorking" -a $i -le 4 ]; do + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Waiting..." >> "$logfile" + sleep 20 + isDNSWorking + i=$(( "$i" + 1 )) + done + + if [ -z "$dnsWorking" ]; then + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - FIXED!" >> "$logfile" + else + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Script has failed..." >> "$logfile" + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Fixing DNS script has failed. More details on $logfile" >> ~/.log_error + fi + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Configuring DNS servers back." >> "$logfile" + sudo chattr -i /etc/resolv.conf + sudo sed -i 's/9.9.9.9/127.0.0.2/' /etc/resolv.conf + sudo chattr +i /etc/resolv.conf +fi +sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Done" >> "$logfile" diff --git a/Services/testDNS.service b/Services/testDNS.service new file mode 100644 index 0000000..9e1accf --- /dev/null +++ b/Services/testDNS.service @@ -0,0 +1,11 @@ +[Unit] +Description=Test and fix DNS update with wrong time +Wants=network-online.target +After=network-online.target + +[Service] +Type=oneshot +ExecStart = /bin/bash /home/bruno/Apps/linuxShortcuts/Services/fixDNS.sh + +[Install] +WantedBy=multi-user.target From 512fab8ec36249d0d70060440787fe261a6351cb Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Fri, 14 Jun 2019 02:38:11 -0300 Subject: [PATCH 2/2] DNS Server: Including scripts to update RootHints monthly --- Services/updateRootHints.service | 6 ++++++ Services/updateRootHints.timer | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 Services/updateRootHints.service create mode 100644 Services/updateRootHints.timer diff --git a/Services/updateRootHints.service b/Services/updateRootHints.service new file mode 100644 index 0000000..10f0902 --- /dev/null +++ b/Services/updateRootHints.service @@ -0,0 +1,6 @@ +[Unit] +Description=Run update DNS root hints montly + +[Service] +Type=oneshot +ExecStart = /bin/bash /home/bruno/dns_update_rootHints.sh diff --git a/Services/updateRootHints.timer b/Services/updateRootHints.timer new file mode 100644 index 0000000..d98bb75 --- /dev/null +++ b/Services/updateRootHints.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Run update DNS root hints montly + +[Timer] +OnCalendar=monthly +Persistent=true + +[Install] +WantedBy=timers.target