Skip to content

Instantly share code, notes, and snippets.

@paulera
Last active June 20, 2023 09:49
Show Gist options
  • Save paulera/b244329d3a818db36d34a93d4ec78417 to your computer and use it in GitHub Desktop.
Save paulera/b244329d3a818db36d34a93d4ec78417 to your computer and use it in GitHub Desktop.
Create a WSL instance from a fresh linux image file. You can have many instances running in parallel.

Create a fresh WSL instance

  • Download ubuntu-20.04-server-cloudimg-amd64-wsl.rootfs.tar.gz from https://cloud-images.ubuntu.com/releases/focal/release

  • Install the .tar.gz image in a folder of your choice: wsl --import <Instance Name> <Folder> <.tar.gz file>

    • example (run in Windows command line):
      mkdir %userprofile%\wsl-developer
      wsl --import Development %userprofile%\wsl-developer ubuntu-20.04-server-cloudimg-amd64-wsl.rootfs.tar.gz
  • Boot up the instance: wsl -d <Instance Name>

  • Create a user with lower permissions for day-to-day use: adduser developer - create a password when asked

  • Make this user a sudoer: usermod -aG sudo developer

  • Add this user to the group www-data: usermod -aG www-data developer

  • Edit the file /etc/wsl.conf:

    [user]
    default = developer
    
    [automount]
    # disable automounht to isolate WSL filesystem from Windows (/mnt/c will not be mounted)
    enabled = true
    # this will make /mnt/c be mounted with correct files permission (not 777 for everything)
    options = "metadata,umask=22,fmask=11"
    
    [network]
    generateHosts = false
    generateResolvConf = false
  • Replace the originally linked resolv.conf with a new file:

    Unlink:

    [ -L /etc/resolv.conf ] && sudo unlink /etc/resolv.conf     # unlink if symlink
    [ -f /etc/resolv.conf ] && sudo rm /etc/resolv.conf         # remove if is file

    New resolv.conf:

    nameserver 10.5.50.31
    nameserver 1.1.1.1
    nameserver 8.8.8.8
    search lan tickle.local
    

    Should you need to restore the initial state of the linked resolv.conf file, this is what you need to do:

    # unlink or delete accordingly
    [ -L resolv.conf ] && sudo unlink resolv.conf
    [ -f resolv.conf ] && sudo rm resolv.conf
    # recreate the original link
    ln -s /etc/resolv.conf ../run/systemd/resolve/stub-resolv.conf
  • Close the sesion with Ctrl+D. On windows command line, restart this WSL instance:

    wsl -t <Instance Name>
    wsl -d <Instance Name>
    

Now your WSL environment should be ready to use. Open it again using Windows Terminal (or your console application of choice). Your should be logged in as developer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment