If you run the sudo command in Bash on Ubuntu in Windows 10, you might face the issue that it shows an error message unable to resolve host followed by your computer name. Here is a quick fix for this issue.
Under Windows 10, Bash on Ubuntu can't resolve the host name defined in the standard configuration file /etc/hostname. To fix it, you need to specify the IP address manually which matches the host name.
This can be done with the following commands. Execute them one by one.
sudo su echo 127.0.0.1 your_pc_name >> /etc/hosts
The first command switches the user context to the root user.
The second adds a new line to the file /etc/hosts which tells that your_pc_name should be resolved to the local computer. Replace the your_pc_name portion with the actual PC name.
After doing this, all network related Ubuntu tools should not produce this error.See the following screenshot:
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
Your listed command
echo 127.0.0.1 your_pc_name > /etc/hosts
doesn’t “*add* a new line to the file /etc/hosts “, it overwrites it so you will loose existing entries like ‘127.0.0.1 localhost’.
You need to use ‘>>’ to append standard output (produced by echo) to files
Following command will work and automatically resolves hostname through command substitution (backquotes).
echo 127.0.0.1 `hostname` >> /etc/hosts
Sorry, sure it should be >>
I made a typo.
Thank you very much, fixed.
I *just* finished installing the “ubuntu on windows” components. I tried installing some packages with apt-get, but they fail to resolve the host names.
I just came across your post here and tried what you suggested. However, even after the initial “sudo su” before adding the line to /etc/hosts file, “sudo su” fails to run with the same error (“sudo: unable to resolve host {PC NAME}”), as does any successive calls.
Any ideas?
Specifically, trying things like…
sudo apt update
Fails with a number of errors, including…
Err http://archive.ubuntu.com trusty Release.gpg
Could not resolve ‘archive.ubuntu.com’
Please delete this threads (should you wish.) Rebooting seems to have resolved my issue (“sudo apt update” now executes without issue.)
Happy to hear!
But why delete it. It might be helpful for others.