diff --git a/docs/network.md b/docs/network.md index 4751a37932e..0fe3a6dbfa6 100644 --- a/docs/network.md +++ b/docs/network.md @@ -40,7 +40,7 @@ DNS over tcp is rarely used. It is usually only used either when user explicitly During initial cloud-init bootstrap, `iptables` may not yet be installed. In that case the repo server is determined using the slirp DNS. After `iptables` has been installed, the forwarding rule is applied, switching over to the hostagent DNS. -If `useHostResoler` is false, then DNS servers can be configured manually in `lima.yaml` via the `dns` setting. If that list is empty, then Lima will either use the slirp DNS (on Linux), or the nameservers from the `en0` host interface (on macOS). +If `useHostResoler` is false, then DNS servers can be configured manually in `lima.yaml` via the `dns` setting. If that list is empty, then Lima will either use the slirp DNS (on Linux), or the nameservers from the first host interface in service order that has an assigned IPv4 address (on macOS). ## `vde_vmnet` (192.168.105.0/24) diff --git a/pkg/limayaml/default.yaml b/pkg/limayaml/default.yaml index 4236b6f9e62..39b640cef52 100644 --- a/pkg/limayaml/default.yaml +++ b/pkg/limayaml/default.yaml @@ -252,9 +252,10 @@ hostResolver: # If useHostResolver is false, then the following rules apply for configuring dns: # Explicitly set DNS addresses for qemu user-mode networking. By default qemu picks *one* # nameserver from the host config and forwards all queries to this server. On macOS -# Lima adds the nameservers configured for the "en0" interface to the list. In case this -# still doesn't work (e.g. VPN setups), the servers can be specified here explicitly. -# If nameservers are specified here, then the "en0" configuration will be ignored. +# Lima adds the nameservers configured for the first host interface in service order, +# that has an IPv4 address, to the list. In case this still doesn't work (e.g. VPN +# setups), the servers can be specified here explicitly. If nameservers are specified +# here, then the configuration from network preferences will be ignored. # dns: # - 1.1.1.1 # - 1.0.0.1 diff --git a/pkg/osutil/dns_darwin.go b/pkg/osutil/dns_darwin.go index 52f2fec8bd9..aa954bdc1a2 100644 --- a/pkg/osutil/dns_darwin.go +++ b/pkg/osutil/dns_darwin.go @@ -14,17 +14,14 @@ func DNSAddresses() ([]string, error) { } var addresses []string if len(nwData) > 0 { - // Return DNS addresses from en0 interface + // Return DNS addresses from the first interface that has an IPv4 address. + // The networks are in service order already. for _, nw := range nwData { - if nw.Interface == "en0" { + if len(nw.IPv4.Addresses) > 0 { addresses = nw.DNS.ServerAddresses break } } - // In case "en0" is not found, use the addresses of the first interface - if len(addresses) == 0 { - addresses = nwData[0].DNS.ServerAddresses - } } return addresses, nil } @@ -48,10 +45,11 @@ func ProxySettings() (map[string]string, error) { } env := make(map[string]string) if len(nwData) > 0 { - // In case "en0" is not found, use the proxies of the first interface - proxies := nwData[0].Proxies + // Return proxy settings from the first interface that has an IPv4 address. + // The networks are in service order already. + var proxies sysprof.Proxies for _, nw := range nwData { - if nw.Interface == "en0" { + if len(nw.IPv4.Addresses) > 0 { proxies = nw.Proxies break } diff --git a/pkg/sysprof/network_darwin.go b/pkg/sysprof/network_darwin.go index 9e49a512959..3e223a35442 100644 --- a/pkg/sysprof/network_darwin.go +++ b/pkg/sysprof/network_darwin.go @@ -7,6 +7,7 @@ type SPNetworkDataType struct { type NetworkDataType struct { DNS DNS `json:"DNS"` Interface string `json:"interface"` + IPv4 IPv4 `json:"IPv4,omitempty"` Proxies Proxies `json:"Proxies"` } @@ -14,6 +15,10 @@ type DNS struct { ServerAddresses []string `json:"ServerAddresses"` } +type IPv4 struct { + Addresses []string `json:"Addresses,omitempty"` +} + type Proxies struct { ExceptionList []string `json:"ExceptionList"` // default: ["*.local", "169.254/16"] FTPEnable string `json:"FTPEnable"`