[PR #1494] Restore dns on unclean shutdown #3133

Open
opened 2025-11-20 08:05:03 -05:00 by saavagebueno · 0 comments
Owner

Original Pull Request: https://github.com/netbirdio/netbird/pull/1494

State: closed
Merged: Yes


Describe your changes

  1. When the Netbird agent terminates ungracefully (kill, power outage, panic, etc.), the system DNS settings are not restored.

    This can result in broken DNS resolution (e.g. a DNS server provided by a peer), in which case the agent won't be able to connect to the management server.

    To fix this issue we create an indicator file after adjusting the system DNS settings.
    This file is deleted once the settings are restored.

    If the agent terminates ungracefully it won't be able to delete this file. Therefore when it is started again it will detect the existence of this file and restore the previous DNS state.

    • On Linux

      /var/lib/netbird/resolv.conf, contains the old /etc/resolv.conf
      /var/lib/netbird/manager, contains the name of the previous host manager and last netbird IP address

      • manager file

        DNS settings are restored by copying the /var/lib/netbird/resolv.conf back to /etc/resolv.conf

        Test:

        • Uninstall systemd-resolved, openresolv/resolvconf
        • echo nameserver 9.9.9.9 | sudo tee /etc/resolv.conf
        • Watch /var/log/netbird/client.log for restore message and /etc/resolv.conf
        • Start agent
        • killall -9 netbird
        • Start agent
      • manager resolvconf

        DNS settings are restored by running restoreHostDNS() (runs resolvconf -d <interface>)

        Test:

        • Uninstall systemd-resolved
        • Install openresolv package
        • echo "nameserver 9.9.9.9" | sudo resolvconf -x -a <physical int>
        • Watch /var/log/netbird/client.log for restore message and /etc/resolv.conf
        • Start agent
        • killall -9 netbird
        • Start agent
      • manager systemd

        DNS settings are restored by running restoreHostDNS() (runs dbus methods`)

        Test:

        • Watch /var/log/netbird/client.log for restore message and resolvectl status
        • Start agent
        • killall -9 netbird
        • Start agent
      • manager networkManager

        DNS settings are restored by running restoreHostDNS() (runs dbus methods`)

        Test:

        • Uninstall systemd-resolved
        • Edit /etc/NetworkManager/NetworkManager.conf, add dns=dnsmasq in the main section
        • sudo nmcli general reload or sudo nmcli networking off; sudo nmcli networking on
        • Watch /var/log/netbird/client.log for restore message and nmcli device show | grep -i dns
        • Start agent
        • killall -9 netbird
        • Start agent
    • On Windows

      %PROGRAMDATA%\Netbird\unclean_shutdown_dns.txt, contains the interface guid that is required to set up the host manager.

      DNS settings are restored by running restoreHostDNS() (removes registry keys)

      Test:

      • Watch %PROGRAMDATA%\Netbird\client.log for restore message and SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig\NetBird-Match registry
      • Start agent
      • Start PowerShell as admin
      • Get-Process -Name netbird | Stop-Process -Force
      • (Re)start agent
    • On macOS

      /var/lib/netbird/unclean_shutdown_dns, empty

      DNS settings are restored by running restoreHostDNS() (runs scutil remove).

      Test:

      • Watch /var/log/netbird/client.log for restore message and scutil --dns for DNS settings
      • Start agent
      • sudo killall -9 netbird
      • Start agent
    • mobile should not be affected

  2. Moves the restore DNS logic before the remove interface logic so we don't get errors for missing interfaces

  3. Improves error handling and error messages for debugging

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)
  • Extended the README / documentation, if necessary
**Original Pull Request:** https://github.com/netbirdio/netbird/pull/1494 **State:** closed **Merged:** Yes --- ## Describe your changes 1. When the Netbird agent terminates ungracefully (kill, power outage, panic, etc.), the system DNS settings are not restored. This can result in broken DNS resolution (e.g. a DNS server provided by a peer), in which case the agent won't be able to connect to the management server. To fix this issue we create an indicator file after adjusting the system DNS settings. This file is deleted once the settings are restored. If the agent terminates ungracefully it won't be able to delete this file. Therefore when it is started again it will detect the existence of this file and restore the previous DNS state. - On Linux `/var/lib/netbird/resolv.conf`, contains the old `/etc/resolv.conf` `/var/lib/netbird/manager`, contains the name of the previous host manager and last netbird IP address - manager `file` DNS settings are restored by copying the `/var/lib/netbird/resolv.conf` back to `/etc/resolv.conf` Test: - Uninstall `systemd-resolved`, `openresolv`/`resolvconf` - `echo nameserver 9.9.9.9 | sudo tee /etc/resolv.conf` - Watch `/var/log/netbird/client.log` for restore message and `/etc/resolv.conf` - Start agent - `killall -9 netbird` - Start agent - manager `resolvconf` DNS settings are restored by running `restoreHostDNS()` (runs `resolvconf -d <interface>`) Test: - Uninstall `systemd-resolved` - Install `openresolv` package - `echo "nameserver 9.9.9.9" | sudo resolvconf -x -a <physical int>` - Watch `/var/log/netbird/client.log` for restore message and `/etc/resolv.conf` - Start agent - `killall -9 netbird` - Start agent - manager `systemd` DNS settings are restored by running `restoreHostDNS()` (runs dbus methods`) Test: - Watch `/var/log/netbird/client.log` for restore message and `resolvectl status` - Start agent - `killall -9 netbird` - Start agent - manager `networkManager` DNS settings are restored by running `restoreHostDNS()` (runs dbus methods`) Test: - Uninstall `systemd-resolved` - Edit `/etc/NetworkManager/NetworkManager.conf`, add `dns=dnsmasq` in the main section - `sudo nmcli general reload` or `sudo nmcli networking off; sudo nmcli networking on` - Watch `/var/log/netbird/client.log` for restore message and `nmcli device show | grep -i dns` - Start agent - `killall -9 netbird` - Start agent - On Windows `%PROGRAMDATA%\Netbird\unclean_shutdown_dns.txt`, contains the interface `guid` that is required to set up the host manager. DNS settings are restored by running `restoreHostDNS()` (removes registry keys) Test: - Watch `%PROGRAMDATA%\Netbird\client.log` for restore message and `SYSTEM\CurrentControlSet\Services\Dnscache\Parameters\DnsPolicyConfig\NetBird-Match` registry - Start agent - Start PowerShell as admin - `Get-Process -Name netbird | Stop-Process -Force` - (Re)start agent - On macOS `/var/lib/netbird/unclean_shutdown_dns`, empty DNS settings are restored by running `restoreHostDNS()` (runs `scutil remove`). Test: - Watch `/var/log/netbird/client.log` for restore message and `scutil --dns` for DNS settings - Start agent - `sudo killall -9 netbird` - Start agent - mobile should not be affected 2. Moves the `restore DNS` logic before the `remove interface` logic so we don't get errors for missing interfaces 3. Improves error handling and error messages for debugging ### Checklist - [x] Is it a bug fix - [ ] Is a typo/documentation fix - [ ] Is a feature enhancement - [ ] It is a refactor - [ ] Created tests that fail without the change (if possible) - [ ] Extended the README / documentation, if necessary
saavagebueno added the pull-request label 2025-11-20 08:05:03 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: SVI/netbird#3133