HomeLinuxHow to Fix “sudo: unable to resolve host”

How to Fix “sudo: unable to resolve host”

If you’re seeing the “sudo: unable to resolve host” error on your system, don’t worry. It’s a common issue that can be fixed with simple steps. This error typically indicates a problem with your computer’s hostname. Here’s a guide that discusses how this issue arises and how to fix it easily.

Reason for the Error

Why Does sudo Need to Resolve Hostnames?

When you run a command with sudo, it checks its configuration files, /etc/sudo.conf and /etc/sudoers, which can reference hostnames. Sudo needs to resolve these hostnames to IP addresses to determine permissions and apply rules correctly. If the hostname in these files is different from the one set on the system, this will result in an error, warning, or permission denied which may vary depending on the specific configuration.

Example Scenario

Let’s say you have the following line in your /etc/sudoers file:

%admin ALL=(ALL) ALL
%webadmins webserver=(ALL) ALL

Here, %admin refers to a user group that has a full sudo privileges on all hosts (ALL). The %webadmins group has sudo privileges only on a host named webserver.

If the hostname webserver does not exist or cannot be resolved, members of the webadmins group will encounter an error when they try to use sudo.

How Does sudo Resolve Hostnames?

When you run a command with sudo, it uses internal functions like sudo_gethostname() and resolve_host() to get and resolve the hostname. These functions ensure that sudo can identify and validate the hostname from your system’s settings or its configuration files. If sudo encounters an issue during this process, such as an incorrect or unresolvable hostname, it will result in an error.

Resolving the Error

What is Your Hostname?

First, find out what your current hostname is. For that execute the command:

hostname

This command will show you your current hostname.

How to Correct the Hostname File?

Open the hostname file with your preferred text editor and just type your hostname in it and save. (Ensure it only contains your hostname)

sudo nano /etc/hostname

How to Update the Hosts File?

Open the hosts file: /etc/hosts using your preferred text editor and add your hostname just after your public IP or Localhost IP: 127.0.0.1, similar to the entries in the screenshot below, then save the file.

sudo nano /etc/hosts

Here in the screenshot, I have used my hostname “webserver”. Replace with the one you intended to use on yours.

Should You Reboot Your Computer After Updating Hostname?

Rebooting ensures that all changes are fully applied and recognized by all services and applications. However, if a reboot is not convenient, manually restarting the relevant services can achieve similar results. The key is to ensure that the system properly recognizes and applies the changes you’ve made.

sudo reboot

How to Verify the Changes?

After rebooting, you need to verify that the changes were applied correctly. This ensures that your system recognizes the correct hostname and that it is properly configured.

First, ensure the output of the command “hostname” matches your intended hostname:

hostname

Verify the same for the hostname file (/etc/hostname) and (/etc/hosts):

cat /etc/hostname

Additional Information

Misconfigured DNS settings can also prevent sudo from resolving these hostnames in some cases, leading to these errors sometimes. Check for current nameservers used in the resolv.conf. If needed an edit, replace the current nameservers with Google (8.8.8.8, 8.8.4.4) or Cloudflare (1.0.0.1,1.1.1.1) nameservers:

nano /etc/resolv.conf

To set the resolv.conf file persistent, we have a separate tutorial with us. Please check it out >> https://www.veeble.org/kb/how-to-change-dns-server-permanently-on-ubuntu-20-04/

By following these steps and understanding the backend mechanisms, you can resolve the “sudo: unable to resolve host” error and ensure your system’s hostname is configured correctly.

Scroll to Top