Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
VGSources
Blog

Fixing “sudo: unable to resolve host” on Ubuntu

Ubuntu’s sudo hostname warning usually comes from a mismatch between the active hostname and /etc/hosts. Follow the checks, repair the mapping, and configure cloud-init for persistence.
Length7 min Posted Quest giverVGSources Team

Free tools Windows power users keep installed

One-click scans. No signup required.

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

The warning means Ubuntu’s active hostname does not match an entry in the local hosts database. It is usually a local naming mismatch, not evidence that your internet DNS is broken.

Check the runtime hostname, /etc/hostname, and /etc/hosts. Make the intended name consistent, keep the standard localhost entries, add a matching mapping such as 127.0.1.1 your-hostname, then verify with getent and sudo. On cloud virtual machines, configure cloud-init as well or the warning can return after reboot.

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.

What “sudo: unable to resolve host” actually means

When sudo starts, it can perform a hostname lookup. Ubuntu’s local networking guidance explains that a newly assigned hostname must also be represented in /etc/hosts. If the machine’s current name cannot be resolved through that local database, sudo prints a warning before running the requested command.

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 message does not, by itself, show that public DNS, your router, or the wider internet is malfunctioning. The lookup that failed is normally the system’s own name-to-address mapping. Three values determine whether that mapping works:

Value Location or command Purpose
Runtime hostname hostname The name the running kernel currently uses.
Static/configured hostname hostnamectl --static and traditionally /etc/hostname The name intended to be applied at boot on conventional Ubuntu installations.
Local address mapping /etc/hosts Associates the hostname and any aliases with a local address, commonly 127.0.1.1.

A warning appears when the active name is absent from, misspelled in, or otherwise inconsistent with the hosts file. Changing only one of these locations can leave the mismatch in place.

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

Inspect all three names before changing anything

Start with read-only checks so you know which value is wrong. Run these commands in a terminal:

  1. hostname
  2. hostnamectl --static
  3. cat /etc/hostname
  4. grep -nE '(^127\.0\.1\.1|^127\.0\.0\.1|::1)' /etc/hosts

Compare spelling and punctuation exactly. Hostnames are commonly written with letters, numbers, and hyphens; an underscore, an accidental trailing character, or an old name on one line is enough to make the lookup fail. The first command reports the name in use now. The second reports systemd’s static hostname, while the third shows the file traditionally read during boot. The final command displays the localhost and loopback-related lines without dumping unrelated hosts entries.

If the commands already agree, inspect the complete hosts file for an entry that maps the active name to a local address:

cat /etc/hosts

Do not remove valid localhost mappings while looking for the problem. Ubuntu systems normally need 127.0.0.1 localhost and ::1 localhost; the hostname mapping is an additional entry, not a replacement for either line.

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

Repair the local hosts mapping

Add the active name on 127.0.1.1

For a workstation or local server, the usual repair is a line in /etc/hosts like this:

127.0.1.1   your-hostname

Replace your-hostname with the exact value returned by hostname. Keep the existing entries for 127.0.0.1 localhost and ::1 localhost. If a 127.0.1.1 line already exists with an obsolete name, edit that line rather than creating multiple competing entries for the machine’s primary name.

Edit safely when sudo prints the warning

The warning usually does not prevent sudo from running, so an editor can still be opened with administrative privileges. If sudo cannot complete the command in your environment, use an already-open root shell, the provider’s console, or Ubuntu recovery mode to make the edit. Once you have a root prompt, open the file with an editor and add or correct the mapping:

sudo -i
nano /etc/hosts

Save the file without changing the localhost lines. A minimal, valid arrangement might look like:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
127.0.0.1   localhost
127.0.1.1   your-hostname
::1         localhost

The address on the second line is a conventional local mapping recommended in Ubuntu’s guidance. It allows the machine’s own name to resolve without requiring an external DNS server.

Change the hostname when the name itself is wrong

If the current name is not the one you intend to keep, set the system’s static hostname with systemd’s interface:

sudo hostnamectl set-hostname your-hostname

hostnamectl is designed to query and change the system hostname and related settings. The command changes the system name, but it does not eliminate the need for a matching /etc/hosts entry. After running it, repeat the inspection commands and make sure the new name appears on the 127.0.1.1 line.

If the warning is emitted while you are carrying out this change, use the root-shell or recovery alternatives described above. The important sequence is to settle on one intended spelling, apply it as the static hostname, and then map that same spelling locally.

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

Verify the fix without rebooting

Use these checks after editing both files:

getent hosts your-hostname
sudo -n true
sudo hostnamectl status

