Ubuntu系统DNS配置方法及临时域名解析失败问题求助
Hey there! As someone new to Ubuntu and virtual machines, it totally makes sense that DNS configuration feels a bit confusing at first. Let's start with why you might be seeing that "temporary failure in name resolution" error, then walk through step-by-step how to fix your DNS settings.
- Virtual Machine Network Mode Issues: If you're using NAT mode, your VM relies on the host machine's DNS settings—if the host has DNS problems, your VM will too. For bridge mode, your VM might not be getting valid network parameters (including DNS) from your router.
- Invalid DNS Server Configuration: Your Ubuntu system might not have been assigned a working DNS server, or the one it's using is temporarily unreachable.
- Unstable Network Connection: Occasional drops in the network link between your VM and the host (or router) can cause temporary resolution failures.
- Stale DNS Cache: Old or incorrect cached DNS data can lead to failed lookups, even if your actual DNS settings are correct.
We'll cover both temporary fixes (great for testing) and permanent configurations (so settings stick after reboots).
Temporary DNS Configuration (Resets After Reboot)
This is perfect for quickly testing if a working DNS server fixes your issue:
- Open the resolv.conf file with a text editor:
sudo nano /etc/resolv.conf - Add one or more public DNS servers (like Google's or Cloudflare's) by adding lines like:
nameserver 8.8.8.8 nameserver 1.1.1.1 - Save the file (press
Ctrl+O, thenEnter) and exit (Ctrl+X). - Test if it works by running:
ping google.com
Note: On newer Ubuntu versions, /etc/resolv.conf is often a symlink managed by systemd-resolved, so this change will be overwritten on reboot. Use the permanent methods below for long-term fixes.
Permanent DNS Configuration
Method 1: Using systemd-resolved (Default for Ubuntu 18.04+)
Most modern Ubuntu systems use systemd-resolved to manage DNS:
- Open the resolved configuration file:
sudo nano /etc/systemd/resolved.conf - Uncomment the lines starting with
DNS=andFallbackDNS=, then add your preferred DNS servers:DNS=8.8.8.8 1.1.1.1 FallbackDNS=8.8.4.4 1.0.0.1 - Save and exit the file.
- Restart the
systemd-resolvedservice to apply changes:sudo systemctl restart systemd-resolved - Verify your settings with:
Look for the "DNS Servers" section to confirm your chosen servers are listed.systemd-resolve --status
Method 2: Using Netplan (For Network-Managed Systems)
If your Ubuntu uses Netplan (common in newer server/desktop versions):
- Find your Netplan config file in
/etc/netplan/—it usually looks like00-installer-config.yamlor01-netcfg.yaml. - Edit the file with:
sudo nano /etc/netplan/00-installer-config.yaml - Add the
nameserverssection under your network interface (replaceenp0s3with your actual interface name, found viaip a):network: ethernets: enp0s3: dhcp4: true nameservers: addresses: [8.8.8.8, 1.1.1.1] version: 2 - Save and exit, then apply the configuration:
sudo netplan apply - Verify with
systemd-resolve --statusto confirm DNS settings.
Method 3: Manual resolv.conf (Bypassing systemd-resolved)
If you prefer to manage /etc/resolv.conf directly:
- Stop and disable the
systemd-resolvedservice:sudo systemctl stop systemd-resolved sudo systemctl disable systemd-resolved - Remove the symlinked
/etc/resolv.conf:sudo rm /etc/resolv.conf - Create a new static resolv.conf file:
sudo nano /etc/resolv.conf - Add your DNS servers:
nameserver 8.8.8.8 nameserver 1.1.1.1 - Save and exit, then lock the file to prevent it from being overwritten:
sudo chattr +i /etc/resolv.conf
If you still see issues after configuring DNS, try flushing the DNS cache:
sudo systemd-resolve --flush-caches
Then test again with nslookup google.com to see if resolution works.
内容的提问来源于stack exchange,提问作者克里斯汀




