Wsl2 new

From UVOO Tech Wiki
Revision as of 17:45, 23 August 2024 by Busk (talk | contribs) (Created page with "Install Ubuntu 22.04 instance ``` $ErrorActionPreference = "Stop" $distributionName = "example1" $distrib = "ubuntu-22.04" $installLocation = "C:\WSL\$distributionName" $ta...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Install Ubuntu 22.04 instance

$ErrorActionPreference = "Stop"

$distributionName = "example1"
$distrib = "ubuntu-22.04"

$installLocation = "C:\WSL\$distributionName"
$tarballUrl = "https://cloud-images.ubuntu.com/releases/jammy/release/${distrib}-server-cloudimg-amd64-root.tar.xz"
$tarballPath = "$env:TEMP\${distrib}.tar.gz"

wsl --set-default-version 2

Invoke-WebRequest -Uri $tarballUrl -OutFile $tarballPath

wsl --import $distributionName $installLocation $tarballPath
wsl -d $distributionName -u root -- bash -c "adduser --disabled-password --gecos '' newuser && usermod -aG sudo newuser"
wsl -d $distributionName -- bash -c "echo -e '[user]\ndefault=newuser' > /etc/wsl.conf"
# Remove-Item $tarballPath
Write-Output "WSL distribution '$distributionName' with $distrib has been installed and configured."

Routing fixes if needed

$interfaceAlias = "vEthernet (WSL (Hyper-V firewall))"
$interfaceName = "usb"
$vpnInterfaceRegex = "PANGP*"
$vpnDNS = "10.x.x.y"
$ipOfDefaultGatewayIPRouteToDelete = "192.168.1.1"

$ErrorActionPreference = "Stop"

if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    $arguments = "& '" + $myInvocation.MyCommand.Definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    exit
}


Get-NetIPInterface -InterfaceAlias $interfaceAlias | Set-NetIPInterface -InterfaceMetric 1
Get-NetAdapter | Where-Object { $_.InterfaceDescription -like $vpnInterfaceRegex } | Set-NetIPInterface -InterfaceMetric 4000

Set-DnsClientServerAddress -InterfaceAlias $interfaceName -ServerAddresses ($vpnDNS)
route delete 0.0.0.0 MASK 0.0.0.0 $ipOfDefaultGatewayIPRouteToDelete