Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk8 min

Fixing “sudo: unable to resolve host” on Ubuntu

The sudo hostname warning is usually a local name mismatch. Learn how to diagnose and repair the three hostname sources, verify resolution, and stop cloud-init from undoing the fix.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The warning means Ubuntu’s current hostname does not match a name in the local hosts database. It is usually a local configuration mismatch—not evidence that your internet connection or public DNS is broken. Compare the runtime hostname, /etc/hostname, and /etc/hosts, then make the intended name resolve locally.

What the warning means

When you run a command through sudo, Ubuntu can perform a hostname lookup. If the machine calls itself one name but the local hosts database maps a different name, sudo prints sudo: unable to resolve host …. The command may still run, but the warning identifies a real inconsistency that can confuse local services, logs, scripts, and automation.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Ubuntu Community Help Wiki describes the cause this way: “This is because sudo requires DNS lookups in certain cases and it cannot resolve your newly set hostname..” The lookup is often satisfied entirely by /etc/hosts; no external DNS query is required for the computer’s own name.

The three names to compare

Location or command What it represents What should agree
hostname The hostname currently active in the running kernel. It should be the name you intend the system to use now.
hostnamectl --static The systemd static hostname setting. On a traditional installation, it normally corresponds to /etc/hostname.
/etc/hostname The configured hostname read during boot on traditional Ubuntu installations. It should contain the intended hostname, normally as one line.
/etc/hosts Local address-to-name mappings used before or alongside DNS. The active hostname should have a local address entry, commonly 127.0.1.1.

A mismatch in any one of these locations can produce the warning. Editing only one file may appear to fix the message until the next boot or until another service regenerates the file.

Inspect the configuration before changing it

Run these read-only checks first. They show the active name, the configured name, the contents of the hostname file, and the relevant address lines in the hosts file.

hostname
hostnamectl --static
cat /etc/hostname
grep -nE '(^127.0.1.1|^127.0.0.1|::1)' /etc/hosts

Compare the spelling, punctuation, and domain suffix in the output. Hostnames are commonly short names such as web-01, but a fully qualified name may also be used. Choose one intended name and use it consistently. A trailing period, an accidental space, or a different hyphen or underscore can make two visually similar values different to the resolver.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read the hosts-file output correctly

  • Keep a line mapping 127.0.0.1 to localhost.
  • Keep the IPv6 loopback mapping, normally ::1 localhost (often with additional standard aliases).
  • For a typical standalone or local server, add the machine’s active name on a separate 127.0.1.1 line.
  • Do not replace the localhost entries with the machine name; localhost and the system hostname serve different purposes.

Repair a normal Ubuntu installation

Make a backup before editing a system file. The following sequence avoids losing the original configuration and keeps the change easy to undo.

  1. Choose the intended hostname. Use the value you want shown by hostname and in system logs. Avoid spaces; use a conventional hostname made of letters, numbers, and hyphens.
  2. Back up the hosts file. From an administrative shell, run cp -a /etc/hosts /etc/hosts.backup. If the command is run through sudo and the warning appears, that warning is expected until the mismatch is corrected.
  3. Set the system hostname when necessary. The systemd interface for changing the static hostname is:
sudo hostnamectl set-hostname your-hostname

hostnamectl is documented as a way to “query and change the system hostname and related settings.” On traditional installs, this updates the configured static hostname as well as the running setting. If you intend to keep the current runtime name, do not run this command; instead, make the hosts entry match that name.

  1. Make the local mapping agree. Edit /etc/hosts with your preferred editor. Preserve the existing localhost mappings and add or correct a line like this:
127.0.0.1   localhost
127.0.1.1   your-hostname
::1         localhost

The spacing is not significant, but each address and name must be separated by whitespace. If the file already has a 127.0.1.1 line, replace the old hostname there rather than adding several competing entries for the same address.

  1. Check /etc/hostname. On a traditional installation, it should contain exactly the chosen short hostname, followed by a newline. If it still contains an old value, update it from an administrative shell. If the file does not exist on a cloud image, read the cloud-init section below before creating it; some images deliberately manage hostname creation differently.
  2. Save the files and close the editor. A malformed hosts file can affect more than sudo, so avoid deleting unrelated valid entries.

If sudo will not let you edit the files

The warning does not normally prevent sudo from authenticating, but a locked-down system or an expired credential can. Obtain an already-open root shell, use the distribution’s recovery mode, or use another approved privileged path, then edit /etc/hosts and /etc/hostname from that shell. Do not change permissions on the files or grant broad write access just to silence the warning.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Verify both name resolution and sudo

After saving the changes, test the exact name you placed in /etc/hosts:

getent hosts your-hostname
sudo -n true
sudo hostnamectl status
  • getent hosts your-hostname should return a line containing the local address, commonly 127.0.1.1. It verifies the system’s normal name-service path rather than merely displaying a file.
  • sudo -n true performs a non-interactive sudo check. A successful exit with no hostname warning confirms that the lookup is no longer producing the message. It can still fail if a password is required and no cached credential is available; that is an authentication result, not proof that hostname resolution is broken.
  • sudo hostnamectl status displays the static and transient hostname information. Confirm that the name shown is the one represented in /etc/hosts.

