Skip to content

Instantly share code, notes, and snippets.

@MichaelBelgium
Created June 5, 2020 07:46
Show Gist options
  • Save MichaelBelgium/2243e5713833ba44b5675d844eeb1c85 to your computer and use it in GitHub Desktop.
Save MichaelBelgium/2243e5713833ba44b5675d844eeb1c85 to your computer and use it in GitHub Desktop.
WSL v2 windows hosts sync (powershell - updates domains to wsl2 ip)
# Requires -RunAsAdministrator
# Only works for WSL v2, this is completely not needed for WSL v1 where u always can use 127.0.0.1 in hosts file
Clear-Host
if ((Get-InstalledModule "Carbon" -ErrorAction SilentlyContinue) -eq $null) {
Install-Module -Name 'Carbon' -AllowClobber
}
Import-Module 'Carbon'
$wslIp = (wsl hostname -I) -replace ' ',''
Write-Host "Setting wsl v2 hosts entries to $wslIp"
$domains = @(
'wsl2.local'
# add more domains if necessary
)
foreach($domain in $domains) {
Set-HostsEntry -IPAddress $wslIp -HostName $domain
}
Write-Host "Done!"
@derik007
Copy link

derik007 commented Oct 6, 2020

# Requires -RunAsAdministrator
# Only works for WSL v2,  this is completely not needed for WSL v1 where u always can use 127.0.0.1 in hosts file
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
exit $LASTEXITCODE
}


Clear-Host

if ((Get-InstalledModule "Carbon" -ErrorAction SilentlyContinue) -eq $null) {
    Install-Module -Name 'Carbon' -AllowClobber
}

Import-Module 'Carbon'

$wslHostIp = (wsl cat /etc/resolv.conf | Select-String -Pattern "\d{1,3}(\.\d{1,3}){3}" -AllMatches).Matches.Value
$wslSelfIp = (wsl hostname -I) -replace ' ',''

Write-Host "Setting wsl v2 host ip to $wslHostIp"
Write-Host "Setting wsl v2 self ip to $wslSelfIp"

$wsl_host_domains = @(
    'wsl2.host'
)
$wsl_self_domains = @(
    'wsl2.self'
)

foreach($domain in $wsl_host_domains) {
    Set-HostsEntry -IPAddress $wslHostIp -HostName $domain
}

foreach($domain in $wsl_self_domains) {
    Set-HostsEntry -IPAddress $wslSelfIp -HostName $domain
}

Write-Host "Done!"

This is the script that I changed a little bit.

@derik007
Copy link

derik007 commented Oct 6, 2020

#!/bin/sh
export WINIP=$(cat /etc/resolv.conf | grep 'nameserver' | cut -f 2 -d ' ') 
export WSLIP=$(ip addr show eth0 | grep 'inet ' | cut -f 6 -d ' ' | cut -f 1 -d '/')
echo WINIP $WINIP
echo WSLIP $WSLIP

echo <Your Password> | sudo -S sed -i "/winip/c $WINIP\twinip" /etc/hosts
echo <Your Password> | sudo -S sed -i "/wslip/c $WSLIP\twslip" /etc/hosts

vim "+:%s/^.*winip/$WINIP\t\twinip/g" "+:%s/^.*wslip/$WSLIP\t\twslip/g" '+:wq' -E /mnt/c/Windows/System32/drivers/etc/hosts 

This is another solution. Run this script in ~/.bashrc can set hosts file both in win and linux.

@kallookoo
Copy link

Hi Guys!
The @derik007 bash version not working. :(
This is my version using Powershell; https://github.com/kallookoo/lamp/blob/feccb3e3c507f2d97afcc0962cb7e449ec0eeffb/src/win/hosts.ps1

@msitarzewski
Copy link

Amazing. Thank you. It's perfect for my use case.

@brunorafaeI
Copy link

brunorafaeI commented Aug 3, 2021

just for info, the @derik007 solution needs to use PowerShell as admin and execute "wsl", I've changed the last line :

vim "+:%s/^.*winip/$WINIP\t\twinip/g" "+:%s/^.*wslip/$WSLIP\t\twslip/g" '+:wq' -E /mnt/c/Windows/System32/drivers/etc/hosts

to

vi "+:%s/^.*wsl2.local/^$WSLIP wsl2.local/g" "+:wq" -E /mnt/c/Windows/System32/drivers/etc/hosts

@kakkun61
Copy link

kakkun61 commented Aug 9, 2022

I forked: https://gist.github.com/kakkun61/2139c240ac0dce1bb1606c1e8bc8038c

Added features:

  • admin check
  • run as admin
  • install Carbon module if necessary
  • remove old hosts entry
  • multiple ip addr support

@petski
Copy link

petski commented Jul 11, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment