-
-
Save MichaelBelgium/2243e5713833ba44b5675d844eeb1c85 to your computer and use it in GitHub Desktop.
# 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!" |
# 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.
#!/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.
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
Amazing. Thank you. It's perfect for my use case.
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
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
FWIW, my take can be found at https://gist.github.com/petski/1414048ca2db37592da2e7af13d718f5
That may be partly correct, in the sense that hostname -I appears to only return the IP associated with eth0 when only a single distro is running (although I couldn't swear to it be consistent all the time). As soon as another WSL distro is started (in my case one that has been configured for systemd) hostname -I startes returning the ip for each bound device, for example below we can see eth0, docker0 and br-f69dc3652dfd ... and it does so in any of the wsl sessions until they are all shutdown (terminating doesn't appear to have any impact). The alternate method will always return the IP for eth0 so I think thats a better bet ?