Local DNS on MacOS X for developers » Historie » Zyklus 1
Peter Pfläging, 12.04.2023 09:18
| 1 | 1 | Peter Pfläging | # Local DNS on MacOS X for developers |
|---|---|---|---|
| 2 | |||
| 3 | Sometimes you have the need of local DNS names. Typical this is in local development of VM's or kubernetes services. |
||
| 4 | |||
| 5 | The common method we are all using is to make entries in `/etc/hosts`. |
||
| 6 | |||
| 7 | I don't like this for a couple of reasons: |
||
| 8 | |||
| 9 | - it's easy to blow up the file and get problems after your next reboot |
||
| 10 | - this file is `root` only, though you have to edit it with sudo :-( |
||
| 11 | |||
| 12 | There should be a better way! |
||
| 13 | |||
| 14 | ## Solution |
||
| 15 | |||
| 16 | - Mac OS X resolver is capable to use multiple DNS servers very easily. |
||
| 17 | - There's a very lightweight and easy to configure DNS server: [dnsmasq](https://thekelleys.org.uk/dnsmasq/doc.html) |
||
| 18 | |||
| 19 | ## Implementation |
||
| 20 | |||
| 21 | 1. Install dnsmasq with [homebrew](https://brew.sh): `brew install dnsmasq` |
||
| 22 | 1. configure it: |
||
| 23 | |||
| 24 | ```shell |
||
| 25 | cp /opt/homebrew/etc/dnsmasq.conf /opt/homebrew/etc/dnsmasq.conf.orig |
||
| 26 | echo "conf-dir=/opt/homebrew/etc/dnsmasq.d/,*.conf" | tee /opt/homebrew/etc/dnsmasq.conf |
||
| 27 | ``` |
||
| 28 | |||
| 29 | 1. generate at least one DNS file in `/opt/homebrew/etc/dnsmasq.d/`: |
||
| 30 | |||
| 31 | local.conf: |
||
| 32 | |||
| 33 | ```conf |
||
| 34 | # 192.168.50 is my local WLAN |
||
| 35 | # for Parlament VM's (macbook air) |
||
| 36 | address=/testvm1.local/192.168.50.47 |
||
| 37 | address=/testvm2.local/192.168.50.152 |
||
| 38 | address=/testvm3.local/192.168.50.32 |
||
| 39 | # testvm has 3 addresses |
||
| 40 | address=/testvm.local/192.168.50.47 |
||
| 41 | address=/testvm.local/192.168.50.152 |
||
| 42 | address=/testvm.local/192.168.50.32 |
||
| 43 | # 192.168.205 is UTM.app local |
||
| 44 | # local machines here |
||
| 45 | address=/alma-arm-vm.local/192.168.205.7 |
||
| 46 | address=/alma-intel-vm.local/192.168.205.8 |
||
| 47 | # 192.168.254 is VMWare Fusion local |
||
| 48 | address=/almavm.local/192.168.254.129 |
||
| 49 | ``` |
||
| 50 | |||
| 51 | pflaeging.net.conf: |
||
| 52 | |||
| 53 | ```conf |
||
| 54 | # 192.168.254 is VMWare Fusion local |
||
| 55 | address=/gubernat1.pflaeging.net/192.168.254.130 |
||
| 56 | address=/ms1.pflaeging.net/192.168.254.131 |
||
| 57 | # this is a wildcard |
||
| 58 | address=/.gubernat1.pflaeging.net/192.168.254.130 |
||
| 59 | # 192.168.50 is my local WLAN |
||
| 60 | # gubernat VM's on MacBook Air |
||
| 61 | address=/rhel9-gubernat.pflaeging.net/192.168.50.173 |
||
| 62 | # this is a wildcard |
||
| 63 | address=/.rg.pflaeging.net/192.168.50.173 |
||
| 64 | ``` |
||
| 65 | |||
| 66 | 1. start your dnsmasq service: `sudo brew services start dnsmasq`` |
||
| 67 | |||
| 68 | 1. make Mac OS ready for a custom resolver: `sudo mkdir -p /etc/resolver` |
||
| 69 | |||
| 70 | 1. set a pointer to your new resolvers and make them in high priority: |
||
| 71 | |||
| 72 | contents of `/etc/resolver/local` (filename equals domain!): |
||
| 73 | |||
| 74 | ```conf |
||
| 75 | nameserver 127.0.0.1 |
||
| 76 | search_order 1 |
||
| 77 | ```` |
||
| 78 | |||
| 79 | Then create a file for each domain you configured above with the same content. |
||
| 80 | (in this use case it would be `/etc/resolver/pflaeging.net`) |
||
| 81 | |||
| 82 | 1. to be sure everythings working kick the original resolver and clean the caches: |
||
| 83 | |||
| 84 | ```shell |
||
| 85 | sudo dscacheutil -flushcache |
||
| 86 | sudo killall -HUP mDNSResponder |
||
| 87 | ``` |
||
| 88 | |||
| 89 | 1. when I change or add things in my dnsmasq config I'm restarting the dnsmasq job. This is not required but the easiest way: `sudo pkill dnsmasq` |
||
| 90 | |||
| 91 | ## Debugging |
||
| 92 | |||
| 93 | - Show resolver config: `scutil --dns` |
||
| 94 | - Query the DNS resolver from the commandline: ` dscacheutil -q host -a name myfunnyname.domain.net` |
||
| 95 | - Query the local dnsmasq instance: `dig myfunnyname.domain.net @localhost` |