PowerShell/scripts/install-unbound-server.ps1

67 lines
2.2 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02:00
<#
2022-05-22 22:35:21 +02:00
.SYNOPSIS
2023-04-06 08:46:59 +02:00
Installs Unbound server (needs admin rights)
2022-05-22 22:35:21 +02:00
.DESCRIPTION
2022-12-29 21:53:10 +01:00
This PowerShell script installs Unbound, a validating, recursive, caching DNS resolver. It needs admin rights.
2022-05-22 22:35:21 +02:00
.EXAMPLE
2023-08-06 21:35:36 +02:00
PS> ./install-unbound-server.ps1
2022-05-22 22:35:21 +02:00
.LINK
2022-12-29 21:53:10 +01:00
https://github.com/fleschutz/PowerShell
2022-05-22 22:35:21 +02:00
.NOTES
Author: Markus Fleschutz | License: CC0
#>
#Requires -RunAsAdministrator
try {
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2023-04-02 11:09:19 +02:00
"⏳ (1/10) Updating package infos..."
2022-05-22 22:35:21 +02:00
& sudo apt update -y
2023-04-02 11:03:40 +02:00
if ($lastExitCode -ne "0") { throw "'apt update' failed" }
2022-05-22 22:35:21 +02:00
2023-04-06 09:04:27 +02:00
"⏳ (2/10) Installing the Unbound packages..."
2023-04-02 11:03:40 +02:00
& sudo apt install unbound unbound-anchor -y
if ($lastExitCode -ne "0") { throw "'apt install unbound' failed" }
2022-05-22 22:35:21 +02:00
2023-04-02 11:09:19 +02:00
"⏳ (3/10) Setting up Unbound..."
2022-05-22 22:35:21 +02:00
& sudo unbound-control-setup
if ($lastExitCode -ne "0") { throw "'unbound-control-setup' failed" }
2023-04-02 11:09:19 +02:00
"⏳ (4/10) Updating DNSSEC Root Trust Anchors..."
2022-05-22 22:35:21 +02:00
& sudo unbound-anchor
if ($lastExitCode -ne "0") { throw "'unbound-anchor' failed" }
2023-04-02 11:09:19 +02:00
"⏳ (5/10) Checking config file..."
2023-10-31 11:25:11 +01:00
& unbound-checkconf "$PSScriptRoot/../data/unbound.conf"
if ($lastExitCode -ne "0") { throw "'unbound-checkconf' failed - check the syntax" }
2023-04-02 11:09:19 +02:00
"⏳ (6/10) Copying config file to /etc/unbound/unbound.conf ..."
2023-10-31 11:25:11 +01:00
& sudo cp "$PSScriptRoot/../data/unbound.conf" /etc/unbound/unbound.conf
2022-05-22 22:35:21 +02:00
if ($lastExitCode -ne "0") { throw "'cp' failed" }
2023-04-02 11:09:19 +02:00
"⏳ (7/10) Stopping default DNS cache daemon systemd-resolved..."
2022-06-13 15:34:53 +02:00
& sudo systemctl stop systemd-resolved
& sudo systemctl disable systemd-resolved
2023-04-02 11:09:19 +02:00
"⏳ (8/10) (Re-)starting Unbound..."
2022-05-22 22:35:21 +02:00
& sudo unbound-control stop
& sudo unbound-control start
if ($lastExitCode -ne "0") { throw "'unbound-control start' failed" }
2023-04-06 08:46:59 +02:00
"⏳ (9/10) Checking status of Unbound..."
2022-05-22 22:35:21 +02:00
& sudo unbound-control status
if ($lastExitCode -ne "0") { throw "'unbound-control status' failed" }
2023-09-20 17:04:17 +02:00
"⏳ (10/10) Training Unbound with 100 popular domain names..."
& "$PSScriptRoot/check-dns.ps1"
if ($lastExitCode -ne "0") { throw "'unbound-control status' failed" }
2022-05-22 22:35:21 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2024-10-01 13:37:53 +02:00
"✅ Installed Unbound in $Elapsed sec"
2022-05-22 22:35:21 +02:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
2023-04-06 08:46:59 +02:00
}