Free tools Windows power users keep installed
One-click scans. No signup required.
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.
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
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.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →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.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsInspect all three names before changing anything
Start with read-only checks so you know which value is wrong. Run these commands in a terminal:
hostnamehostnamectl --staticcat /etc/hostnamegrep -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:
Rank #2
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.
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:
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 →Rank #3
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.
Recommended Free Tools
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.
Rank #4
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.
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:
Best Value
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.
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.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWhen 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.
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.
Quick Recap
Operational checklist
- Record the output of
hostname,hostnamectl --static, andcat /etc/hostname. - Keep
127.0.0.1 localhostand::1 localhostin/etc/hosts. - Map the active hostname, commonly with
127.0.1.1 your-hostname. - If the intended name changes, use
hostnamectl set-hostnameand update the mapping. - Verify with
getent hosts,sudo -n true, andhostnamectl 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: truewhen a persistent/etc/hostnameis 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.




