VPS Tutorials
How to Fix “sudo: unable to resolve host” on Ubuntu or Debian
Check the VPS hostname with getent, correct its entry in /etc/hosts, and find out what cloud-init is rewriting if the warning comes back after a reboot.

If sudo starts printing unable to resolve host web-01 after a VPS rename, look up web-01 on the server itself. A common Ubuntu or Debian setup has the new name in hostname and the old name next to 127.0.1.1 in /etc/hosts. Fix that mapping, then run getent and sudo true again to see whether the warning is gone.
Run these on the VPS that prints the warning. They work from a regular shell account.
hostname
cat /etc/hostname
getent hosts "$(hostname)"
grep '^hosts:' /etc/nsswitch.conf
cat /etc/hosts
hostname prints the name the system is using now. /etc/hostname normally holds the static name used at boot. getent hosts uses the host's name service configuration, which can include /etc/hosts. dig sends a query to a DNS server. Ubuntu's getent manual
| What you find | What to check next |
|---|---|
Both names are web-01, getent prints nothing, and /etc/hosts lists web-old | Correct the entry for this machine in /etc/hosts |
| The active name, static name and intended name differ | Find out whether the system administrator, deployment tool or cloud platform owns the hostname. Then align its mapping |
getent returns the expected address and sudo still warns | Look up the exact name from the warning and confirm which VPS or container is running the command |
The sudoers policy looks up the local hostname when its fqdn option is in use. sudo can print the warning and still run the command. Check the exit status before deciding whether you need a console to edit the file. Hostname lookup in sudoers
Back up the file and open it with an authorized account. Keep the current SSH session open while you verify the change.
sudo cp -a -- /etc/hosts "/etc/hosts.bak.$(date +%Y%m%d-%H%M%S)" &&
sudoedit /etc/hosts
Say the active and static names are both web-01 and the server already uses 127.0.1.1 for its local mapping. Change the name on the 127.0.1.1 web-old line to web-01. Check these two lines and keep the IPv6 entries and mappings for other hosts.
127.0.0.1 localhost
127.0.1.1 web-01
If the fully qualified domain name (FQDN) web-01.example.net and the short name both need to resolve on the server, use 127.0.1.1 web-01.example.net web-01. Put the full name first and the short name after it as an alias. If your setup already maps this host to a permanent IP by design, keep that IP and correct the names on its entry. 127.0.1.1 is a loopback address for local lookups. Debian's hostname resolution reference
If hostname prints (none) or an unexpected default, decide which name the instance should use. On a systemd VPS where you manage the name yourself, add the intended mapping first and run sudo hostnamectl set-hostname web-01 with your chosen name. If your current account cannot edit the file through sudo, use your cloud console and existing administrator access. systemd's hostnamectl manual
getent hosts "$(hostname)"
sudo true
printf 'sudo exit=%s\n' "$?"
With the loopback example, expect getent to show 127.0.1.1 and web-01. sudo true should finish quietly. The final line prints its exit status, which should be 0. You can usually verify an /etc/hosts edit as soon as you save it. If the lookup still fails, inspect the hosts: line in /etc/nsswitch.conf for the files source and check for systemd-resolved or nss-myhostname. If getent hosts example.org also fails, follow the VPS DNS resolver checklist.
Compare hostname, /etc/hostname and /etc/hosts with the results from before the reboot. That tells you whether the hostname changed or its mapping was overwritten. On an image with cloud-init installed, check the local config files and log.
cloud-init status --long
sudo grep -nE 'manage_etc_hosts|preserve_hostname' /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.d/*.cfg
sudo grep -E 'update_hostname|update_etc_hosts' /var/log/cloud-init.log | tail -n 20
| Setting or symptom | Where to make the fix |
|---|---|
manage_etc_hosts: true | Cloud-init generates /etc/hosts from a template at each boot. Check the generated file header for the template and configuration source. A common Debian template is /etc/cloud/templates/hosts.debian.tmpl |
manage_etc_hosts: localhost | Cloud-init maintains the entry for the local name. Check cloud hostname metadata and any hostname or fqdn value in cloud-config |
| The system hostname reverts | Correct the hostname at the source that manages it. Consider preserve_hostname: true once you confirm the administrator should manage it locally |
preserve_hostname controls the system hostname. manage_etc_hosts controls the hosts file. After correcting the effective cloud configuration, run the same getent and sudo true checks following your next scheduled reboot. Cloud-init host and hostname modules · Debian hosts template
- SSH works and sudo prints the warning. Start with
getent hosts "$(hostname)", then check whethersudo truecompletes quietly. - SSH itself returns
Permission denied. Check the login account, key and SSH server logs. SSH permission troubleshooting - Sudo works again and visitors still see
NXDOMAIN. Check the public DNS records and delegation for the site. Website NXDOMAIN troubleshooting
The procedure was checked against the official sudo, Debian, Ubuntu and cloud-init documentation. web-01 and its command output are illustrative.
Next step: compare the actual plans
Filter the listed plans by budget, specifications and region, or start with a buying guide.