If you’ve used WSL for any amount of time, you might have noticed that anytime that you attempt to run a command with
sudo, there’s a slight delay followed by an error that your hostname can’t be resolved and then the usual prompt for your password:
$ sudo apt-get update sudo: unable to resolve host MacBookWin [sudo] password for jim:
While it’s not the end of the world that you see this little warning, that small delay can get to be a little annoying each time you want to run a command with elevated privledges.
The issue is that when WSL is installed and setup, your hostname is not included in your hosts file, which you can see by viewing your hosts file:
$ cat /etc/hosts 127.0.0.1 localhost # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts
Luckily the solution is very simple, you just need to edit your hosts file to include your hostname by doing the following:
Check Your Hostname
While not 100% necessary if already know your hostname in WSL (it should be the same as your Computer Name in Windows), you can see your hostname by typing
$ hostname MacBookWin
Edit Your Hosts File
Open your hosts file in nano:
sudo nano /etc/hosts
Scroll down to
127.0.0.1 localhost and add your hostname after
localhost. Your hosts file should now look like this (with your hostname from above instead of MacBookWin):
127.0.0.1 localhost MacBookWin # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts
Now you just need to hit Ctrl+O to save the file and Ctrl+X to exit nano.
Now as a good measure, close out of Bash, start it up and try to use
$ sudo apt-get update [sudo] password for jim:
If you don’t see that line complaining that your hostname can’t be resolved, you’re good to go!