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-hostnamecommand updates/etc/hostnamebut 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:
- First, determine your machine’s exact active hostname by running:
hostname(Assume the terminal prints:
ubuntu-server) - Open the hosts file in nano with elevated permissions:
sudo nano /etc/hosts - 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 - 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). - If no line for
127.0.1.1exists, add it immediately beneath the127.0.0.1 localhostline. - Save the file by pressing Ctrl + O and Enter. Exit nano with Ctrl + X.
- Test your changes by running any sudo command:
sudo trueThe 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:
- Choose your desired new hostname (e.g.,
my-workstation). - Set the hostname via systemd:
sudo hostnamectl set-hostname my-workstation - Open
/etc/hosts:sudo nano /etc/hosts - Update the
127.0.1.1line to match:127.0.1.1 my-workstation - 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:
- Open the cloud-init configuration file:
sudo nano /etc/cloud/cloud.cfg - Locate the directive:
preserve_hostname: false - Change it to:
preserve_hostname: true - Save the file. This prevents cloud-init from discarding your manual hostname definitions during subsequent server restarts.