Tuesday, June 27, 2023

Windows Subsystem for Linux (WSL)


Here are the installation Steps for Windows Subsystem for Linux (WSL).

## 1. Enable the Windows Subsystem for Linux

### 1.1 You should  first enable the "Windows Subsystem for Linux" optional feature before installing any Linux distributions on Windows.

> Open Powershell in Adminstrative mode and execute below command to enable WSL feature 

```sh
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

```

### 1.2  To update to WSL 2 , Machine should have window 10 or Windows 11 

> For x64 systems: Version 1903 or later, with Build 18362.1049 or later.
> For ARM64 systems: Version 2004 or later, with Build 19041 or later.

### 1.3 Enable Virtual Machine Feature 

> Open Powershell in Adminstrative mode and execute below command to enable virtual machine feature.

```sh
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

```
### 1.4 Download the Linux kernel update package

Download the latest package : [WSL2 Linux kernel update package for x64 machines](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)

Once downloaded Linux kernal update package from above link then double click to run with elevated permissions.

### 1.5  Set WSL 2 as a default version 

Open PowerShell and run this command to set WSL 2 as the default version when installing a new Linux distribution

```sh
wsl --set-default-version 2

```

## 2. Installation steps for WSL (Windows Subsystem for Linux) Terminal

### 2.1. We can install WSL Terminal using chocolatey Package manager. if chocolatey Package manager CLI is not installed in your machine . 

> Please execute below powershell commands in Powershell ISE (Adminstrative mode)

```sh 
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'))

```

### 2.2. Need to install dependecy package( Microsoft.VCLibs.x64.14.00.Desktop.appx) 

> Execute below powershell commands in Powershell ISE (Adminstrative mode) to install dependecy package( Microsoft.VCLibs.x64.14.00.Desktop.appx) 

```sh
    $packageURL=https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx  

    ## https://aka.ms/Microsoft.VCLibs.x86.14.00.Desktop.appx URL for X86 version of VCLibs.x86.14.00.Desktop package.

    $pageName= Split-Path -Leaf $packageURL

    $packagePath = "$env:TEMP\$pageName"

    (New-Object System.Net.WebClient).DownloadFile($packageURL, $packagePath)

    Add-AppxPackage $packagePath

    Remove-Item $packagePath
```
### 2.3. Now We can install  WSL (Windows Subsystem for Linux) Terminal using below commands with the help of  choco package manager CLI 

> Execute below powershell commands in Powershell ISE (Adminstrative mode) to install - WSL Terminal.
```sh
    
    choco install -y microsoft-windows-terminal --version 1.12.10732.0 -force
    
    choco upgrade microsoft-windows-terminal -y
```

> 2.4. Reboot machine 

> 2.5. serach for Terminial 




## 3. Installation steps for install Ubuntu os on WSL in Windows Machine.

we can see list of WSL Distributions already installed in your machine or Open the Microsoft Store and select your favorite Linux distribution.

```sh
	wsl --list

	Output :
		Windows Subsystem for Linux Distributions:
		Ubuntu (Default)

```

If you are allowed to install WSL distributions from Microsoft store  then we can see list of available WSL distributions list and install linux distribution using "wsl --install -d "

```sh 
	wsl --list --online

	Output:

	The following is a list of valid distributions that can be installed using 'wsl --install -d '.

	NAME                                   FRIENDLY NAME
	Ubuntu                                 Ubuntu
	Debian                                 Debian GNU/Linux
	kali-linux                             Kali Linux Rolling
	Ubuntu-18.04                           Ubuntu 18.04 LTS
	Ubuntu-20.04                           Ubuntu 20.04 LTS
	Ubuntu-22.04                           Ubuntu 22.04 LTS
	OracleLinux_7_9                        Oracle Linux 7.9
	OracleLinux_8_7                        Oracle Linux 8.7
	OracleLinux_9_1                        Oracle Linux 9.1
	SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
	openSUSE-Leap-15.4                     openSUSE Leap 15.4


```
> Example wsl --install -d Ubuntu

However, Most of the organization will not provide access to Microsoft store so We can install WSL ubuntu distribution using AppxPackage.

> 3.1  if you want to install Ubuntu distribution using AppxPackage , Execute below powershell command in powershell-ISE (in Admintrative mode)

```sh
	Invoke-WebRequest -Uri https://aka.ms/wslubuntu2004 -OutFile Ubuntu.appx -UseBasicParsing

	Add-AppxPackage .\Ubuntu.appx

	$userenv = [System.Environment]::GetEnvironmentVariable("Path", "User")


   [System.Environment]::SetEnvironmentVariable("PATH", $userenv + "; C:\Users\Administrator\Ubuntu", "User")
   
 #Note : If you are using Windows server, or run into problems running the command above you can find the alternate install instructions on the Windows Server documentation page to install the .appx file by changing it to a zip file.
 
```

|Distribution links  |
|----------------------|
|[Ubuntu](https://aka.ms/wslubuntu)|
|[Ubuntu 22.04 LTS](https://aka.ms/wslubuntu2204)|
| [Ubuntu 20.04](https://aka.ms/wslubuntu2004|
|[Ubuntu 20.04 ARM](https://aka.ms/wslubuntu2004arm)|
|[Ubuntu 18.04](https://aka.ms/wsl-ubuntu-1804)|
|[Ubuntu 18.04 ARM](https://aka.ms/wsl-ubuntu-1804-arm)|
|[Ubuntu 16.04](https://aka.ms/wsl-ubuntu-1604)|
|[Debian GNU/Linux](https://aka.ms/wsl-debian-gnulinux)|
|[Kali Linux](https://aka.ms/wsl-kali-linux-new)|
|[Kali Linux](https://aka.ms/wsl-kali-linux-new)|
|[SUSE Linux Enterprise Server 12](https://aka.ms/wsl-sles-12)|
|[SUSE Linux Enterprise Server 15 SP2](https://aka.ms/wsl-SUSELinuxEnterpriseServer15SP2)|
|[SUSE Linux Enterprise Server 15 SP3](https://aka.ms/wsl-SUSELinuxEnterpriseServer15SP3)|
|[openSUSE Tumbleweed](https://aka.ms/wsl-opensuse-tumbleweed)|
|[openSUSE Leap 15.3](https://aka.ms/wsl-opensuseleap15-3)|
|[openSUSE Leap 15.2](https://aka.ms/wsl-opensuseleap15-2)|
|[Oracle Linux 8.5](https://aka.ms/wsl-oraclelinux-8-5)|
|[Oracle Linux 7.9](https://aka.ms/wsl-oraclelinux-7-9)|
|[Fedora Remix for WSL](https://github.com/WhitewaterFoundry/WSLFedoraRemix/releases/)||

> 3.2. Reboot machine and seach for ubuntu app



> 3.3  Connect to Ubuntu Linux distribution from WSL terminial 
	Open  WSL and click on (+) new tab select Ubuntu linux distribution 




>  3.4 Reset password for user /Forgot password

  To change password for user in Linux distribution (WSL) . We can type "passwd" then it will prompt for current password , new password ,Re-enter Password 
 
 
 > 3.5 connect to ubuntu with root user
 
```sh 
     ssh <user>@<HostName>
    
```
 
 > 3.6  update/upgrade package
 
```sh
   apt list --upgradable 
    
    sudo apt  update 
    
    sudo apt  upgrade
```
 
 
 
 
  

Merge multiple files

### Following code snippet is used to merge multiple fiels 


```sh
$Logpaths=("D:\Logs\App1\*.txt","D:\Logs\*.log","D:\MsgLog\*.log","D:\ErrorLog\*.log")

Get-Content $Logpaths |Set-content "D:\Logs\merge_log.txt"
```