About this article: This article is reviewed against the cited sources. This is source research, not lab validation. Check the stated versions and your own requirements before making changes.
The line "sudo: unable to resolve host web02" means sudo asked the system resolver for the machine's own name and got no usable answer. It appears after a hostname change or a VM clone when /etc/hosts still lists the old name. The fix is to make the current hostname resolve, by an entry in /etc/hosts or a DNS record, and to check that nothing such as cloud-init puts the old name back.
What the message means
The message comes from sudo's sudoers plugin. In the sudo source at tag SUDO_1_9_15p5, the function cb_fqdn calls resolve_host, which calls getaddrinfo on the host name, and, if that fails, logs the text unable to resolve host %s with the host name. The program name, a colon, the message and then the resolver's own reason are printed, so the line has the shape sudo: unable to resolve host web02: REASON. The reason text comes from gai_strerror, which the getaddrinfo(3) manual describes for each error code, for example EAI_NONAME for a name that is not known and EAI_AGAIN for a temporary failure from the name server. The same message text exists in tag SUDO_1_8_31 (in sudoers.c), where one of its two uses prints no resolver reason.
The lookup only runs when the sudoers fqdn flag is on. The sudo.ws sudoers manual says "This flag is off by default". The Debian bookworm sudoers(5) page (sudo 1.9.13p3, dated January 16, 2023) and the Ubuntu noble page (sudo 1.9.15p5-3ubuntu5.24.04.4) both say "This flag is on by default". The upstream configure.ac has a --with-fqdn build option, so packages can differ. If you see the message, the lookup is running on your build.
The sudoers manual also warns that turning fqdn on "requires sudoers to make DNS lookups which renders sudo unusable if DNS stops working". Failures of the resolver itself are covered in '"Temporary failure in name resolution" on a Linux server: test each layer from systemd-resolved down'. The fix below is for a local name that is simply not listed anywhere.
How the machine finds its own name
- The hostname. The
hostnamecommand prints the name the kernel reports throughgethostname(2)(hostname(1)). With systemd, a static name in/etc/hostnamehas priority over a transient name received from DHCP, which has priority over the fallbacklocalhost(hostname(5), systemd 262).hostnamectlshows the three kinds: static, transient and pretty. - The lookup order.
/etc/nsswitch.confhas ahostsline listing the sources in order; per nsswitch.conf(5), "The order of the services on the line determines the order in which those services will be queried, in turn, until a result is found".filesmeans/etc/hostsanddnsmeans the DNS servers. - The hosts file. hosts(5) says each line is an IP address, the canonical hostname and optional aliases, that modifications "normally take effect immediately, except in cases where the file is cached by applications", and shows
127.0.1.1 thishost.example.org thishost, noting "127.0.1.1 is often used for the FQDN of the machine".
Likely causes
| Cause | What you see | How to confirm |
|---|---|---|
Hostname changed, /etc/hosts still has the old name |
Message begins after a rename or clone | getent ahosts for the current name returns nothing |
| Hostname set by DHCP or cloud metadata, not matching the hosts file | Name differs from what you set | hostnamectl shows the transient name |
files missing or after dns in nsswitch.conf |
Local entries ignored | The hosts: line in /etc/nsswitch.conf |
cloud-init rewrites /etc/hosts at each boot |
Your fix disappears after a reboot | cloud-init manage_etc_hosts is true or localhost |
| Name not in DNS, and DNS is first | Only on hosts that skip /etc/hosts |
No DNS record for the name |
Prove the cause before changing anything
These steps only read.
1. See the current name.
hostnamectl
hostname
hostnamectl with no command shows the hostname and related information (hostnamectl(1)). Write down the name hostname prints.
2. Test it the way sudo does. The getent(1) page says the ahosts database passes each key to getaddrinfo(3), which is also what sudo uses:
getent ahosts web02
Use your own name. A good result lists an address for the name. A bad result prints nothing, which matches the sudo message.
3. Read the hosts file and lookup order. Open /etc/hosts in a viewer and look for a line with your current name. Open /etc/nsswitch.conf, find the line that begins hosts:, and check that files is on it.
4. Check for cloud-init. If the machine was built from a cloud image, look in its cloud-init configuration and user data for the settings manage_etc_hosts and preserve_hostname. The cloud-init documentation does not name a single file for them. The cloud-init 26.2 documentation says manage_etc_hosts defaults to false, and that when it is true "the contents of the hosts file will be updated every boot" from /etc/cloud/templates/hosts.tmpl.
5. Optionally, as root (no sudo is needed then), sudo -V displays the configure options sudo was built with (sudo(8)), which may show --with-fqdn.
Fix
Add the current name to /etc/hosts
Save first: copy the file under a dated name.
cp /etc/hosts /etc/hosts.20261120
Risk: an error in /etc/hosts can break name lookups for other services on the machine. The file has no syntax checker, so run getent ahosts web02 straight afterwards, and keep a second root session open until it passes. Back out: cp /etc/hosts.20261120 /etc/hosts, then repeat the getent test.
Edit /etc/hosts so that one line maps the machine's address to the current name, in the form from hosts(5): the address, the full name, then the short name. For example 127.0.1.1 web02.example.com web02. Remove or correct the line that holds the old name if the machine is no longer called that. Host names may contain only alphanumeric characters, minus signs and periods, per hosts(5).
Correct the hostname itself
Disruptive step: changing the hostname with hostnamectl hostname NAME means other software that reads the hostname, such as logging, monitoring, mail and licensing, sees the new name on its next start. Save first: record the output of hostnamectl before the change, including the static, transient and pretty names, because hostnamectl(1) says hostname NAME "will alter the pretty, the static, and the transient hostname alike" unless one or more of --static, --transient and --pretty are given. Back out: run hostnamectl hostname with the recorded name and --static, then again with --transient and the recorded transient name, and with --pretty and the recorded pretty name, so each of the three is restored.
If the name was changed by mistake, set the intended static name. hostnamectl(1) documents hostnamectl hostname NAME (added in systemd version 249); older systemd releases are not covered by that page.
Stop cloud-init changing it back
Risk: editing the cloud-init hosts template changes what every boot writes, because cloud-init settings apply to every boot. Save first: copy the file you will edit to a dated name, for example cp /etc/cloud/templates/hosts.tmpl /etc/cloud/templates/hosts.tmpl.20261120. The cloud-init documentation does not give a command that checks the template, so read the edited file before the next boot. Back out: copy the saved file back over the original under its own name, for example cp /etc/cloud/templates/hosts.tmpl.20261120 /etc/cloud/templates/hosts.tmpl, then run the getent ahosts web02 test (with your name) after the next boot.
If manage_etc_hosts is true, edit /etc/cloud/templates/hosts.tmpl, not /etc/hosts, because the cloud-init page says changes to the hosts file are lost at each boot unless made there. With manage_etc_hosts: localhost, cloud-init adds a 127.0.1.1 entry from the FQDN and hostname every boot. The cloud-init "Update hostname" example page also says that when preserve_hostname is not specified, cloud-init updates /etc/hostname per boot based on the cloud-provided local-hostname setting, so a clone whose cloud metadata still carries the old name can get that name back. It adds: "If you manually change /etc/hostname after boot, cloud-init will no longer modify it." With manage_etc_hosts: localhost there is no template to edit; the page says cloud-init does not rewrite the hosts file entirely, so check the file after a reboot.
DNS and lookup order
Risk: a DNS change affects every client that resolves the name, caches keep the old answer until they expire, and a wrong address breaks every service that uses the name. Save first: print the existing records for the name with your DNS server's own tool or console, for example nslookup web02.example.com, and keep the output. Back out: remove the A record you added, wait for cached answers to expire, then run getent ahosts web02 again (with your name).
If the machine should resolve through DNS, add an A record for its name.
If files is missing from the hosts: line, treat that as a configuration fault in the image and ask whoever owns the base build before editing /etc/nsswitch.conf.
Example (illustrative): web02 is a clone of web01, renamed with hostnamectl hostname web02. hostname prints web02, but getent ahosts web02 prints nothing. /etc/hosts has the line 127.0.1.1 web01.example.com web01. After cp /etc/hosts /etc/hosts.20261120, the administrator changes that line to 127.0.1.1 web02.example.com web02. getent ahosts web02 now lists an address, and sudo id runs without the warning. The image uses cloud-init with manage_etc_hosts false, so the file stays that way after a reboot.
Confirm the fix
getent ahosts web02(with your name) returns an address.sudo idruns with no "unable to resolve host" line and without a pause.- Disruptive step: a reboot interrupts every service on the machine. Save first: the dated copies from the Fix section are already in place, and
hostnamectloutput is recorded. Then reboot at a convenient time and repeat steps 1 and 2, to be sure nothing rewrote the file. Back out: a reboot cannot be undone; if the file was rewritten, restore/etc/hosts.20261120withcpand repeat thegetenttest. - Other services that start with the new name, such as mail, log in with the expected name.
What to watch afterwards
- Add a hosts check to the VM clone procedure, with the rename and the hosts line done together.
- If you use cloud-init, put the hosts line from the Fix section (the address, the full name and the short name) in the template.
- Do not suppress the symptom by switching off
fqdnin sudoers; the flag is about how host names in the sudoers file are matched, so fix resolution instead.
Read next
Related articles you may want next.
- Troubleshooting"Temporary failure in name resolution" on a Linux server: test each layer from systemd-resolved downLinux says Temporary failure in name resolution? Test getent, resolvectl and the upstream with dig, then fix resolved, netplan or NetworkManager DNS.Read article
- TroubleshootingStatic IP Lost After a VM Restore or Virtual NIC ChangeA restored or re-NICed VM on DHCP or no network may have a new network device. Prove it with read-only listings, then move settings to the right device.Read article
- TroubleshootingSSH "Permission denied (publickey)": what the server log says and what to fixSSH says Permission denied (publickey)? Read the sshd log line, then check authorized_keys modes, StrictModes, AllowUsers and accepted key algorithms.Read article
Risks and limits
- These commands have not been run or lab-tested.
- The reason text after the host name depends on your C library and locale, so it varies from machine to machine.
- The
fqdndefault differs between the upstream, Debian and Ubuntu pages. Other distributions may differ again. hostnamectl hostnameneeds systemd 249 or later.- Containers, NIS and other lookup sources are not covered.
Sources
- sudoers_cb.c at tag SUDO_1_9_15p5 — sudo project source, tag SUDO_1_9_15p5, no date shown, accessed 20 November 2026
- sudoers.c at tag SUDO_1_8_31 — sudo project source, tag SUDO_1_8_31, no date shown, accessed 20 November 2026
- logging.c at tag SUDO_1_9_15p5 — sudo project source, tag SUDO_1_9_15p5, no date shown, accessed 20 November 2026
- fatal.c at tag SUDO_1_9_15p5 — sudo project source, tag SUDO_1_9_15p5, no date shown, accessed 20 November 2026
- configure.ac at tag SUDO_1_9_15p5 — sudo project source, tag SUDO_1_9_15p5, no date shown, accessed 20 November 2026
- Sudoers Manual — sudo.ws, no date shown, accessed 20 November 2026
- sudoers(5) — Debian manpages, Debian bookworm, sudo 1.9.13p3, January 16, 2023, accessed 20 November 2026
- sudoers(5) — Ubuntu manpages, noble, sudo 1.9.15p5-3ubuntu5.24.04.4, no date shown, accessed 20 November 2026
- sudo(8) manual — sudo.ws, no date shown, accessed 20 November 2026
- hosts(5) — Linux man-pages 6.19, 2026-02-11, accessed 20 November 2026
- nsswitch.conf(5) — Linux man-pages 6.19, 2026-02-08, accessed 20 November 2026
- getent(1) — Linux man-pages 6.19, 2026-02-08, accessed 20 November 2026
- getaddrinfo(3) and gai_strerror(3) — Linux man-pages 6.19, 2026-02-08, accessed 20 November 2026
- hostname(1) — net-tools, 2025-09-10, accessed 20 November 2026
- hostname(5) — systemd 262~devel manual page, colophon dated 2026-08-04, accessed 20 November 2026
- hostnamectl(1) — systemd 262~devel manual page, colophon dated 2026-08-04, accessed 20 November 2026
- cloud-init example: Update hostname — cloud-init 26.2 documentation, no date shown, accessed 20 November 2026
- cloud-init module reference: Update Etc Hosts and Set Hostname — cloud-init 26.2 documentation, no date shown, accessed 20 November 2026
This article was originally published on .