Skip to content

Instantly share code, notes, and snippets.

@antriver
Last active April 28, 2021 12:03
Show Gist options
  • Save antriver/1321a8e32c50ed2452db1762c4d7ebd3 to your computer and use it in GitHub Desktop.
Save antriver/1321a8e32c50ed2452db1762c4d7ebd3 to your computer and use it in GitHub Desktop.
Add a "wsl.local" entry to both the WSL and Windows hosts file, pointing to the IP address of WSL
#!/bin/bash
# You must run this script from an "elevated terminal", that means right click on Windows Terminal and "Run as Administrator".
# Then run it with `sudo wsl-ip.sh` from within WSL.
# Remove existing wsl.local lines
sed -i '/wsl.local/d' /etc/hosts
sed -i '/wsl.local/d' /mnt/c/Windows/system32/drivers/etc/hosts
# Add to WSL hosts file
echo "`ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'` wsl.local" >> /etc/hosts
# Add to Windows hosts file
echo "`ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'` wsl.local" >> /mnt/c/Windows/system32/drivers/etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment