When troubleshooting network connectivity or configuring DNS settings in Linux, many administrators instinctively check /etc/resolv.conf
first. However, modern Linux distributions using NetworkManager or systemd-resolved may not store DHCP-assigned DNS servers in this traditional location.
Here are several reliable approaches to identify DNS servers provided by DHCP:
Method 1: Using nmcli (NetworkManager)
For systems using NetworkManager:
nmcli dev show | grep DNS
Example output:
IP4.DNS[1]: 192.168.1.1 IP4.DNS[2]: 8.8.8.8
Method 2: Using resolvectl (systemd-resolved)
On systems with systemd-resolved:
resolvectl status
This provides comprehensive DNS information including DHCP-assigned servers.
Method 3: Querying DHCP Lease Files
Directly examining DHCP lease information:
cat /var/lib/NetworkManager/dhclient-*.lease | grep dhcp-server-identifier
Or for systemd-networkd:
journalctl -u systemd-networkd | grep "DNS server"
For scripting or programmatic access:
Python Example
import subprocess def get_dhcp_dns(): result = subprocess.run(['nmcli', 'dev', 'show'], capture_output=True, text=True) return [line.split()[-1] for line in result.stdout.splitlines() if 'DNS' in line] print(get_dhcp_dns())
Bash One-Liner
nmcli -f IP4.DNS dev show $(nmcli -g DEVICE dev) | awk '{print $2}'
The results may show multiple DNS servers - typically the first is the primary, while others are fallbacks. Servers marked with "DHCP" or appearing in lease files are DHCP-assigned, while static configurations may appear in network scripts.
When working with DHCP-configured network interfaces in Linux, DNS servers are typically stored in /etc/resolv.conf
. However, modern Linux distributions using systemd-resolved, NetworkManager, or other network management tools may not populate this file directly. Here's how to reliably discover your DHCP-assigned DNS servers through various methods.
For systems using NetworkManager, this is the most straightforward approach:
nmcli dev show | grep DNS
Example output showing both IPv4 and IPv6 DNS servers:
IP4.DNS[1]: 192.168.1.1
IP6.DNS[1]: 2606:4700:4700::1111
On systems using systemd-resolved (common in Ubuntu and others):
systemd-resolve --status | grep "DNS Servers" -A2
Alternatively, query the DNS configuration directly:
resolvectl dns
For systems not using NetworkManager, check the DHCP lease files:
cat /var/lib/dhcp/dhclient.leases | grep domain-name-servers
Or for a specific interface (e.g., eth0):
cat /var/lib/dhcp/dhclient.eth0.leases | grep domain-name-servers
You can also find DNS information through the iproute2 utilities:
ip route show dev eth0 | grep via
While this shows the gateway rather than DNS, it's often the same address in home networks.
For programmatic access, use NetworkManager's D-Bus API:
import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
manager = dbus.Interface(proxy, 'org.freedesktop.NetworkManager')
active_connections = manager.GetActiveConnections()
for ac_path in active_connections:
ac_proxy = bus.get_object('org.freedesktop.NetworkManager', ac_path)
ac = dbus.Interface(ac_proxy, 'org.freedesktop.NetworkManager.Connection.Active')
ip4_config = ac.Ip4Config
if ip4_config:
ip4_proxy = bus.get_object('org.freedesktop.NetworkManager', ip4_config)
ip4 = dbus.Interface(ip4_proxy, 'org.freedesktop.NetworkManager.IP4Config')
print("DNS servers:", ip4.Get('org.freedesktop.NetworkManager.IP4Config', 'Nameservers'))
For systems using dhclient
directly, check the lease acquisition process:
dhclient -v eth0
The verbose output will show the received DNS server information during the DHCP negotiation.