If getent returns no result for the chosen name, recheck for a spelling difference and make sure the entry is not commented out with #. If it returns an unexpected address, inspect duplicate entries and remove stale mappings that no longer describe the machine.

Why the warning returns after a reboot

A reboot can expose a mismatch that was temporarily corrected at runtime. The most common cause on virtual machines is cloud-init, which may rewrite /etc/hosts or /etc/hostname during boot.

cloud-init hosts management modes

cloud-init’s hosts module documents three relevant modes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Setting Behavior on boot What a persistent fix requires
manage_etc_hosts: false The default mode; cloud-init leaves /etc/hosts alone. Edit the file normally, provided another service is not managing it.
manage_etc_hosts: true cloud-init generates the hosts file from /etc/cloud/templates/hosts.tmpl on each boot. Put the desired mapping in that template or supply equivalent cloud-init user data. Editing only the generated file will be overwritten.
manage_etc_hosts: localhost cloud-init ensures a hostname entry is present on each boot. Configure the hostname and cloud-init data so the generated entry uses the intended name.

The cloud-init documentation states: “If set to true, cloud-init will generate the hosts file using the template located at /etc/cloud/templates/hosts.tmpl.” That template is therefore the durable edit point when the module is enabled.

cloud-init hostname settings

The hostname module can update /etc/hostname. The preserve_hostname: true option tells cloud-init not to change the hostname, while create_hostname_file controls whether cloud-init creates the file. If cloud-init is intended to own the hostname, configure those options in user data rather than repeatedly repairing the file after every boot.

For a cloud-init-managed hosts file, a template containing the required local mappings can include:

127.0.0.1   localhost
127.0.1.1   your-hostname
::1         localhost

Use the image’s existing template structure and retain any provider-specific entries that other services require. Changing the template affects future generations of the file; it does not necessarily rewrite the current file until the relevant cloud-init stage runs again.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Ubuntu 24.04 LTS and later images on Google Compute Engine

Ubuntu 24.04 LTS and later GCE images have a release-specific behavior: /etc/hostname may be absent, and create_hostname_file defaults to false. In that situation, setting a hostname without creating the file can be transient. A later DHCP cycle or boot sequence may replace it, bringing the warning back.

When a persistent configured hostname is required on these images, set create_hostname_file: true through the image’s cloud-init configuration and ensure the hosts mapping is generated or maintained with the same name. If cloud-init should not replace an administrator-selected name, use preserve_hostname: true as appropriate. Test persistence by rebooting and repeating the inspection commands, not by assuming that a successful change in the current session will survive.

Common mistakes and their fixes

Changing only the runtime hostname

A command that changes the running name can make the warning disappear briefly, but a boot-time file or cloud-init configuration may still contain the old value. Compare all three locations again and make the persistent source agree.

Replacing localhost instead of adding the machine name

Deleting 127.0.0.1 localhost or ::1 localhost to make room for the system name can break software that expects loopback resolution. Keep localhost entries and add the active hostname separately, normally on 127.0.1.1.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leaving stale duplicate entries

Multiple lines for the same hostname can produce an address different from the one you expect. Remove obsolete lines after confirming which address the machine should use. Then run getent hosts your-hostname and check the returned address.

Editing a generated file on a cloud VM

If cloud-init has manage_etc_hosts enabled, a manual edit to /etc/hosts is temporary by design. Change /etc/cloud/templates/hosts.tmpl or the instance’s cloud-init user data instead.

Assuming the warning proves public DNS is broken

This message concerns the machine’s own local name lookup. It does not, by itself, show that external domains fail, that a DNS server is unavailable, or that the network interface is down. Test public DNS separately only if applications also report external-resolution failures.

Using a fully qualified name in one place and a short name in another

Decide whether the system should identify itself by a short hostname, a fully qualified name, or both as aliases. Put the exact active name in the hosts entry, and add an alias only when it is intentionally used by local software. Inconsistent suffixes are a frequent reason that a seemingly correct edit does not resolve the name sudo requests.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A reboot-proof checklist

  • Record the output of hostname and hostnamectl --static.
  • Confirm the intended value in /etc/hostname, when that file is used by the image.
  • Keep valid localhost entries for IPv4 and IPv6.
  • Map the active hostname locally, commonly with 127.0.1.1 your-hostname.
  • Run getent hosts your-hostname before testing sudo.
  • On cloud instances, determine whether cloud-init manages either file.
  • If it does, edit the documented template or user data and set hostname-file behavior deliberately.
  • On Ubuntu 24.04 LTS and later GCE images, account for the default create_hostname_file: false behavior when persistence matters.
  • Reboot once, then repeat the inspection and verification commands to confirm the fix survives the boot process.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.