iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
The sudo: unable to resolve host warning usually means Ubuntu’s current hostname does not match an entry in the local hosts database. It is normally a local naming mismatch, not evidence that internet DNS is down. Compare the running hostname, /etc/hostname, and the hostname entries in /etc/hosts; then make the intended name consistent in all three places.
On a conventional Ubuntu installation, the durable repair is to keep the normal localhost lines and add the machine’s active name, commonly on 127.0.1.1. Cloud-init-managed virtual machines need an additional check because cloud-init can regenerate either file during boot.
Outdated 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 matchWindows 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 reinstallWhat the warning means
When you run a command through sudo, sudo can perform hostname resolution in some situations. If Ubuntu cannot resolve the machine’s own hostname through its local name mappings, it prints a warning such as:
sudo: unable to resolve host your-hostname: Name or service not known
The command may still run, but the warning identifies a configuration problem that can affect software expecting the local hostname to resolve reliably. Ubuntu’s 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..”
#1 Best Overall
Three values must be considered together:
| Location or command | What it represents | What to look for |
|---|---|---|
hostname |
The hostname currently active in the running system | The name Ubuntu is using right now |
hostnamectl --static |
The static hostname reported through systemd | Usually the intended persistent name |
/etc/hostname |
The configured hostname traditionally read at boot | A single name matching the intended hostname |
/etc/hosts |
Local address-to-name mappings | An entry that resolves the active hostname, commonly on 127.0.1.1 |
A mismatch in any of these locations can produce the warning. Changing the hostname with hostnamectl alone does not automatically guarantee that /etc/hosts contains a matching mapping.
Inspect the configuration before changing it
Run these read-only commands from a terminal:
hostname
hostnamectl --static
cat /etc/hostname
grep -nE '(^127.0.1.1|^127.0.0.1|::1)' /etc/hosts
Record the output before editing anything. The first two commands show the runtime and systemd-reported names. The cat command shows the boot-time hostname file. The final command displays the localhost and loopback-related lines most likely to matter for this warning.
A healthy traditional configuration commonly resembles:
127.0.0.1 localhost
127.0.1.1 your-hostname
::1 localhost ip6-localhost ip6-loopback
Your hostname can be different from your-hostname; use the exact intended name, including capitalization choices made by your system. Do not remove a valid 127.0.0.1 localhost or ::1 localhost line simply because you are adding a machine-specific entry.
Rank #2
Repair a normal Ubuntu installation
1. Decide which hostname should be authoritative
Choose the name you actually want the machine to use. If the outputs from hostname, hostnamectl --static, and /etc/hostname already agree, keep that name and repair only the hosts mapping. If they disagree, select one spelling and use it consistently.
For a simple local server or desktop, a short hostname such as app01 is generally represented as a single label. If your environment intentionally uses a fully qualified name, preserve that design and ensure the hosts entry matches the name Ubuntu reports as its active hostname.
2. Set the system hostname when it is wrong
Use the systemd interface to change the static hostname:
sudo hostnamectl set-hostname your-hostname
The Ubuntu hostnamectl manual describes the command as a way to “query and change the system hostname and related settings.” The command updates the system’s configured hostname, but you still need a matching local mapping in /etc/hosts.
Rank #3
If sudo displays the warning while you are fixing it, that does not prevent the repair in many cases. If sudo cannot be used successfully, open an already-authorized root shell, use Ubuntu recovery mode, or use another administrative path available on the machine. Do not repeatedly change names at random; first establish which value is intended.
3. Add the local mapping in /etc/hosts
Edit the file as root with an editor available on your installation. For example:
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →sudoedit /etc/hosts
Keep the existing localhost mappings and add or correct a line for the active hostname. The common Ubuntu pattern is:
127.0.1.1 your-hostname
If an old name is on the 127.0.1.1 line, replace it with the chosen active name. If several local aliases are deliberate, they may appear after the primary name on the same line, but do not delete aliases that another local service requires without understanding their purpose. Avoid replacing the localhost line with the hostname: 127.0.0.1 localhost and ::1 localhost serve different loopback-name roles.
Rank #4
4. Verify the mapping and sudo path
Use the same name you placed in /etc/hosts:
getent hosts your-hostname
sudo -n true
sudo hostnamectl status
getent hosts your-hostnameasks the system name-service stack to resolve the local name. It should return an address and the name rather than an error.sudo -n trueruns a harmless command without prompting for a password. It is a path check, not a password reset; if sudo requires authentication, it can fail with a password-related message even when hostname resolution is fixed.sudo hostnamectl statusconfirms that sudo can invoke a command without printing the hostname warning.
Open a new terminal or rerun the command that originally produced the warning. If the warning has disappeared and the three hostname locations agree, the local mismatch has been corrected.
Why the warning returns after a reboot
A repair that survives the current session can still be overwritten at boot. This is especially common on cloud virtual machines, where cloud-init may manage the hostname or regenerate /etc/hosts.
Free tools Windows power users keep installed
One-click scans. No signup required.
Check whether cloud-init manages /etc/hosts
cloud-init’s hosts module has three relevant behaviors:
| Setting | Behavior | Persistence implication |
|---|---|---|
manage_etc_hosts: false |
The default mode; cloud-init leaves /etc/hosts alone |
Manual edits can remain unless another service changes the file |
manage_etc_hosts: true |
cloud-init renders the hosts file from /etc/cloud/templates/hosts.tmpl on each boot |
Edit the template or cloud-init configuration, not only the generated file |
manage_etc_hosts: localhost |
cloud-init ensures a hostname entry exists on each boot | Boot-time generation can alter the file, so configure the managed source |
When the module manages the file, a direct edit to /etc/hosts may be replaced at the next boot. 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.” Make the hostname mapping in that template or in the cloud-init user-data configuration that controls it, then reboot and inspect the resulting file.
Best Value
Check whether cloud-init changes /etc/hostname
cloud-init’s hostname module can also update /etc/hostname. Its settings include:
preserve_hostname: trueprevents cloud-init from changing the hostname.create_hostname_filecontrols whether cloud-init creates the hostname file.
If cloud-init is expected to own the hostname, configure the desired name in cloud-init rather than relying on a one-time shell command. If the operating system’s configured name should remain authoritative, use the preservation setting appropriate to your image and deployment process.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Ubuntu 24.04 and later GCE images: a special persistence case
Ubuntu’s Google Compute Engine guidance documents a release-specific behavior for Ubuntu 24.04 LTS and later GCE images: /etc/hostname may be absent, and create_hostname_file defaults to false. In that situation, setting a hostname without creating the file can be temporary. DHCP or cloud-init may supply a different name after reboot.
If a persistent configured hostname is required on one of these images, set create_hostname_file: true in the cloud-init configuration, then ensure the hosts mapping is generated consistently. After the next boot, check all three locations again rather than assuming the running name is permanent.
Troubleshooting branches
The names match, but sudo still warns
- Confirm that the hosts entry contains the exact output of
hostname, including punctuation and spelling. - Check that the entry is not commented out with
#and that fields are separated by whitespace. - Run
getent hosts your-hostname. If it does not return an address, the system’s name-service configuration is not reading the mapping as expected. - Inspect the file again after restarting or reconnecting. A provisioning agent may have rewritten it.
The warning appears only on a cloud VM
Inspect cloud-init configuration and logs for hosts or hostname management. Determine whether manage_etc_hosts, preserve_hostname, or create_hostname_file is controlling the result. Change the managed template or user data, not just the generated file, and then verify after a reboot.
Sudo refuses to run while you are repairing the file
The warning itself is not always a fatal error, but authentication or policy errors can be. Use an existing root session, the provider’s console, Ubuntu recovery mode, or another approved administrative account to edit /etc/hostname and /etc/hosts. Once the mapping resolves, retry sudo normally.
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 →You are worried that this proves internet DNS is broken
Test the local name first with getent hosts. The warning concerns resolution of the machine’s own hostname through local configuration. It does not, by itself, establish that external DNS lookups or internet connectivity have failed.
Quick Recap
Preventing future mismatches
- When renaming a machine, update the active hostname and the matching
/etc/hostsentry in the same maintenance window. - After changing an image, cloud provider, or provisioning policy, check whether cloud-init now manages either file.
- On managed instances, keep the desired hostname in the source configuration that recreates files at boot.
- For Ubuntu 24.04 LTS and later GCE images, explicitly decide whether
/etc/hostnamemust be created and setcreate_hostname_file: truewhen persistence requires it. - Use
getent hostsafter changes and after the first reboot to verify the name through the same resolution path that local programs use.
Quick reference
- Run
hostname,hostnamectl --static,cat /etc/hostname, and thegrepcommand for/etc/hosts. - Choose one intended hostname and make the runtime and configured values agree.
- Keep
127.0.0.1 localhostand::1 localhost; add the active name, commonly as127.0.1.1 your-hostname. - Verify with
getent hosts your-hostname,sudo -n true, andsudo hostnamectl status. - If the warning returns after reboot, configure cloud-init’s hosts and hostname behavior, including the GCE-specific
create_hostname_filesetting where applicable.
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.

