Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Installing Multiple Ubuntu Systems in WSL2

Tech 2

This guide exlpains how to install multiple Ubuntu systems within Windows Subsystem for Linux version 2 (WSL2), specifically demonstrating how to import an additional Ubuntu 16.04 system using a tarballl.

Prerequisites

Ensure you have Docker installed and check its version:

docker --version

Setting Up WSL2

Before importing another distribution, configure WSL to use version 2 by default:

# Set WSL 2 as the default version
wsl --set-default-version 2

# Update existing Ubuntu 20.04 installation to WSL 2
wsl --set-version Ubuntu-20.04 2

Obtaining the Ubuntu 16.04 Tar File

To get the required tar file containing all Linux binaries of the desired distribution, we'll use Docker inside our current Ubuntu environment.

Installing Docker Inside Ubuntu 20.04

Create a script called setup-docker.sh with the following commands to set up Docker's APT repository:

#!/bin/bash

# setup-docker.sh
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Run the setup script:

sudo sh ./setup-docker.sh

Install Docker components:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Verify the installation:

sudo docker run hello-world

Creating and Exporting the Ubuntu 16.04 Container

Pull the Ubuntu 16.04 image and create a container from it:

sudo docker pull ubuntu:16.04
sudo docker run -it ubuntu:16.04 /bin/bash

After exiting the container, export it to a tar file. First, identify the container ID:

CONTAINER_ID=$(sudo docker container ls -a | grep -i ubuntu:16.04 | awk '{print $1}')
sudo docker export $CONTAINER_ID > /mnt/c/temp/ubuntu1604.tar

Note: Ensure that /mnt/c/temp/ exists and is accessible from both Windows and WSL.

Importing the Distribution into WSL

With the tar file ready, import it into WSL using PowerShell:

  1. Create directories if they don't exist:

    cd C:\temp
    mkdir E:\wslDistroStorage\ubuntu1604
    
  2. Import the tar file:

    wsl --import Ubuntu-16.04 E:\wslDistroStorage\ubuntu1604 .\ubuntu1604.tar
    
  3. Verify the imported distribution:

    wsl -l -v
    

    You should see output similar to:

      NAME                   STATE           VERSION
    * Ubuntu-18.04           Running         2
      CentOS                 Stopped         2
      docker-desktop-data    Running         2
      docker-desktop         Running         2
      Ubuntu-16.04           Stopped         2
    

Configuring the Default User

Launch the newly imported Ubuntu 16.04 instance:

wsl -d Ubuntu-16.04

Inside the instance, update packages and install sudo:

apt-get update
apt-get install sudo

Add a new user with administrative privileges:

NEW_USER="ztl"
useradd -m -G sudo -s /bin/bash "$NEW_USER"
passwd "$NEW_USER"

Set this user as the default login user by creating or editing /etc/wsl.conf:

tee /etc/wsl.conf <<_EOF
[user]
default=${NEW_USER}
_EOF

Exit and restart the WSL instance so changes take effect:

exit
wsl -t Ubuntu-16.04
wsl -d Ubuntu-16.04

Final, set the root password for switching users via su:

sudo passwd root
Tags: WSL2

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.