Tips running Linux under WSL2

Here are some tips for using Windows Subsystem for Linux.

LUKS

Mount encrypted drives

To use LUKS encryption under Ubuntu (see http://phwl.org/2020/luks-encryption/). One can list and mount drives from Administrator Powershell (control-shift and open Powershell) as follows:

C:> GET-CimInstance -query "SELECT * from Win32_DiskDrive"
C:> wsl --mount \\.\PHYSICALDRIVE1 --bare

Start up wsl`` and you should see the disk withlsblk. Note the drive (it is /dev/sdd1 below) and create a file (mountback2.sh```) with the commands below.

cryptsetup luksOpen /dev/sdd1 back2
mkdir /mnt/wsl/back2
mount /dev/mapper/back2 /mnt/wsl/back2

Then ssh mountback2.sh should mount the encrypted drive.

Create encrypted drive

(base) phwl@bream:~$ lsblk
NAME          MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
loop0           7:0    0   219M  1 loop  /snap/gnome-3-34-1804/72
loop1           7:1    0 140.7M  1 loop  /snap/gnome-3-26-1604/104
loop2           7:2    0   2.5M  1 loop  /snap/gnome-system-monitor/169
loop3           7:3    0 140.7M  1 loop  /snap/gnome-3-26-1604/102
loop4           7:4    0  61.9M  1 loop  /snap/core20/1361
loop5           7:5    0 247.9M  1 loop  /snap/gnome-3-38-2004/87
loop6           7:6    0   219M  1 loop  /snap/gnome-3-34-1804/77
loop7           7:7    0  65.1M  1 loop  /snap/gtk-common-themes/1515
loop8           7:8    0  61.9M  1 loop  /snap/core20/1376
loop9           7:9    0   2.5M  1 loop  /snap/gnome-system-monitor/174
loop10          7:10   0  55.5M  1 loop  /snap/core18/2284
loop11          7:11   0   2.6M  1 loop  /snap/gnome-calculator/920
loop12          7:12   0   548K  1 loop  /snap/gnome-logs/106
loop14          7:14   0 248.8M  1 loop  /snap/gnome-3-38-2004/99
loop15          7:15   0   2.5M  1 loop  /snap/gnome-calculator/884
loop16          7:16   0   704K  1 loop  /snap/gnome-characters/761
loop18          7:18   0   704K  1 loop  /snap/gnome-characters/741
loop19          7:19   0     4K  1 loop  /snap/bare/5
loop20          7:20   0  65.2M  1 loop  /snap/gtk-common-themes/1519
loop21          7:21   0  55.5M  1 loop  /snap/core18/2253
loop22          7:22   0   548K  1 loop  /snap/gnome-logs/103
loop23          7:23   0 110.8M  1 loop  /snap/core/12725
sda             8:0    0   7.3T  0 disk  
└─sda1          8:1    0   7.3T  0 part  
  └─breamdisk 253:0    0   7.3T  0 crypt /srv/breamdisk
sdb             8:16   0   3.7T  0 disk  
├─sdb1          8:17   0   200M  0 part  
└─sdb2          8:18   0   3.7T  0 part  
nvme0n1       259:0    0 931.5G  0 disk  
└─nvme0n1p1   259:1    0 931.5G  0 part  /
(base) phwl@bream:~$ sudo fdisk /dev/sdb

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (4294967294 != 7814037166) will be corrected by w(rite).
GPT PMBR size mismatch (4294967294 != 7814037166) will be corrected by w(rite).

Command (m for help): d
Partition number (1,2, default 2): 

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-7814037133, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-7814037133, default 7814037133): 

Created a new partition 1 of type 'Linux filesystem' and of size 3.7 TiB.

Command (m for help): w
GPT PMBR size mismatch (4294967294 != 7814037166) will be corrected by w(rite).

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
(base) phwl@bream:~$ sudo cryptsetup luksFormat /dev/sdb1
[sudo] password for phwl: 

WARNING!
========
This will overwrite data on /dev/sdb1 irrevocably.

Are you sure? (Type uppercase yes): YES
Enter passphrase for /dev/sdb1: 
Verify passphrase: 

Then format the new encrypted drive.

(base) phwl@bream:~$ sudo cryptsetup luksOpen /dev/sdb1 back3
Enter passphrase for /dev/sdb1: 
(base) phwl@bream:~$ sudo mkfs.ext4 /dev/mapper/back3
mke2fs 1.44.1 (24-Mar-2018)
Creating filesystem with 976753873 4k blocks and 244195328 inodes
Filesystem UUID: ea124cd2-7978-4467-a4b8-cbb3cb17881c
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
	102400000, 214990848, 512000000, 550731776, 644972544

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done    

and mount it:

(base) phwl@bream:~$ sudo mkdir /srv/back3
(base) phwl@bream:~$ sudo mount /dev/mapper/back3 /srv/back3

ssh access

Follow these instructions: https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse.

Also do the following to change the login shell from powershell to bash.

# Set login shell to WSL2 bash
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\WINDOWS\System32\bash.exe" -PropertyType String -Force

In powershell, figure out your login name:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

You should now be able to ssh NAME\user@localhost from powershell. From a zsh or bash the backslash needs to be escaped:

ssh GARFISH\\[email protected]