Skip to content

Commit 29e63be

Browse files
committed
Use DNS and proxy settings from first interface with an IPv4 address
Instead of always preferring en0. Signed-off-by: Jan Dubois <[email protected]>
1 parent 53b78dc commit 29e63be

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

docs/network.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ DNS over tcp is rarely used. It is usually only used either when user explicitly
4040

4141
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.
4242

43-
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).
43+
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).
4444

4545
## `vde_vmnet` (192.168.105.0/24)
4646

pkg/limayaml/default.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ hostResolver:
252252
# If useHostResolver is false, then the following rules apply for configuring dns:
253253
# Explicitly set DNS addresses for qemu user-mode networking. By default qemu picks *one*
254254
# nameserver from the host config and forwards all queries to this server. On macOS
255-
# Lima adds the nameservers configured for the "en0" interface to the list. In case this
256-
# still doesn't work (e.g. VPN setups), the servers can be specified here explicitly.
257-
# If nameservers are specified here, then the "en0" configuration will be ignored.
255+
# Lima adds the nameservers configured for the first host interface in service order,
256+
# that has an IPv4 address, to the list. In case this still doesn't work (e.g. VPN
257+
# setups), the servers can be specified here explicitly. If nameservers are specified
258+
# here, then the configuration from network preferences will be ignored.
258259
# dns:
259260
# - 1.1.1.1
260261
# - 1.0.0.1

pkg/osutil/dns_darwin.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ func DNSAddresses() ([]string, error) {
1414
}
1515
var addresses []string
1616
if len(nwData) > 0 {
17-
// Return DNS addresses from en0 interface
17+
// Return DNS addresses from the first interface that has an IPv4 address.
18+
// The networks are in service order already.
1819
for _, nw := range nwData {
19-
if nw.Interface == "en0" {
20+
if len(nw.IPv4.Addresses) > 0 {
2021
addresses = nw.DNS.ServerAddresses
2122
break
2223
}
2324
}
24-
// In case "en0" is not found, use the addresses of the first interface
25-
if len(addresses) == 0 {
26-
addresses = nwData[0].DNS.ServerAddresses
27-
}
2825
}
2926
return addresses, nil
3027
}
@@ -48,10 +45,11 @@ func ProxySettings() (map[string]string, error) {
4845
}
4946
env := make(map[string]string)
5047
if len(nwData) > 0 {
51-
// In case "en0" is not found, use the proxies of the first interface
52-
proxies := nwData[0].Proxies
48+
// Return proxy settings from the first interface that has an IPv4 address.
49+
// The networks are in service order already.
50+
var proxies sysprof.Proxies
5351
for _, nw := range nwData {
54-
if nw.Interface == "en0" {
52+
if len(nw.IPv4.Addresses) > 0 {
5553
proxies = nw.Proxies
5654
break
5755
}

pkg/sysprof/network_darwin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ type SPNetworkDataType struct {
77
type NetworkDataType struct {
88
DNS DNS `json:"DNS"`
99
Interface string `json:"interface"`
10+
IPv4 IPv4 `json:"IPv4,omitempty"`
1011
Proxies Proxies `json:"Proxies"`
1112
}
1213

1314
type DNS struct {
1415
ServerAddresses []string `json:"ServerAddresses"`
1516
}
1617

18+
type IPv4 struct {
19+
Addresses []string `json:"Addresses,omitempty"`
20+
}
21+
1722
type Proxies struct {
1823
ExceptionList []string `json:"ExceptionList"` // default: ["*.local", "169.254/16"]
1924
FTPEnable string `json:"FTPEnable"`

0 commit comments

Comments
 (0)