wsl2 – Adding ssh key in WSL permanently

I am relatively new to Linux. Using Ubuntu on WSL2. I added a private key for github with no passphrase using ssh-add. I used it a few times throughout the session but the next day I couldn’t connect. When I list all keys using ssh-add -l there aren’t any.

Am I supposed to add the key for every session or create an automation for it? Or is this a normal (lack of) persistence behaviour with WSL?

windows subsystem for linux – Install under WSL2 with default non-root username BESIDES value of %USERNAME%

Years ago, I started using ‘root’ as my Windows username. Yeah, I know… in retrospect, it wasn’t a great idea, but it seemed cute and harmless back when the closest Windows and Linux ever really got to each other was booting from independent primary partitions or running as VirtualBox guests of each other.

Anyway… I enabled WSL2, installed Ubuntu 20.04 from the Microsoft store, then discovered that the resulting virtual installation apparently picked up ‘root’ from %USERNAME% and made a mess.

I’m not averse to just blowing it away and starting over with the Ubuntu installation, but I’m scratching my head trying to figure out how to keep the installer from doing the exact same thing the next time around. I’m sure there HAS to be a way to set an environment variable or something that tells the installer to use something besides the current value of %USERNAME%… but I’m not sure what it is. Does anybody know?

powershell – Export/import compressed WSL2 distros

I’m looking for a way to compress WSL2 distros while exporting it. For that purpose I’ve tried to pass the tar file produced be wsl to 7z through pipe:

wsl --export myDistro - | 7z a -tgzip myDistro.tar.gz -si

I waited about an hour but it produced nothing but cpu load. 7z process wasn’t even started according to Task Manager.

I tried to pass a sample tar file though pipe to 7z:

Get-Content -Path sample.tar -Raw | 7z a -tgzip sample.tar.gz -si

And sample.tar.gz was created without any issue.

I also tried to export the distro to file instead of stdout:

wsl --export myDistro .myDistro.tar

It took about 15 minutes and the distro was exported successfully.

What am I doing wrong? How to compress an exported WSL2 distro to a gzip archive without storing the tar file on a disk? And how to import it through pipe with wsl --import after that?

drivers – Nvidia on WSL2 still not working

I am trying to get my GPU recognized by WSL2. I have followed this guide, looked at the reply to this post, ran through multiple other tutorials and I still cannot get my GPU connected. When I run the following command I get:

nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

even though I installed all the drivers given in the tutorial. I also have Windows Insider on Dev, installed all updates. Moreover, I am new to working with WSL which complicates things even further. Any help would be greatly appreciated.

wsl2 – Windows shortcut to map disk with sshfs

I don’t want to permanently map remote disks from production systems, I want to click on shortcut to map the disk and than click another shortcut to unmap it. The sshfs works fine when used from Bash. However when I run the command from shortcut with wsl.exe, it maps the disk but as soon as the command ends, mapping is removed. This can be replicated by running wsl.exe from PS:

PS C:Usersuser> wsl.exe --user root -- sshfs -o allow_other,default_permissions,ro user@machine:/path /mnt/path `&`& read -p "Press enter to continue"

After the sshfs is executed, I can see that the disk is mapped but when I press enter, the disk is unmapped. It seems that there is some kind of “console session” and the mapping is related to the session only, once the session ends, the mapping ends.

I have no idea what’s going on, I am missing something simple but fundamental. Thank you for any pointers. And no, using bash.exe does not fix the problem.

networking – Accessing WSL2 From Public IP Address

I have installed WSL2 with Ubuntu 20.04 on Windows 10.

I have an Apache server running in WSL2, and this works fine when I use a browser in Windows (Chrome) to access it via WSL IP address.

As the WSL2 IP address may change, I’ve created the following Powershell script which restarts WSL, grabs the new WSL IP address, restarts the services (Apache and MySQL for the website itself, and also Cron to run “certbot” for SSL certificate renewal), then I set up port forwarding from Windows to the WSL IP for ports 80 and 443, ensure that the Windows Firewall is open for those ports, then update the hosts file for the domain to the new WSL IP address.

Write-Host "Shutting down WSL"

wsl --shutdown

Write-Host "Starting services..."

wsl sudo service mysql restart
wsl sudo service apache2 restart
wsl sudo service cron restart

$wsl_ip = wsl hostname -I

Write-Host "Port forwarding to $wsl_ip"

netsh interface portproxy reset
netsh interface portproxy add v4tov4 listenport=80 connectport=80 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenport=443 connectport=443 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenaddress=192.168.1.165 listenport=80 connectport=80 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenaddress=192.168.1.165 listenport=443 connectport=443 connectaddress=$wsl_ip
netsh interface portproxy show all

Write-Host "Open Firewall"
Remove-NetFirewallRule -DisplayName "Apache2 Port 80 TCP"
Remove-NetFirewallRule -DisplayName "Apache2 Port 443 TCP"
New-NetFirewallRule -DisplayName "Apache2 Port 80 TCP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow -EdgeTraversalPolicy Allow
New-NetFirewallRule -DisplayName "Apache2 Port 80 TCP" -Direction Outbound -Protocol TCP -LocalPort 80 -Action Allow
New-NetFirewallRule -DisplayName "Apache2 Port 443 TCP" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow -EdgeTraversalPolicy Allow
New-NetFirewallRule -DisplayName "Apache2 Port 443 TCP" -Direction Outbound -Protocol TCP -LocalPort 443 -Action Allow

