Category: Linux

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

  • How to Install & Configure AWS CLI on Ubuntu

    How to Install & Configure AWS CLI on Ubuntu

    This post should help guide you through the process of installing and configuring AWS CLI (version 1) on Ubuntu (20.04).

    The following areas are covered in this demo:
    Updating Local Packages
    Installing AWS CLI on Ubuntu
    Checking Installed AWS CLI Version
    Configuring AWS Profile
    Running AWS CLI Commands

    Updating Local Packages

    To start, update your local packages by running the following command:

    # update local packages ubuntu
    sudo apt-get update
    

    Installing AWS CLI on Ubuntu

    You can install AWS CLI on Ubuntu by running the following command:

    # install awscli ubuntu
    sudo apt-get install awscli
    

    Checking Installed AWS CLI Version

    After the installation is complete, check the version of AWS CLI that was installed by running:

    # check version of awscli installed
    aws --version

    Configuring Your AWS Profile

    Once you have confirmed the version, you can run aws configure to set up your AWS profile. Your Access Key ID and Secret Access Key can be found and recreated in the AWS IAM Console, which I have covered in my previous post on installing and configuring AWS CLI on Windows.

    # configure aws profile
    aws configure

    For reference, here are the output config files – this is an area that gets touched a lot.

    Running AWS CLI Commands

    Once you have completed the configuration, you can run AWS CLI commands like the following example, which lists all the buckets in your AWS account that begin with the pw-* prefix:

    Note that this is not the only way to install AWS CLI on Ubuntu, and this guide covers version 1 of AWS CLI.

  • 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.

  • 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.

  • Create a Link Between Local Windows Files and WSL

    Create a Link Between Local Windows Files and WSL

    The ln command in Linux is used to create a link between files. This is useful for sharing files between your local machine and WSL.

    I’m linking to a ‘projects’ folder in this post but I get an error when cloning git repos in them (chmod error). Using mounted Windows drives for version-controlled projects is appearing to be a no-go.

    # Create a New Linked Folder
    # Remove a Linked Folder

    Create a New Linked Folder

    # Create sym link wsl
    ln -s /mnt/c/projects

    -s Create symbolic links instead of hard links.

    First I’m creating a local folder and then creating a link to the folder in my WSL home directory.

    PowerShell New-Item

    Next, I’m creating a new folder and file within WSL on the linked folder.

    Create Folder and File Linux

    And here’s the file created above shown within the local Windows environment.

    Folder and File in Windows Explorer

    Remove a Linked Folder

    List the files (-lai) to check for existing links ad then remove as your would a file (rm command).

    Remove Symlink

    We’re deleting the object visible to Linux here, not the folder in Windows.

  • Access Local Files from Windows Subsystem for Linux (WSL)

    Access Local Files from Windows Subsystem for Linux (WSL)

    Accessing local Windows files from a WSL distro can be achieved by navigating to the /mnt/c/ directory in your WSL Linux machine, or you can enter WSL from any Windows directory within a terminal.

    This post is to demo both methods described for accessing Windows files from WSL:
    # Navigate with cd /mnt/c/
    # Open WSL from PowerShell Terminal

    You can also create a symbolic link to create a new linked folder between your Windows computer and WSL. Check out my other blog post Create a Link Between Local Windows Files and WSL for more information.

    Navigate in Terminal to /mnt/c/

    When working within a WSL Linux environment, we can look at the files on our local Windows machine by navigating to the /mnt/ directory as shown in the example below.

    # navigate to home (wsl) linux folder
    cd ~
    
    # navigate c:\temp folder on local windows computer
    cd /mnt/c/temp
    WSL access Windows Files

    In the Windows Terminal above we are checking the contents of /mnt/ which is the local C & D volumes, and a folder named WSL.

    Open WSL from PowerShell Terminal

    When working within a PowerShell Terminal, we can open WSL from any Windows directory and the default Linux distribution will be opened with that local Windows-mounted path.

    In the example below we’re creating a new folder on Windows within a PowerShell Terminal, navigating into the new folder and then entering WSL.

    PowerShell Terminal Open WSL

    The PowerShell colours are difficult to see on that path, but it shows the new folder we created within Ubuntu WSL.

  • Reinstalling a WSL Disto

    Reinstalling a WSL Disto

    This post is a guide on how to reinstall WSL (Windows Subsystem for Linux) via command. This may be useful if you are looking to clear down and start a up fresh WSL instance, treating it as a throw-away environment. You should have any WIP code backed up to Git before running this.

    To remove a WSL distribution and its data, run wsl --unregister <distro-name>, amend the statement with the name of the distribution, which you can see by running wsl -l.

    You can also uninstall each of your WSL Linux distro apps on your machine via the Microsoft Store. This blog post shows examples of the command-line way.

    The following is included here:
    # List Installed WSL Distros
    # Uninstall a WSL Distro
    # Install Ubuntu on WSL

    List Installed WSL Distros

    We need to know the WSL distribution name which we want to remove. Run the following to show the currently installed Linux Distributions on your system:

    wsl -l
    

    Unregister/Uninstall a WSL Distro

    Caution: Once unregistered, all data, settings, and software associated with that distribution will be permanently lost. Reinstalling from the store will install a clean copy of the distribution. (MS Docs).

    The following command will uninstall a WSL Distro:

    wsl --unregister <distro-name>
    

    Install Ubuntu WSL

    My previous blog post covers installing Ubuntu 20.04 on WSL. It includes how to install Ubuntu via command or the Microsoft Store.

    We run the .exe file when doing this via command, entering the username/password details when prompted.