DEV Community

Артем Ахмадеев
Артем Ахмадеев

Posted on Originally published at lamplightservers.github.io AI-assisted

Self-Host AdGuard Home with Docker to Block Ads Network-Wide

Ads and trackers follow you across every device: phones, laptops, smart TVs. Browser extensions only cover one browser at a time, and public DNS blockers hand your query history to a third party. A DNS-level blocker stops unwanted domains before they load, on any device that uses it.

In this tutorial we build a private AdGuard Home server on your own VPS, running in Docker. DNS is open only to your own IP, and the dashboard is never exposed to the internet, only reachable through an SSH tunnel. Self-hosting means you control the logs, the blocklists and who can use the server, with no subscription.

What you need

  • A VPS with Ubuntu 24.04 (2 vCPU / 4 GB RAM is plenty). I use VDSina (affiliate link — it supports the channel at no extra cost to you).
  • An SSH connection to it (any terminal works).

Short on time? Every command below is also in one ready-to-run script.

Step 1: Update the system

We start by refreshing the package lists and installing pending upgrades, which brings in the latest security fixes. Non-interactive flags let the upgrade run unattended instead of stopping at prompts.

export DEBIAN_FRONTEND=noninteractive NEEDRESTART_MODE=a; apt-get update -y && apt-get upgrade -y -o Dpkg::Options::=--force-confold
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Docker

Docker is installed with the official convenience script, which sets up both the engine and the compose plugin in one go. Running AdGuard Home in a container keeps it isolated and easy to update.

curl -fsSL https://get.docker.com | sh
Enter fullscreen mode Exit fullscreen mode

Step 3: Free port 53

Ubuntu's built-in resolver already occupies port 53, which AdGuard needs. We disable its stub listener, point resolv.conf to the real upstream configuration and restart the service, so the server itself still resolves names normally.

mkdir -p /etc/systemd/resolved.conf.d
cat > /etc/systemd/resolved.conf.d/adguardhome.conf <<'EOF'
[Resolve]
DNSStubListener=no
EOF
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
systemctl restart systemd-resolved
Enter fullscreen mode Exit fullscreen mode

Step 4: Write the compose file

The compose file lives in /opt/adguard and uses host networking so DNS traffic reaches the container directly. Two mounted folders keep configuration and data on disk so updates don't erase them, and a restart policy brings the service back after a reboot.

mkdir -p /opt/adguard && cd /opt/adguard
cat > docker-compose.yml <<'EOF'
services:
  adguardhome:
    image: adguard/adguardhome:latest
    container_name: adguardhome
    network_mode: host
    restart: unless-stopped
    volumes:
      - ./work:/opt/adguardhome/work
      - ./conf:/opt/adguardhome/conf
EOF
Enter fullscreen mode Exit fullscreen mode

Step 5: Restrict access with UFW

The firewall step is critical, because open DNS servers are found and abused by strangers very quickly. We deny all incoming traffic, keep SSH open, and allow port 53 only from your current IP. If your home IP changes, that rule must be updated.

IP=${SSH_CLIENT%% *}; IP=${IP:-127.0.0.1}
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow from $IP to any port 53 proto udp
ufw allow from $IP to any port 53 proto tcp
ufw --force enable
Enter fullscreen mode Exit fullscreen mode

Step 6: Start AdGuard Home

A single docker compose command pulls the image and starts AdGuard Home in the background. Once the image has downloaded, startup takes only a few seconds.

cd /opt/adguard && docker compose up -d
Enter fullscreen mode Exit fullscreen mode

Step 7: Finish the setup wizard

Instead of using the browser wizard, we complete setup from the terminal: generate a random password, store it in a root-only file, and send the configuration to AdGuard's local API. The dashboard stays closed to the internet, and you reach it from your PC through an SSH tunnel on port 3000.

echo "On your own PC the tunnel would be: ssh -L 3000:localhost:3000 root@YOUR_SERVER_IP"
tr -dc A-Za-z0-9 </dev/urandom | head -c 20 > /root/adguard-pass.txt; chmod 600 /root/adguard-pass.txt
curl -s -X POST http://127.0.0.1:3000/control/install/configure \
  -H 'Content-Type: application/json' \
  -d '{"web":{"ip":"0.0.0.0","port":3000},"dns":{"ip":"0.0.0.0","port":53},"username":"admin","password":"'"$(cat /root/adguard-pass.txt)"'"}'
echo "Login: admin / password in /root/adguard-pass.txt"
Enter fullscreen mode Exit fullscreen mode

Step 8: Add a blocklist

We add OISD Small, a well-known list that blocks ads and trackers while rarely breaking sites, with one API call. More lists can be added later in the dashboard, but starting small helps avoid false blocks.

curl -s -u admin:$(cat /root/adguard-pass.txt) -X POST http://127.0.0.1:3000/control/filtering/add_url \
  -H 'Content-Type: application/json' \
  -d '{"name":"OISD Small","url":"https://small.oisd.nl/","whitelist":false}'
Enter fullscreen mode Exit fullscreen mode

Step 9: Test DNS blocking

To verify, we install dig and query our server for a normal domain and an ad domain. The normal one should return an address, while the ad domain should come back blocked, usually as an empty or zero address. For a phone, enter the server IP as manual DNS in the Wi-Fi settings.

apt-get install -y dnsutils
echo "Phone: Wi-Fi settings > DNS > Manual > $(hostname -I | cut -d' ' -f1) (works from the IP you allowed in UFW)"
echo "Allowed domain:"; dig @127.0.0.1 example.com +short
echo "Blocked domain:"; dig @127.0.0.1 doubleclick.net +short
Enter fullscreen mode Exit fullscreen mode

Common mistakes

  • Leaving port 53 open to the whole internet turns your server into an open resolver that attackers use for amplification attacks. Always restrict it with UFW to your own IP addresses.
  • Your home IP is often dynamic, so the DNS suddenly stops working for your devices after it changes. Update the UFW rule with the new IP, or use a VPN with a fixed address.
  • Skipping the stub listener fix causes AdGuard to fail to start because port 53 is already in use. Check the change was applied and the resolver restarted, and enable too many blocklists at once and you will see broken sites, so add lists gradually.

Wrap-up

Now that your server is running, point your phone and laptop to it and watch the dashboard's query log to see what gets blocked. Then try adding a second blocklist, setting up allowlist entries for anything that breaks, and making a backup of the /opt/adguard folders.

Every command here was tested on a fresh Ubuntu 24.04 server before publishing. Watch the full walkthrough on YouTube — new tutorials every week on Lamplight Servers.

Top comments (0)