getent hosts your-hostname should return an address and the name, proving that the normal name-service path can resolve it locally. sudo -n true performs a non-interactive sudo check; when the configuration is corrected it should complete without printing the unable-to-resolve warning. The final command displays the hostnamectl view so you can confirm that the static and runtime names are the ones you selected.

These are verification commands, not a requirement to contact a DNS provider. If getent still returns nothing, go back to the exact output of hostname and correct the spelling or alias in /etc/hosts. If the command succeeds now but fails after a reboot, a boot-time service is rewriting one of the files.

Why the warning comes back after a reboot on cloud VMs

Cloud-init can regenerate /etc/hosts or change /etc/hostname during boot. An edit made directly to a generated file can therefore appear to work until the next restart.

cloud-init hosts-module modes

Setting Behavior at boot Where to make a durable change
manage_etc_hosts: false The default; cloud-init leaves /etc/hosts alone. Edit /etc/hosts directly after confirming no other service manages it.
manage_etc_hosts: true cloud-init renders the hosts file from /etc/cloud/templates/hosts.tmpl on each boot. Put the required hostname mapping in that template or in cloud-init user data, and keep this mode enabled.
manage_etc_hosts: localhost cloud-init ensures that a hostname entry exists on each boot. Use the cloud-init configuration as the source of truth rather than relying on a one-time manual edit.

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 regeneration explains why a hand-edited /etc/hosts can revert.

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

cloud-init hostname settings

cloud-init’s hostname module can also update /etc/hostname. Set preserve_hostname: true when the name you configured must not be replaced by cloud-init. The create_hostname_file option controls whether cloud-init creates the hostname file at all.

A cloud-init configuration that asks for a persistent name and a managed hosts file can include:

preserve_hostname: true
create_hostname_file: true
manage_etc_hosts: true

Use the options that match your deployment policy. If you choose manage_etc_hosts: true, ensure the corresponding entry is present in /etc/cloud/templates/hosts.tmpl or supplied through user data; changing only the rendered file will not survive the next regeneration.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Ubuntu 24.04 and later GCE images need an extra check

Ubuntu’s Google Compute Engine guidance documents a release-specific behavior: Ubuntu 24.04 LTS and later GCE images may omit /etc/hostname and default create_hostname_file to false. A hostname set without creating that file can be temporary and later be replaced by DHCP.

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

When a configured hostname must persist on one of these images, set create_hostname_file: true in the cloud-init configuration, then ensure the hosts mapping uses the same name. This is separate from the manage_etc_hosts choice: one option controls creation of the hostname file, while the other controls how /etc/hosts is maintained.

Troubleshoot the remaining failure patterns

The warning appears immediately after hostnamectl

hostnamectl set-hostname changed the system name, but the local mapping still contains the old value. Re-run hostname, inspect /etc/hosts, and replace the old primary name on the local mapping line. Keep localhost entries intact.

It is fixed until the next reboot

This pattern points to boot-time management rather than a typo that suddenly returned. On a cloud VM, check whether cloud-init is configured to render hosts entries or to update the hostname. Move the change into the documented template or user data, and set preserve_hostname or create_hostname_file according to whether cloud-init should retain and create the configured name.

/etc/hostname is missing on a GCE Ubuntu 24.04-or-newer image

Do not assume the missing file is harmless if persistence matters. Enable create_hostname_file: true, apply the intended hostname, and add the same name to the managed hosts configuration.

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

getent hosts still fails after editing

Compare the requested name character by character with the output of hostname. Check for an old name, a typo, or a hosts entry that was overwritten by cloud-init. Confirm that the line is formatted as an address followed by whitespace and the hostname, and that the localhost lines were not accidentally deleted.

You cannot obtain a usable sudo session

Use a root shell that is already available, the VM provider’s emergency console, or Ubuntu recovery mode to repair the two files. Once the mapping is corrected, return to the normal verification commands; no external DNS change is required for this local warning.

Operational checklist

  • Record the output of hostname, hostnamectl --static, and cat /etc/hostname.
  • Keep 127.0.0.1 localhost and ::1 localhost in /etc/hosts.
  • Map the active hostname, commonly with 127.0.1.1 your-hostname.
  • If the intended name changes, use hostnamectl set-hostname and update the mapping.
  • Verify with getent hosts, sudo -n true, and hostnamectl status.
  • On cloud VMs, configure cloud-init’s hosts and hostname behavior so boot does not overwrite the repair.
  • On Ubuntu 24.04 LTS and later GCE images, explicitly consider create_hostname_file: true when a persistent /etc/hostname is required.

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 *

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

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.