Write-Host "Updating hosts..."

$domain = "example.com"
$line = "$wsl_ip`t$domain"
$hostsPath = "$env:windirSystem32driversetchosts"
$items = Get-Content $hostsPath | Select-String $domain

if($items -eq $null)
{
    Add-Content $hostsPath $line
}
else
{
    foreach($item in $items)
    {
        (Get-Content $hostsPath) -replace $item, $line | Set-Content $hostsPath
    }
}

pause

I’ve tested the script and it does complete all the tasks correctly. The “hosts” file is updated, the firewall rules are added (this one could just be run once and needn’t be in this “restart server” script, but I’ve bundled all the steps together into this script).

The script shows all the portproxy rules and they are set up as expected (there’s not necessarily a reason for listening on all addresses and then also specifically listening on the Windows LAN IP – this is just paranonia and testing different things, when it wouldn’t work).

And the server itself is up and running, because if I browse to the WSL IP address (or use the domain name, thanks to the hosts entry – using the correct domain name matches the SSL certificate to not have to wave away browser warnings) or “localhost” then the website comes up just fine.

But If I try to browse to “127.0.0.1” or the Windows LAN IP address (192.168.1.165, as shown in the script) then I get “connection refused”.

Note that I’ve directly placed portproxy commands in the script for IP 192.168.1.165 to the WSL IP address (which works when used directly), so this portproxy is being explicitly refused (by the firewall? But I’ve added Firewall rules to open those ports, right?).

And if I try to use the public IP address (or real domain name) then the browser just spins until it says “timed out”. Which is interestingly different, as 127.0.0.1 and the LAN IP address are “connection refused” (returning immediately) but this is timing out from no response at all.

The server itself is in the DMZ and the public IP address is NAT’d to the LAN IP address, which is why I’m specifically trying to get that one working, as it should make it publicly accessible.

I did have this server up and running previously – with full public access and all was fine – but the server suffered a power outage, and now I can’t get it to work again.

It’s possible that there was some command or setting I did previously, that wasn’t saved and got lost in the power outage, but I can’t think what it could be.

Any ideas what could be making the LAN IP / 127.0.0.1 fail with “connection refused”, while “localhost” and the WSL2 IP works just fine?

Though 127.0.0.1 is less important, as it’s the LAN IP that needs to be working to get it publicly accessible, because that’s what the NAT sends packets to.

linux – Git, WSL2, SSH: unexpected disconnect while reading sideband packet

While attempting to clone a remote Git repo using SSH I got the following error:

git clone user@remote-repo:project.git .
remote: Counting objects: 187444, done.
remote: Compressing objects: 100% (73430/73430), done.
client_loop: send disconnect: Broken pipe.06 MiB | 4.04 MiB/s
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: index-pack failed

My environment has the following Specs:

  • WSL2 (Windows Subsystem 4 Linux)
  • git --version git version 2.30.1

The git package can be found here https://pkgs.alpinelinux.org/package/edge/main/x86_64/git

uname -a
Linux 4.19.128-microsoft-standard #1 SMP Tue Jun 23 12:58:10 UTC 2020 x86_64 Linux

2021-03-09-07:11AM wsl@Alpine:~$ more /etc/issue
Welcome to Alpine Linux 3.10
Kernel r on an m (l)

I’ve got no clue why is this happening. Is it a bursting or kernel configuration issue on Alpine? WSL? I doubt it’s the remote host. The remote host works fine for SSH, as well as cloning for every non-WSL user.

xorg – “Error: Can’t open display: 172.28.80.1:0” in WSL2

I’ve tried “all the things” from SO, github, etc. I’m using MobaXTerm.

In my .bashrc I have

export DISPLAY="$(/sbin/ip route | awk '/default/ { print $3 }'):0"

Which results in

(base) balter@ORLT1072346:~$ echo $DISPLAY
172.28.80.1:0

In CMD ipconfig gives

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::7442:c2c6:5b47:11ff%41
   IPv4 Address. . . . . . . . . . . : 172.28.80.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

At the top of my moba terminal it says “Your DISPLAY is set to 192.168.1.14:0.0” So I”ve tried DISPLAY=192.168.1.14:0.0, which results in the same “Can’t open display…” error.

My resolv.conf reads:

(base) balter@ORLT1072346:~$ cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.8.4

I had to do this an override the auto-generated one because I wasn’t getting internet.

Oh, and my /etc/ssh/sshd_conf has

(base) balter@ORLT1072346:~$ cat /etc/ssh/sshd_config | grep X11
X11Forwarding yes
#X11DisplayOffset 10
X11UseLocalhost yes
#       X11Forwarding no

dpkg: unrecoverable fatal error, aborting: on kali linux am using WSL2

dpkg: unrecoverable fatal error, aborting:
unknown system group ‘mlocate’ in statoverride file; the system group got removed
before the override, which is most probably a packaging bug, to recover you
can remove the override manually with dpkg-statoverride
E: Sub-process /usr/bin/dpkg returned an error code (2)