The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →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.
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
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.
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.
#1 Best Overall
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.
Read the hosts-file output correctly
- Keep a line mapping
127.0.0.1tolocalhost. - 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.1line. - 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.
- Choose the intended hostname. Use the value you want shown by
hostnameand in system logs. Avoid spaces; use a conventional hostname made of letters, numbers, and hyphens. - Back up the hosts file. From an administrative shell, run
cp -a /etc/hosts /etc/hosts.backup. If the command is run throughsudoand the warning appears, that warning is expected until the mismatch is corrected. - 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.
Rank #2
- Make the local mapping agree. Edit
/etc/hostswith 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.
- 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. - 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.
Recommended Free Tools
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-hostnameshould return a line containing the local address, commonly127.0.1.1. It verifies the system’s normal name-service path rather than merely displaying a file.sudo -n trueperforms 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 statusdisplays 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.
Rank #3
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:
| 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.
Rank #4
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.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →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.
Best Value
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.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Leaving 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.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsQuick Recap
A reboot-proof checklist
- Record the output of
hostnameandhostnamectl --static. - Confirm the intended value in
/etc/hostname, when that file is used by the image. - Keep valid
localhostentries for IPv4 and IPv6. - Map the active hostname locally, commonly with
127.0.1.1 your-hostname. - Run
getent hosts your-hostnamebefore testingsudo. - 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: falsebehavior 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.




