Category: Windows

Windows Administration Blog Archives, by Peter Whyte (Database Administrator).
Includes: Database Admin, Windows Admin, PowerShell, WSL & more…

  • How to Install Ubuntu 20.04 on WSL

    How to Install Ubuntu 20.04 on WSL

    This guide shows how to install Ubuntu 20.04 on WSL (Windows Subsystem for Linux). Before installing a WSL Linux distribution you should have already enabled WSL on your computer.

    All WSL Linux distributions are available for download in the Microsoft Store. This is the easiest way to install a WSL Linux distro. If you are automating WSL installations, you may be looking for the WSL distro download links and PowerShell install command.

    This post contains the following methods to get Ubuntu installed on WSL ->
    # Install WSL Ubuntu 20.04 via Microsoft Store
    # Install WSL Ubuntu 20.04 via Command

    Install WSL Ubuntu 20.04 via Microsoft Store

    You can find all the available WSL Linux distributions in the Microsoft Store available for download. This may be restricted by Group Policy if you’re on a Domain (a work/corporate computer).

    Click Start, open the Microsoft Store and simply search for the Ubuntu distribution version you desire.

    Microsoft Store Ubuntu WSL

    Click to download/install Ubuntu WSL, and then go ahead and install Windows Terminal while you’re at it.

    Windows Terminal Microsoft Store

    Once both of the above installs are complete, search and run Windows Terminal, then enter Ubuntu by running wsl in the terminal.

    Open WSL Ubuntu

    Install WSL Ubuntu 20.04 via Command

    To install WSL via PowerShell script or command, we’d download the WSL Linux distro and then extract/install it.

    The Microsoft Documentation contains a full list of WSL download links for each of the available WSL Linux distros:-
    – Ubuntu 20.04 (LTS / 18.04 LTS)
    – openSUSE Leap 15.1
    – SUSE Linux Enterprise Server (15 SP1 / 12 SP5)
    – Kali Linux
    – Debian GNU/Linux
    – Fedora Remix for WSL
    – Pengwin (+ Enterprise)
    – Alpine WSL
    – Raft (Free Trial)

    We are installing Ubuntu 20.04 in this demo, manually via a web browser rather than PowerShell Invoke-WebRequest or Curl.

    Move the downloaded file into some directory.

    Now into the directory within PowerShell, rename the file from .appx to .zip format.

    # rename Ubuntu appx file to zip format
    # add-appxpackage doesn't always work, extracting the .exe seems better
    Rename-Item .\Ubuntu_2004.2020.424.0_x64.appx .\Ubuntu_2004.zip

    Extract the zip file into a new directory.

    # extract the zip file into a new dir
    Expand-Archive .\Ubuntu_2004.zip .\Ubuntu_2004

    Run the ubuntu2004.exe file within the extracted directory and follow the setup instructions to create a new Linux login.

    Once done wsl should be waiting for you.

    Lastly, I’m now exiting (‘exit’ command) out of WSL, listing out my Linux distros and setting this Ubuntu 20.04 distro as my default.

  • How to Unregister a Linux Distro in WSL

    How to Unregister a Linux Distro in WSL

    If you want to remove a Linux distro from your Windows Subsystem for Linux (WSL) installation, you can use the wsl --unregister <distro-name> command to unregister it. This is useful for cleaning up an old installation and starting fresh with a new distro.

    To unregister a Linux distro in Windows Subsystem for Linux (WSL), you can follow the steps in this blog post.

    Keep in mind that unregistering a distro will permanently delete all data, settings, and software associated with it. If you want to use the distro again, you will need to reinstall it from the Microsoft Store.

    Uninstall a WSL Distro

    Open a command prompt or terminal and type the following command to show a list of all the WSL distros currently installed on your machine:

    # show list of wsl distros
    wsl -l -v
    

    Identify the distro you want to remove from the list and type the following command, replacing <distro_name> with the name of the distro you want to unregister:

    # unregister a wsl distro
    wsl --unregister <distro_name>
    
    wsl --unregister

    In the example given above, the user has unregistered their Ubuntu-20.04 distro.

    If you want to learn more about using the Windows Subsystem for Linux (WSL), you can check out my WSL page. It includes a list of all my latest posts and notes on the topic.

  • How to Set Default WSL Distribution

    How to Set Default WSL Distribution

    To set your default Windows Subsystem for Linux (WSL) distribution, get the Linux distro name by running wsl -l -v, then run wsl -s to set the new default WSL distro.

    This post shows a demo of setting the default WSL Linux distribution on a Windows computer. This is useful if you have more than one WSL distro installed and you’re managing multiple Windows WSL environments.

    There are 2 parts to this guide:
    # Check Installed WSL Distros
    # Set Default WSL Distro

    Check Installed WSL Distros

    To view your currently installed WSL Linux distributions on your Windows computer, run the following WSL command:

    # Check installed WSL distros
    wsl -l -v
    WSL List Installed Distros

    The star at the distro name indicates the current default Linux distribution. If we enter back into WSL by running wsl on its own, we will open the default WSL distro, Ubunutu 20.04 in this case.

    Change Default WSL Distro

    To set the default Linux distro in Windows Subsystem for Linux, run wsl -s <distribution-name> in a PowerShell Terminal. You should get the Linux distribution name for this command from the WSL list command displayed above in this post.

    # change default linux distro in windows subsystem
    wsl -s <distro_name>
    WSL Set Default Distro

    We have now set the default WSL distribution to WSL2 Debian. This won’t interrupt an active WSL terminal session if you have one open.

    The screenshot below displays running the WSL command to enter the default WSL distro, and I’m running a few test commands within Debian WSL Edition to verify which Linux version this is.

    WSL Enter Default Distro

    That’s covered for setting your default WSL distro on the Windows Subsystem. Check out my WSL Windows Tips page for similar WSL administration guides.

  • Install oh-my-posh On Windows

    Install oh-my-posh On Windows

    Oh-my-posh is a theme engine for PowerShell that’s separate from your default PowerShell console. It’s powered by posh-git and has status indications for git, failed, and admin commands.

    Oh-my-posh requires and runs in Windows Terminal which was my previous blog post.

    This post is just a quick guide on installing oh-my-posh and showing off my current favourite theme.


    Install oh-my-posh

    Installing using Chocolatey here and you’ll also need to install posh-git too. This can also be done with the Import-Module cmdlet, an example of which is included in the docs.

    choco install oh-my-posh

    Once installed pick a theme – see this link in the oh-my-posh documentation for screenshot examples for each of the themes

    Set-Theme Zash

    Looking good now…


  • How to Install Windows Terminal

    How to Install Windows Terminal

    Windows Terminal is a more modern command-line tool that has many additional features. A major benefit of WT is the multiple tabs feature.

    Windows Terminal multiple tabs

    Other benefits of Windows Terminal include Unicode / UTF-8 character support, GPU-accelerated text rendering, and the ability to create your own themes & shortcuts.

    This blog post contains a demo on how to install Windows Terminal, with an added tip on how to open PowerShell as Administrator using Windows Terminal.

    # Install Windows Terminal (WT)
    # Run PowerShell WT Session as Administrator

    Install Windows Terminal (WT)

    I’m installing using Chocolatey, the Windows package manager. Note, a reboot is required post-install.

    # install windows terminal chocolatey
    choco install microsoft-windows-terminal

    Search and open Windows Terminal.

    Click the drop-down to open new tabs and you’re good to go.

    Run PowerShell WT Session as Administrator

    Run the command below and a new Windows Terminal will open.

    # Run PowerShell as administrator with Windows Terminal
    Start-Process -Verb RunAs cmd.exe '/c start wt.exe -p "Windows PowerShell"'

    And here’s a typical command that would require admin elevation.


  • Install posh-git on Windows

    Install posh-git on Windows

    Posh-git is a PowerShell module that integrates with Git to show git status information on the command line, and it comes with tab completion for common commands.

    This post is a quick guide on installing this using the Chocolatey package manager.

    Install Posh-Git

    With Chocolately the posh-git install command is:

    choco install poshgit

    How Posh-Git Looks

    After install navigate into a local git repo. It’s showing me to be on the master branch.

    Below is a quick demo of it working – I’m creating a new branch, creating a new text file and staging the file.


  • Install Chocolatey on Windows

    Install Chocolatey on Windows

    This post is a guide on installing the Chocolatey package manager on Windows by running a PowerShell command.

    Chocolatey allows you to manage software packages on your system with simple ‘choco’ commands. In this post after I install Chocolatey I’m installing a GUI to view all installed packages.

    # Install Chocolatey using PowerShell
    # Install Chocolatey GUI


    Install Chocolatey using PowerShell

    Run the following script within PowerShell (as Administrator):

    Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

    Check the version of choco to verify the install, or go ahead and install a package (e.g. choco install git).


    Install Chocolatey GUI

    Installing a package is done with ‘choco install <packagename>’. There’s about 8,000 maintained packages at the time of writing this. Some of the most popular packages include Chrome, Firefox, Java, Flash, Notepad++, 7zip, Git & Python.

    choco install chocolateygui

    Now open the Chocolatey GUI application. There will be a shortcut for this in your Start Menu too.

    You can uninstall/reinstall packages from the GUI.


  • Change Git Remote URL to SSH from HTTPS

    Change Git Remote URL to SSH from HTTPS

    This post is a guide on how to change Git from using SSH to HTTPS for authentication.

    Using SSH for your version control system is more efficient as we do not have to enter a password every time we need to contact the remote repository.

    You should have SSH keys already setup before proceeding with the steps below. If you don’t yet have SSH keys setup, have a look at my other post which will help guide you through creating SSH keys on an Ubuntu WSL instance – Setup SSH on Ubuntu (WSL)

    The following steps demo changing SSH to HTTPS:
    # Check Current Git URL
    # Change Git from HTTPS to SSH auth

    Check Current Git URL

    I’m going to clone a git repo using HTTPS to set this demo up –

    Git Clone Repo HTTPS

    Now that we are authenticating to Git using HTTPS, we should check the Git remote URL.

    The following command will return your remote Git URL –

    # Check current remote git url
    git remote get-url origin

    If the returned value starts with HTTPS, that means we are using HTTPS to auth to Git, not SSH.

    As mentioned above, we want to move from this to SSH so we can avoid entering a password everytime we need to authenticate to the Git system. Lets make the move over to SSH auth!

    Change Git from HTTPS to SSH Auth

    We are now going to change from HTTPS to SSH in Git.

    Navigate to your Git repository link and select SSH. For this demo I’m using the Bitbucket version control system. It’s all the same though and you should be able to find the similar drop-down menu below.

    Bitbucket Remote URL

    Run the following command to set the new Git URL and make the move to SSH auth –

    # Set remote URL for Git (to SSH)
    git remote set-url origin <ssh-url>
    Git Set URL to SSH

    The above screenshot shows various commands to verify this works as expected, including git log –online which is great for checking recent repo commits with a condensed output.

  • Setup SSH Keys in WSL

    Setup SSH Keys in WSL

    Setting up SSH keys on Windows is generally done via OpenSSL or Putty. We can set this up and have quick authentication to places like GitHub or Bitbucket. But, we are also able to rely on a Linux distro in WSL for our SSH. If you are developing in WSL, this might be useful as that’s where you will be cloning your git repos to.

    This post contains a demo of setting up SSH keys in WSL (Windows Subsystem for Linux), on an Ubuntu 20.04 distro. The version-control system being used here is Bitbucket.

    The steps included in this SSH WSL setup are as follows:
    # Create New SSH Key
    # Add SSH Key to Agent
    # Add SSH Key to Project

    Create New SSH Key

    I’m creating a new SSH key within a WSL linked folder, which means my SSH keys are backed up on the local machine in case anything happens on this WSL instance. Even though I can easily recreate and add a new SSH key to my Bitbucket account if need be.

    This SSH key is being created with a password and being added to the SSH Agent so I don’t need to enter the password each time.

    The following command will create a new SSH key in my WSL Ubuntu instance –

    # Create a new ssh key
    ssh-keygen -t rsa -b 4096 -C "someone@email.com"
    WSL ssh-keygen

    Add SSH Key to Agent

    As mentioned above by adding our new SSH key to the ssh-agent so we don’t have to enter the passphrase every time we use it.

    Run the following to add the SSH key to SSH Agent –

    # Add ssh key to agent
    eval $(ssh-agent -s)
    ssh-add pw_bitbucket

    Add SSH Key to Project

    Now that the SSH key is ready, we just need to add that to the Git account you’re using. As mentioned, I’m using Bitbucket in this demo.

    Run the cat Linux command on the public SSH key (.pub file) to show the SSH key for Bitbucket. Copy everything except the email at the end.

    # Ssh to app/host
    ssh -T git@bitbucket.org

    Below is an example of when you haven’t added the SSH key to your account yet –

    This is what we are going to do now. Add the SSH key to your git/Bitbucket account.

    Now re-trying the SSH connection –

    logged in as bl4h130b1aH” – We can see it has worked this time. Our SSH keys are stored in Bitbucket and we are logged in, and authenticated via SSH.

    We are ready to clone a repo into WSL. Navigate to the desired folder in the WSL Linux terminal and run the git clone command as per the repo link.

    The git repo has been cloned successfully. I will now open Visual Studio Code and get to work!

  • “Config Lock Failed Operation not Permitted” – Git

    “Config Lock Failed Operation not Permitted” – Git

    When attempting to clone a git repository on a linked folder within Windows Subsystem for Linux (WSL), I encountered the following error message:

    Cloning into 'pw-iam'...
    error: chmod on /mnt/c/projects/pw-iam/.git/config.lock failed: Operation not permitted
    fatal: could not set 'core.filemode' to 'false'
    
    WSL Git Clone Linked Folder Error

    This error occurs when cloning repos to a mounted Windows drive in WSL. I am not sure of the fix for this issue, but if anyone has a solution, please let me know. Otherwise, this post serves as a note-to-self for me to try and figure out the solution in the future.