Last active
January 27, 2025 20:47
-
-
Save toryano0820/6ee3bff2474cdf13e70d972da710996a to your computer and use it in GitHub Desktop.
For WSL2: Fixes "Connection Refused" issue when accessing Windows localhost services. Run with "sudo sh wsl2-ubuntu-map-win-localhost.sh" !!! Remember to backup "/etc/hosts" just in case !!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nameserver=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}') # find nameserver | |
[ -n "$nameserver" ] || "unable to find nameserver" || exit 1 # exit immediately if nameserver was not found | |
echo "##### nameserver found: '$nameserver'" | |
localhost_entry=$(grep -v "127.0.0.1" /etc/hosts | grep "\slocalhost$") # find localhost entry excluding 127.0.0.1 | |
if [ -n "$localhost_entry" ]; then # if localhost entry was found | |
echo "##### localhost entry found: '$localhost_entry'" | |
sed -i "s/$localhost_entry/$nameserver localhost/g" /etc/hosts # then update localhost entry with the new $nameserver | |
else # else if entry was not found | |
echo "##### localhost entry not found" | |
echo "$nameserver localhost" >> /etc/hosts # then append $nameserver mapping to localhost | |
fi | |
cat /etc/hosts # display to confirm |
你好,你的邮件我已经收到,如有急事可以打电话给我:15099953695
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you, saved an hour researching on how to map wsl2 to windows localhost