Skip to content

Instantly share code, notes, and snippets.

@khorsmann
Last active March 27, 2024 08:22
Show Gist options
  • Save khorsmann/5ceab2cabbf4b7c2321683c4e450a285 to your computer and use it in GitHub Desktop.
Save khorsmann/5ceab2cabbf4b7c2321683c4e450a285 to your computer and use it in GitHub Desktop.
wsl2 installation dnsmasq.md

Copy from https://raw.githubusercontent.com/absolunet/pleaz/production/documentation/installation/wsl2/dnsmasq.md

Installation for WSL 2 - dnsmasq

Documentation > Installation for WSL 2 > dnsmasq

dnsmasq is free software providing Domain Name System (DNS) caching, a Dynamic Host Configuration Protocol (DHCP) server, router advertisement and network boot features, intended for small computer networks.

Table of Contents

  1. Add DNS servers on the WSL network adapter
  2. Configure the dnsmasq service on WSL 2 Ubuntu
  3. Starting the dnsmasq service with a Docker container on WSL 2

Stack Requirement

Install and configure the following services

1. Change preferred DNS servers on the WSL network adapter.

In order to provide a custom dns server to windows, the wsl network adapter needs to be modified to use a local dns server along with the default ones.

The execution will be done on Windows with PowerShell (Administrator) .

# PowerShell (Admin)
Register-ScheduledTask -TaskName "WSL-DnsConfig" -Trigger (New-ScheduledTaskTrigger -AtLogon) -Action (New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-WindowStyle Hidden -Command ""wsl exit; Get-NetAdapter -Name *WSL* | Select-Object InterfaceIndex | Set-DnsClientServerAddress -ServerAddresses ('127.0.0.1','1.1.1.1','8.8.8.8')""") -RunLevel Highest -Force;

This will change the main DNS of the WSL network adapter to use Google, Cloudflare and local DNS (dnsmasq on Ubuntu)

2. Configure the dnsmasq service on WSL 2

On WSL 2, create the file /etc/dnsmasq.conf and add the following configuration:

#dnsmasq config, for a complete example, see:
#  http://oss.segetech.com/intra/srv/dnsmasq.conf
#log all dns queries
log-queries
#dont use hosts nameservers
no-resolv
#use cloudflare as default nameservers, prefer 1^4
server=1.0.0.1
server=1.1.1.1
strict-order
#explicitly define host-ip mappings
address=/test/127.0.0.1
address=/docker/127.0.0.1

If you use another local domain name, add as follows to the bottom of the dnsmasq config file:

address=/<DOMAIN_NAME>/127.0.0.1

3. Configure the dnsmasq service to start automatically with Docker

Open a terminal on WSL 2 (Ubuntu) and execute this command:

# Bash
docker run \
--name dnsmasq -d \
-p 53:53/udp \
-p 5380:8080 \
--log-opt "max-size=100m" \
-e "HTTP_USER=foo" \
-e "HTTP_PASS=bar" \
-v /etc/dnsmasq.conf:/etc/dnsmasq.conf \
--restart always \
jpillora/dnsmasq

By using the flag --restart always, the container will start automatically when WSL 2 is restarted.

Note: You can also connect to http://dnsmasq.docker:5380/ to see the dnsmasq logs and change configurations.

(credentials: foo/bar)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment