Category: Windows

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

  • How to Open WSL Projects in Windows File Explorer

    How to Open WSL Projects in Windows File Explorer

    This is a guide on how to open WSL files in Windows File Explorer, with added descriptions.

    The best practice for managing project files in WSL is to store your files on the same Operating System as the tools your plan to use. So if the reason you’re using WSL is to develop an app in Linux, then keeping your files in WSL (use Linux root directories, not /mnt/c/Users/etc).

    Also to note, copying files to and from WSL and your local Windows machine can usually be avoided by storing all your WSL application code within source control (e.g. Git / Bitbucket).

    With that said, there are 2 ways to open WSL directories in Windows Explorer.

    The quick way of opening WSL project files in Windows Explorer is to run the following, while in your Linux terminal session –

    # open windows explorer from within wsl
    explorer.exe . 
    WSL Ubuntu

    This opens the directory you are already working in on your Terminal.

    … Now follows, the alternative method of opening WSL project files in Windows Explorer.

    Open Windows Explorer and navigate to \\wsl$\ as shown –

    WSL Windows File Explorer

    The above is showing my only running WSL Linux distribution, even though I have 3 WSL distros installed.

    WSL List Versions

    I’m going to open the Ubuntu 20.04 distro in a new tab in Windows Terminal to start it up –

    WSL Distros

    Now I can see both distributions files in Windows Explorer –

    WSL Windows Files

    From here we can drill into the folders and navigate to any directory –

    WSL Windows Explorer

    For further reading, see this Microsoft Docs link for more info on WSL Best Practises.

  • How to Change Default Linux Distro in WSL

    How to Change Default Linux Distro in WSL

    This post is a guide on how to change a WSL distribution, from your default WSL Linux distro in the Windows Subsystem for Linux This is useful when you are managing multiple WSL distros on your computer.

    List Installed WSL Distros

    First up, we want to display your installed WSL distributions and versions by running the following command.

    # show installed wsl distros & versions
    wsl -l -v
    Get WSL Versions

    The star at the distro name indicates the current default WSL Linux distribution.

    When we enter WSL by running the wsl command, we will log into the Ubuntu-20.04 distro.

    WSL enter Default Distro

    We can run lsb_release -a as above to verify which Linux distribution we are using.

    Set Default WSL Linux Distro

    To set the default WSL Linux distro we need to run wsl -s <wsl-distribution-name>

    Before running this command, the WSL distro name is as-per what is shown in the wsl -l -v command.

    # wsl show versions
    wsl -l -v
    
    # set default wsl distribution (as per 'wsl -l' name)
    wsl -s <wsl-distribution-name>
    wsl default distro

    Noticing here the same ‘lsb_release -a‘ command does not work for me when in the Debian distro – using cat /etc/os-release instead.

    That’s it for this one and is all you should need to know on changing your default WSL distribution. For more WSL Admin tips, check out some of my other WSL Blog posts:
    # How to Reboot WSL (Windows Subsystem for Linux)
    # How to Upgrade WSL from Version 1 to Version 2
    # How to Check WSL Versions

  • How to Install Postgres on Ubuntu 20.04 in WSL

    How to Install Postgres on Ubuntu 20.04 in WSL

    This is a post on how to install Postgres on Ubuntu 20.04, on Windows Subsystem for Linux (WSL). This should hopefully be a straightforward installation for you, which is done by following these 2 steps:

    # Update Local Packages on Ubuntu in WSL
    # Install Postgres on Ubuntu


    Update Local Packages on Ubuntu in WSL

    First, we need to update our local packages, by running the following apt command:

    # update local packages
    sudo apt update
    ubuntu sudo apt update

    Install Postgres on Ubuntu

    The next command will install Postgres. We’re including the ‘-contrib‘ package which will give us extra functionalities. After installing with the first command I need to restart the Postgres Service to resolve an “error: could not connect to server: No such file or directory” error message.

    # install postgres on ubuntu
    sudo apt install postgresql postgresql-contrib
    
    # restart postgres service ubuntu
    sudo service postgresql restart
    Install Postgres on Ubuntu
    service postgresql restart

    Once the installation completes, we can enter psql using the ‘postgres’ user that was automatically created during the installation.

    # login to psql with the out of box postgres user
    sudo -u postgres psql
    
    \q (to quit back to terminal)
    Enter PSQL
  • How to Check WSL Versions

    How to Check WSL Versions

    To check your installed Windows Subsystem for Linux (WSL) versions, run wsl -l -v. This will show all installed WSL Linux distro names & versions.

    WSL allows users to develop and run applications within a GNU/Linux environment on a Windows computer. Previous to this feature release we needed Virtual Machines (VMs), and dual boot setups were much more common if you wanted local test environments.

    There are 2 versions of WSL, the old (1), and the new (2). We should ideally be using the latest version like all other software in existence, but there are use cases where we might prefer to use WSL Version 1.

    As always, we should look for the most relevant MS Docs when researching a Microsoft Product or Application. The image below shows the current feature differences between WSL 1 and WSL 2.

    wsl1 vs wsl2

    Have a look at Microsoft Documentation – Comparing WSL 1 and WSL 2 for more information on WSL version differences. One of the key differences in 2022 is that WSL 2 allows the use of Docker in WSL.

    How to Check WSL Versions

    To check the versions of your installed WSL distros, open a PowerShell Terminal and run wsl -l -v

    # wsl check version
    wsl -l -v

    The command above lists all installed Windows Subsystem Linux distributions on your local machine, including WSL versions (WSL1 or WSL2) and their state (Running or Stopped).

    wsl check version

    I’m using Windows Terminal in this demo, which is great for managing multiple WSL distributions. We are able to open multiple Linux WSL distros and manage them using tabs as displayed in the screenshot above.

    That concludes this post on how to check a WSL version. Feel free to check out some of my other WSL posts that relate to version checks and the Windows Subsystem:
    # How to Upgrade WSL from Version 1 to Version 2
    # How to Downgrade from WSL Version 2 to WSL Version 1
    # How to Change Default Linux Distro in WSL

  • How To Install Windows Subsystem for Linux (WSL)

    How To Install Windows Subsystem for Linux (WSL)

    To install WSL (Windows Subsystem for Linux), we first enable the WSL Windows Feature using PowerShell, then install WSL Linux Distributions via the Microsoft Store.

    This post will help guide you through enabling and installing Windows Subsystem for Linux (WSL) on your local Windows computer.

    WSL allows Windows users to develop and run applications within a GNU/Linux environment. Previous to this feature release we needed Virtual Machines (VMs), and dual boot setups were much more common if you wanted local test environments.

    The list of available Linux Distributions for installation, current as of this post date, includes the following:

    – 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)

    For more information on WSL as a whole, have a look at the MS Docs – Microsoft Documentation: What is Windows Subsystem for Linux?
    https://aka.ms/wslinstall

    This blog post is a demo to help you install WSL for the first time on your Windows machine. FYI, there’s a reboot of your computer required at the end of the first step (enabling WSL).

    # Enable Windows Subsystem for Linux (WSL)
    # Download & Install a Linux Distro

    Enable Windows Subsystem for Linux (WSL)

    As described above in this post, need to enable the optional WSL Windows Feature before we look at installing a Linux Distribution.

    To enable WSL, we need to open PowerShell, running as Administrator.

    PowerShell Run as Administrator

    Then run the following PowerShell cmdlet, PowerShellEnable-WindowsOptionalFeature which will enable the Windows Subsystem.

    # windows subsystem for linux enable
    PowerShellEnable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    PowerShell Enable WSL

    You’ll then be prompted to restart your local computer.

    PowerShell Enable WSL

    Reboot your Windows machine and then proceed to the next steps to install a Linux WSL distro.

    Download & Install a Linux Distribution

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

    We can also download and install Linux Distributions manually. To download a WSL Linux Distro, have a look at this MS Docs link Microsoft Documentation – Downloading distributions

    In this post, we’re going for the simple Linux install option, which is done via the Microsoft Store.

    Microsoft Store Linux

    Select Apps within your search to filter your list, and find your desired Linux Distribution. We’re choosing Ubuntu 20.04 in this demo.

    When you click to open this Ubuntu LTS App, a WSL terminal will be opened and will prompt you for a username and password for Linux.

    WSL Ubuntu 20.04

    We are now in our Ubuntu 20.04 instance and can now test or develop in the Linux world, on Windows.

    If we close this app down and open a PowerShell or Windows Terminal, we can enter this WSL Distribution by typing wsl.

    WSL Version

    That about wraps things up for now on installing the Windows Subsystem.

    For more useful tips on administering WSL, have a look at some of my other WSL blog posts:
    # How to Reboot WSL (Windows Subsystem for Linux)
    # How to Upgrade WSL from Version 1 to Version 2
    # How to Change Default Linux Distro in WSL
    # How to Check WSL Versions

  • Git Log Show in One Line (–oneline)

    Git Log Show in One Line (–oneline)

    To check our commit history on a Repository within Git, we use the git log command. When using this command we scroll through the commit log within our Terminal by hitting the Enter key, and then we need to hit q on our keyboard to exit reading the git log.

    This post is to demo the --online parameter that can be added to the git log statement. Adding this parameter to the git log command will return a condensed commit log history, which shows the first part of the commit hash and message on one line.

    The following is included in this demo post:
    # Git Log
    # Git Log –OneLine
    # Git Log –OneLine -5
    # Git Log –OneLine with Custom Formatting

    Git Log

    In the following example, I’m running the standard git log command on one of my Repos. This is what we’d use if we want information including full commit hashes, commit dates/times, and authors.

    Git Log

    Git Log –Oneline

    This next example shows will return a condensed commit log history. It’s the same command as above but we’re adding the --oneline parameter.

    # Show git log in one line
    git log --oneline
    Git Log --oneline

    Git Log –Oneline -5

    You might think the above shows too many logs on the screen. We can pass in a line number parameter for it to return a specific number of commits.

    This next example shows with and without the above –oneline parameter. We limit the number of commits/rows being returned by adding ‘-5‘ (can be any number).

    # show most recent 2 commits
    git log -2
    
    # show most recent 5 commits on one line
    git log -5 --oneline
    Git Log -5

    Git Log –Oneline -5 with Custom Formatting

    We can amend the formatting of the returned list, including changing colours, adding in commit times, and adding the contributor.

    This would be ideal if it was added as a function on your Terminal Profile.

    # show most recent 5 commites on one line, with formatting
    git log -5 --graph --pretty=format:'%C(red)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr) %C(yellow)<%an>%Creset'
    Git Log Formatting

    That’s it for this tip, check out my other posts on this topic in the Git Tag if of interest.

  • How To Change From WSL2 to WSL1

    How To Change From WSL2 to WSL1

    This is a post on how to change a Windows Subsystem for Linux (WSL) Distribution from running on Version 2 to Version 1.

    WSL has two versions, the latest (2), and the original version (1). You can have many different Linux Distributions installed on your machine, and you can easily change the WSL Version for one of them.

    This Microsoft documentation page explains the key differences between versions and will help explain why you might want to downgrade back to version 1 for your particular WSL development needs.

    To change one of your WSL Distributions from WSL2 to WSL1, we just need to pass in the –set-version parameter and it’ll be converted very quick. I’m listing my available WSL Distro’s before and after to visually verify the change.

    # check wsl distro versions
    wsl -l -v
    
    # change wsl distro using name above to desired version
    wsl --set-version Ubuntu 1
    
    WSL Downgrade WSL2 to WSL1

  • How to Reboot a WSL Distribution/Instance

    How to Reboot a WSL Distribution/Instance

    This is a post on how to reboot a Windows Subsystem for Linux (WSL) virtual machine environment on your local machine. I’ve needed to do this a few times in the past year, my WSL instance throws some random error which was only resolved after a reboot.

    As far as I know, we are unable to reboot from within WSL due to the lack of systemd. There are some open-source projects in the works to enable this which I’ve not tried yet. So we need to run a simple ‘wsl’ command to shutdown the host, and the enter WSL to get it back online.

    This example shows exactly that, and I’m listing my installed WSL Distributions between each step.

    # check wsl version
    wsl -l -v
    
    # reboot wsl2 instance
    wsl --shutdown
    # enter default wsl instance to boot
    wsl

    If you have multiple Linux Distributions installed on your Windows machine, by default you can only have one of them running, so when you’re switching between using ‘wsl -d <instanceName>’ the other will go offline. There is a way to get multiple WSL instances running at the same time, even with a GUI – have a look at this WSL Distro Manage project in GitHub if of interest.

    One last note, the LXSS Manager Service is the broker to the Linux subsystem driver. We can also restart this service or of course reboot our local machine.

  • How to Check WSL Versions

    How to Check WSL Versions

    WSL allows users to develop and run applications within a GNU/Linux environment on a Windows computer. Previous to this Windows Feature you might have needed VM’s or a dual boot setup for this type of test/development environment.

    There are 2 versions of WSL, the old (1), and the new (2). We should ideally be using the latest version like all other software in existence, but there are use cases where we might prefer to use WSL Version 1. This Microsoft documentation page explains the key differences between versions, and this post is to help explain how to check your WSL Version.

    Personally I prefer to use WSL Version 2 as we’re able to make use of more apps like Docker. Also, I don’t need to worry about the exceptions for when it’s best to use WSL Version 1.

    With descriptions out of the way, we can check our WSL Versions by running the following from a PowerShell Terminal –

    # check wsl version
    wsl -l -v

    The above gives us a list of all installed Linux Distributions on your local machine including WSL Versions.

  • How to Install and Configure AWS CLI on Windows

    How to Install and Configure AWS CLI on Windows

    This post is a how-to for installing & configuring AWS CLI (Version 2) on Windows.

    In this guide, we’re going to download the AWS Command Line Interface installation media and run through the simple installation. Once done we’ll configure AWS CLI, which you’ll need an AWS Account to do so.

    # Install AWS CLI V2 on Windows
    # Configure AWS CLI V2

    Install AWS CLI V2

    Download AWS CLI V2 and run through the MSI installer. Run the .msi file you downloaded, and click next, next & done to complete the installation.

    Alternatively, we can install AWS CLI using the PowerShell commands below.

    # Download AWS CLI msi file, output to current directory
    Invoke-WebRequest -Uri https://awscli.amazonaws.com/AWSCLIV2.msi -UseBasicParsing -OutFile 'AWSCLIV2.msi'
    
    # Run AWS CLI install
    .\AWSCLIV2.msi
    AWS CLI Install Windows

    When the installation completes, close and re-open any terminals you have open. You should remember/consider doing this every time you install a package for development, close/reopen your VS Code or Windows Terminal.

    Open your command terminal and verify the AWS CLI install by checking the version. Run ‘aws –version‘ as shown in the example below.

    Check AWS CLI Version Windows

    Configure AWS CLI V2

    Now that we have AWS CLI installed on our machine, we need to open a web browser for the next steps, and head to the AWS Console.

    Open AWS IAM and create/select a user with appropriate permissions. My ‘pete‘ login has full Admin (AdministratorAccess Policy) in this case.

    AWS IAM Users

    Click on the Security Credentials tab within the user properties.

    AWS IAM User Creds

    Scroll down to view & create Access Keys. Click to create a new key to see both the Access key ID and Secret Access Key of existing keys.

    AWS IAM User Access Keys

    Open PowerShell and run ‘aws configure‘. You’ll be prompted for:
    AWS Access Key ID (above)
    AWS Secret Access Key (above)
    Default Region Name
    Default Output Format

    AWS CLI Check Version in PowerShell

    Once you’ve entered these details for the first time it’ll save your details, so for me above I’m just hitting enter at each prompt.

    Run ‘aws sts get-caller-identity‘ to confirm what you’re logged in as.

    AWS sts-get-caller-identity