WSL icon indicating copy to clipboard operation
WSL copied to clipboard

New feature to set alias when I'm installing a new distro like a "--alias" param

Open ohrlando opened this issue 3 years ago • 1 comments

I would like to able run multiples Ubuntu with same release version. But, the names are equal. I should be able to decide which distro run based to an alias.

For example:

$ wsl --install -d Ubuntu-20.04 --alias ExperimentalUbuntu
$ wsl --install -d Ubuntu-20.04 --alias MainUbuntu

$ wsl --list
Distribuições do Subsistema do Windows para Linux:
MainUbuntu           Ubuntu-20.04 (Padrão)
ExperimentalUbuntu   Ubuntu-20.04

I tried to find out how I can rename, but I need to deep diving in Registry Editor. Probably not recommended way

ohrlando avatar May 10 '23 21:05 ohrlando

Yes, it would be nice to have a wsl.exe option to help with this.

And no, I don't recommend registry hacking here. Especially don't change the name without also changing the location, because the next time you run wsl --install -d Ubuntu-20.04, it's going to try to put it in the same location.

The recommended way to do this used to be to export the distribution and re-import it as a new one.

Nowadays, recent versions make this a bit easier with wsl --import --vhd, which can simply create a new distribution from an existing one. I'm going to "script this out" to make it as robust as possible, but I recommend running it line-by-line to (a) make sure you understand what's happening, and (b) can adapt if anything goes wrong along the way.

From PowerShell (as your regular, non-admin user):

wsl --shutdown
# Adjust as needed:
$oldname = "Ubuntu-20.04"
$newname = "ExperimentalUbuntu"
$newLocation = "D:\wsl\instances\ExperimentalUbuntu"

(Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss\ |
    ForEach-Object {
        (Get-ItemProperty $_.PSPATH) | Select-Object DistributionName,BasePath
    } | Where-Object { $_.DistributionName -eq $oldname }).BasePath | tee -Variable oldLocation

# This should print a location like `C:\User\<path>\AppData\Local\<packagename\LocalState

wsl --import --vhd $newname $newLocation $oldLocation\ext4.vhdx

# Distribution is copied, but need to set the default username, otherwise it will always start as root
wsl ~ -u root -d $newname -e sh -c "id -un 1000" | tee -Variable wslUsername

# Confirm the username is correct

# This will add the username to the /etc/wsl.conf in the distribution.
# All four lines must be entered at once, since it's really once command
# split across 4 lines for quoting purposes:
wsl ~ -u root -d $newname -e sh -c @"
echo "[user]
default=$wslUsername" >> /etc/wsl.conf
"@

Once confirmed, you can either remove the old "Ubuntu-20.04" template (with --unregister) or copy it again.

NotTheDr01ds avatar May 12 '23 21:05 NotTheDr01ds