Created
February 28, 2026 15:17
-
-
Save karthiks/eb823570f8bce4a16957a70237df6d78 to your computer and use it in GitHub Desktop.
Accessing Windows Host from WSL2 Linux using Hostname
This file contains hidden or 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
| ### Check out detailed blog post at https://blog.codonomics.com/2026/01/accessing-windows-host-from-wsl2-linux.html | |
| ### Start: Adding Windows Host IP to WSL2 Hosts File | |
| # Add to the end of ~/.bashrc or ~/.zshrc | |
| echo "Updating Windows host IP in /etc/hosts..." | |
| # Get Windows IP from default route | |
| WIN_HOST_IP=$(ip route show | grep -i default | awk '{print $3}') | |
| # Check if we got a valid IP | |
| if [ -n "$WIN_HOST_IP" ]; then | |
| # Remove any existing entry for winhost | |
| sudo sed -i '/winhost/d' /etc/hosts | |
| # Add new entry | |
| echo "$WIN_HOST_IP winhost" | sudo tee -a /etc/hosts > /dev/null | |
| echo "✓ winhost → $WIN_HOST_IP" | |
| else | |
| echo "✗ Failed to get Windows host IP" | |
| fi | |
| ### End: Adding Windows Host IP to WSL2 Hosts File |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment