Skip to content

Instantly share code, notes, and snippets.

@abhirup-dev
Forked from MichaelBelgium/wsl2-hosts-sync.ps1
Created June 26, 2020 04:03
Show Gist options
  • Save abhirup-dev/80dcd873d73b610aa0dd0079a920d95e to your computer and use it in GitHub Desktop.
Save abhirup-dev/80dcd873d73b610aa0dd0079a920d95e to your computer and use it in GitHub Desktop.

Revisions

  1. @MichaelBelgium MichaelBelgium created this gist Jun 5, 2020.
    25 changes: 25 additions & 0 deletions wsl2-hosts-sync.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    # 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!"