Category: Linux

Linux Blog Archives, by Peter Whyte (SQL Database Administrator). Includes random Linux posts, and Windows Subsystem for Linux (WSL).

  • 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 Postgres on Ubuntu 20.04

    How to Install Postgres on Ubuntu 20.04

    This post is a short note on how to install Postgres on Ubuntu 20.04.

    I’m doing this in WSL, however this shouldn’t make any difference if you’re on the same Ubuntu version.

    First, lets update our local packages by running the following apt command –

    # update local packages
    sudo apt update

    This Postgres installation is very quick and simple.

    The following command is also including the ‘-contrib‘ package which gives us extra functionalities.

    # install postgres on ubuntu
    sudo apt install postgresql postgresql-contrib

    Once the installation completes, we can enter psql using the postgres user created automatically during the install.

    # login to psql with the out of box postgres user
    sudo -u postgres psql
    
    \q (to quit back to terminal)

    There may be a need to restart the services post install which can be done by running the following.

    # restart postgres service ubuntu
    sudo service postgresql restart

  • How to Manage S3 Buckets with AWS CLI

    How to Manage S3 Buckets with AWS CLI

    This is a post on how to help manage S3 Buckets with AWS CLI, and to help explain some local Operating System (OS) considerations when running such commands.

    First of all, you will need to be authenticated to your AWS Account and have AWS CLI installed. I cover this in previous blog posts:
    # How to Install and Configure AWS CLI on Windows
    # How to Install and Configure AWS CLI on Ubuntu

    I’m more often involved in the PowerShell side rather than Linux. AWS CLI commands do the same thing in both environments, but the native (OS) language is used around it for manipulating data for output and for other things like wrapping commands in a For Each loop. All commands in this post can run on either OS.

    PowerShell is cross-platform and has supported various Linux & DOS commands since its release. Some are essential for everyday use, for example, ping, cd, ls, mkdir, rm, cat, pwd and more. There are more commands being added over time like tar and curl which is good to see. Plus, we have WSL to help integrate non-supported Linux commands.

    Here’s one of the simplest examples which list all S3 buckets the executing IAM User owns within your AWS Account.

    # List all buckets in an AWS Account
    aws s3 ls
    AWS S3 List Buckets

    The Default Region is configured during the AWS CLI Configuration as linked above. We can change this by running aws configure set region or configure your Environment Variables. Alternatively, we can pass in the –Region variable after ‘ls’ in the command to get a specific Region’s S3 Buckets. There are more ways for us to run commands across multiple Regions which I hope to cover another day.

    Now I’m going to run a command to show me the Access Control List (ACL) of the bucket, using the list of Bucket Names I ran in the previous command. This time, I’m utilising the s3api command rather than s3 – look here for more information on the differences between them. When running AWS CLI commands these API docs will always help you out.

    # Show S3 Bucket ACL
    aws s3api get-bucket-acl --bucket my-bucket

    Next up, I’m going to create a bucket using the s3 command rather than s3api. The reason I’m doing this is, I want to rely on my Default Region for the new S3 Bucket, rather than specifying it within the command. Here’s AWS’s explanation of this –

    “Regions outside of us-east-1 require the appropriate LocationConstraint to be specified in order to create the bucket in the desired region – “
    –create-bucket-configuration LocationConstraint=eu-west-1
    AWS API Docs

    The following command is creating a new S3 Bucket in my Default Region and I’m verifying the location with get-bucket-location afterwards.

    # Change AWS CLI Default Region
    aws configure set region eu-west-1
    
    # Create a new S3 Bucket in your Default Region
    aws s3 mb s3://pw-blog-bucket-101
    
    # Check the Region of a S3 Bucket
    aws s3api get-bucket-location --bucket pw-blog-bucket-101 --output text

    And finally, to finish this off I’m going to:
    – Create a folder (known as Object) within the new Bucket.
    – List items in the S3 Bucket.
    – Copy a file from my desktop into the folder.
    – List items in the S3 Bucket.
    – Delete the Bucket.

    # Create folder/object within a S3 Bucket
    aws s3api put-object --bucket pw-blog-bucket-101 --key folder1/
    
    # Show objects within S3 Bucket
    aws s3 ls s3://pw-blog-bucket-101 --recursive --human-readable
    
    # Copy a local file into the folder above
    aws s3 cp .\Upload2S3.txt s3://pw-blog-bucket-101/folder1
    
    # Show objects within S3 Bucket
    aws s3 ls s3://pw-blog-bucket-101 --recursive --human-readable
    
    # Delete the S3 Bucket
    aws s3 rb s3://pw-blog-bucket-101
    
    # List the S3 Bucket above (expect error)
    aws s3 ls s3://pw-blog-bucket-101