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.
- Add DNS servers on the WSL network adapter
- Configure the
dnsmasq
service on WSL 2 Ubuntu - Starting the
dnsmasq
service with a Docker container on WSL 2
Install and configure the following services
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)
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
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 whenWSL 2
is restarted.
Note: You can also connect to
http://dnsmasq.docker:5380/
to see the dnsmasq logs and change configurations.(credentials:
foo/bar
)