Last active
July 11, 2024 14:23
-
-
Save petski/1414048ca2db37592da2e7af13d718f5 to your computer and use it in GitHub Desktop.
Update the Windows hosts file with the WSL2 IP address and additional hostnames
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
# Description: Update the Windows hosts file with the WSL2 IP address and additional hostnames. | |
# It will add the WSL2 IP address to the hosts file with the hostname `wsl.local`. | |
# It will also add any additional hostnames from the `~/.wslhosts` file in the WSL2 home directory. | |
# | |
# Usage: `powershell -ExecutionPolicy Bypass -File wslhosts.ps1` | |
# | |
# Follow these steps to run the script at each WSL2 boot: | |
# - Open Task Scheduler | |
# - Create a new task | |
# - Set the trigger to run on: | |
# - Log: `System` | |
# - Source: `Hyper-V-VmSwitch` | |
# - Event Id : `102` | |
# - Set the action to start a program and point it to `powershell.exe` with the script file as an argument: | |
# - Program/script: `\Windows\System32\WindowsPowerShell\v1.0\powershell.exe` | |
# - Add arguments: `-ExecutionPolicy Bypass -File "C:\path\to\wslhosts.ps1"` | |
# - Make sure to check these settings are checked: | |
# - "Run whether user is logged on or now" | |
# - "Run with highest privileges" | |
# - For a first test, right click on your newly created task and choose "Run now" | |
# | |
# Inspired by https://gist.github.com/MichaelBelgium/2243e5713833ba44b5675d844eeb1c85 | |
$wslHostname = "wsl.local" | |
$wslDistribution = "Ubuntu" | |
$hostsPath = "$Env:windir\System32\drivers\etc\hosts" | |
$additionalHostnamesPath = "~/.wslhosts" | |
$markerStart = "# WSLHOSTS-START" | |
$markerEnd = "# WSLHOSTS-END" | |
# Make sure we run as Administrator | |
$user = New-Object Security.Principal.WindowsPrincipal $( [Security.Principal.WindowsIdentity]::GetCurrent() ) | |
if ( $user.IsInRole( [Security.Principal.WindowsBuiltinRole]::Administrator ) -eq $false ) { | |
Start-Process powershell.exe -WindowStyle Hidden -Verb RunAs ` | |
-ArgumentList ( '-noprofile -noexit -file "{0}" -elevated' -f ( $myinvocation.MyCommand.Definition ) ) | |
exit | |
} | |
$wslIp = (wsl -d "$wslDistribution" -- ip addr show eth0 '|' grep 'inet ' '|' cut -f 6 -d ' ' '|' cut -f 1 -d '/') | |
if (-not $wslIp) { | |
Write-Error "WSL is not running" | |
exit | |
} | |
$additionalHostnames = @() | |
try { | |
$additionalHostnames = (wsl -d "$wslDistribution" -- cat "$additionalHostnamesPath").Replace("`r", "").Split("`n") | |
} catch { | |
Write-Warning "No additional hostnames found in $additionalHostnamesPath file" | |
} | |
$content = Get-Content $hostsPath | |
$newContent = @() | |
$inMarkerBlock = $false | |
foreach ($line in $content) { | |
if ($line -eq $markerStart) { | |
$inMarkerBlock = $true | |
continue | |
} | |
if ($line -eq $markerEnd) { | |
$inMarkerBlock = $false | |
continue | |
} | |
if (-not $inMarkerBlock) { | |
$newContent += $line | |
} | |
} | |
$newContent += $markerStart | |
$newContent += "$wslIp $wslHostname" | |
foreach ($hostname in $additionalHostnames) { | |
$newContent += "$wslIp $hostname" | |
} | |
$newContent += $markerEnd | |
$newContent | Set-Content $hostsPath -Force | |
Write-Output "Hosts file updated with IP: $wslIp" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment