How to Fix ‘Unable to Resolve Host’ Warning in Ubuntu Linux

Executing administrative commands with sudo on an Ubuntu desktop or remote cloud server often prints an irritating delay and terminal warning: sudo: unable to resolve host [hostname]: Name or service not known. Although the command usually completes after a brief hesitation, this warning repeats on every elevated invocation, scripts experience DNS timeout latency, and automated background cron tasks spam internal system logs with resolution failures. This warning occurs when the operating system’s configured system hostname is absent from the local host name resolution file located at /etc/hosts.

Fixing the ‘unable to resolve host’ warning requires harmonising your system hostname between /etc/hostname and /etc/hosts.

Why Does Sudo Complain About an Unresolvable Host?

Whenever a user executes sudo, the security subsystem queries the system’s Name Service Switch (NSS) configuration to resolve its own machine name to a loopback IP address (typically 127.0.0.1 or 127.0.1.1). The warning is triggered by several standard system changes:

  • Changing Hostname Without Updating /etc/hosts: Modifying your computer’s name using the hostnamectl set-hostname command updates /etc/hostname but leaves the previous hostname in /etc/hosts.
  • Cloud Server Provisioning Inconsistencies: Cloud providers (AWS, DigitalOcean, Linode, Hetzner) assign dynamic hostnames during initial VPS deployment without automatically writing corresponding loopback aliases.
  • Corrupted or Emptied /etc/hosts File: Manual configuration errors or buggy deployment scripts wiped or commented out the primary localhost records.
  • NetworkManager or Cloud-Init Overwrites: Automated network initialization daemons reset host mapping tables upon system reboot.

Method 1: Map the Hostname in /etc/hosts (Permanent Fix)

To eliminate the warning permanently, link your exact system hostname to the local loopback interface:

  1. First, determine your machine’s exact active hostname by running:
    hostname

    (Assume the terminal prints: ubuntu-server)

  2. Open the hosts file in nano with elevated permissions:
    sudo nano /etc/hosts
  3. Inspect the contents of the file. Standard Ubuntu hosts files should contain the following baseline configuration:
    127.0.0.1   localhost
    127.0.1.1   ubuntu-server
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
  4. Locate the line starting with 127.0.1.1. Replace the existing name on that line with your actual hostname (e.g., 127.0.1.1 ubuntu-server).
  5. If no line for 127.0.1.1 exists, add it immediately beneath the 127.0.0.1 localhost line.
  6. Save the file by pressing Ctrl + O and Enter. Exit nano with Ctrl + X.
  7. Test your changes by running any sudo command:
    sudo true

    The command will execute instantly with zero warnings or output, confirming the fix.

Method 2: Synchronise Using hostnamectl

If you prefer using the modern systemd hostname manager to update both system files cleanly:

  1. Choose your desired new hostname (e.g., my-workstation).
  2. Set the hostname via systemd:
    sudo hostnamectl set-hostname my-workstation
  3. Open /etc/hosts:
    sudo nano /etc/hosts
  4. Update the 127.0.1.1 line to match:
    127.0.1.1   my-workstation
  5. Save and exit. Reboot the machine or restart systemd-hostnamed:
    sudo systemctl restart systemd-hostnamed

Method 3: Resolving Cloud-Init Overwrites on Cloud VPS Instances

On virtual cloud instances running cloud-init (such as AWS EC2 or DigitalOcean Droplets), cloud-init can overwrite your /etc/hosts file upon reboot:

  1. Open the cloud-init configuration file:
    sudo nano /etc/cloud/cloud.cfg
  2. Locate the directive:
    preserve_hostname: false
  3. Change it to:
    preserve_hostname: true
  4. Save the file. This prevents cloud-init from discarding your manual hostname definitions during subsequent server restarts.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.