Skip to content

Instantly share code, notes, and snippets.

@nicoverbruggen
Last active April 1, 2024 08:35
Show Gist options
  • Save nicoverbruggen/2d81dc0516a4c1d32c2afc7d9a7cf839 to your computer and use it in GitHub Desktop.
Save nicoverbruggen/2d81dc0516a4c1d32c2afc7d9a7cf839 to your computer and use it in GitHub Desktop.
Use different Ubuntu mirror on WSL

First, take a look at: https://launchpad.net/ubuntu/+archivemirrors

Then, pick a mirror closeby. For example, for me it's: ftp.belnet.be

As such, you can now run:

sudo sed -i "s/archive.ubuntu.com/<your-mirror-here>/" /etc/apt/sources.list && sudo apt update

Still, some network operations may be slow using WSL. You may want to add some exceptions to MS Defender.

Open a new Powershell and run:

 Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Afterwards, run the script below (script.ps1) and reset your execution policy if needed:

 Set-ExecutionPolicy -ExecutionPolicy Default
############
# This script will add your WSL environments to the Windows Defender exclusion list so that
# realtime protection does not have an adverse effect on performance.
#
# You should be aware that this could make your system less secure. Use at your own risk.
# Note: This should be run from an administrative PowerShell prompt
############
# Find registered WSL environments
$wslPaths = (Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss | ForEach-Object { Get-ItemProperty $_.PSPath}).BasePath
# Get the current Windows Defender exclusion paths
$currentExclusions = $(Get-MpPreference).ExclusionPath
if (!$currentExclusions) {
$currentExclusions = ''
}
# Find the WSL paths that are not excluded
$exclusionsToAdd = ((Compare-Object $wslPaths $currentExclusions) | Where-Object SideIndicator -eq "<=").InputObject
# List of paths inside the Linux distro to exclude (https://github.com/Microsoft/WSL/issues/1932#issuecomment-407855346)
$dirs = @("\bin", "\sbin", "\usr\bin", "\usr\sbin", "\usr\local\bin", "\usr\local\go\bin")
# Add the missing entries to Windows Defender
if ($exclusionsToAdd.Length -gt 0) {
$exclusionsToAdd | ForEach-Object {
# Exclude paths from the root of the WSL install
Add-MpPreference -ExclusionPath $_
Write-Output "Added exclusion for $_"
# Exclude processes contained inside WSL
$rootfs = $_ + "\rootfs"
$dirs | ForEach-Object {
$exclusion = $rootfs + $_ + "\*"
Add-MpPreference -ExclusionProcess $exclusion
Write-Output "Added exclusion for $exclusion"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment