10

I am fairly new to this business and I fail to understand how to SSH from my win10 machine into my installed wsl2 ubuntu 20.4

Basically, I followed this tutorial, But I keep getting the following errors:

  • when I try to SSH using the public port (using curl ifconfig.me) gives me the error "connection timed out"

  • when I try to SSH using the private port (using ip route get 1.2.3.4 | awk '{print $7}') it gives me the error "Permission denied"

at some point I got the error "sshd: no hostkeys available -- exiting" so I followed this fix but then I got the errors mentioned before. Should I delete any from the /etc/ssh folder?

The end-goal is ssh'ing through vs-code, but I guess once I could do it from powershell, it's the same from vs-code.

Killerz0ne
  • 254
  • 1
  • 2
  • 12

2 Answers2

7

It appears that you need to enter /etc/ssh/sshd_config (with sudo permissions) and change the following lines:

  • ChallengeResponseAuthentication yes
  • PasswordAuthentication yes
Killerz0ne
  • 254
  • 1
  • 2
  • 12
4

Since you seem to have fixed your issue with ssh, let me propose that your ultimate goal ("ssh into WSL from VSCode) might be better accomplished using Microsoft's "Remote Development" extension pack, which includes several extensions. While it sounds like you are considering using the "Remote - SSH" extension, you can also use the "Remote - WSL" extension directly.

After installing either the extension pack or the WSL extension directly, just open your WSL instance, cd to the directory with your code and then code . (including the period). This will open VSCode and install a shim into the WSL instance which will allow communication between the two.

See the docs from Microsoft for more detail.

Also, on the topic of your original question, you said that you edited sshd_config to permit password authentication (I don't think the ChallengeResponseAuthentication change was necessary). That's one way to go, but ultimately I'd recommend generating an SSH key pair, copying the private key to something like C:\Users\yourid\.ssh\id_rsa and using that instead of a password login.

And you mentioned in your original question that you were unable to access SSH on the public port. This is because WSL2 does not do NAT, so it also won't be accessible from a second computer without (a lot of) additional effort (manual port-forwarding from Windows to WSL, which will have to be reset on reboot since the WSL interface address will change).

As you've discovered, the WSL interface address will work, but remember that it will change on each reboot of Windows (technically, I think, any time the WSL subsystem is shut down and restarted). IMHO, you're better off using 127.0.0.1 or localhost.

But really, my preferred method of accessing WSL remotely is to install OpenSSH on Windows 10, port 22. Then you can simply do something like ssh -t windowsusername@mycomputername.local wsl to get access to the WSL instance. You can even do this when you have multiple WSL instances on your machine with ssh -t windowsusername@mycomputername.local wsl -d WSLInstanceName.

If you use this technique, of course, and you still want to run an SSH server in a WSL instance, you'll need to use a different port. But I really think you should do this anyways when running SSH under WSL. Otherwise, you are likely to spin up a second WSL instance at some point and run into port conflicts anyway.

The downside is that the Windows OpenSSH -> WSL hack won't allow you to run things like VSCode through SSH, but it does provide super-simple access to WSL through SSH, and works remotely (if you ever need that) as well.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70
  • thanks for the answer, is there a difference between ssh-wsl ans remote-ssh? – Killerz0ne Dec 10 '20 at 14:44
  • As far as the extensions go, there's "Remote - WSL" and "Remote - SSH". The WSL specific version shouldn't require SSH at all, nor does it need any extra configuration. It also can do some extra "magic" like enable inotify support for things like nodemon (and others), which isn't easily done on WSL2 (yet) without it. For those reasons, I would prefer it for this use case. You'd want to use "Remote - SSH" when accessing source on a remote computer, perhaps in a VM or in the Cloud (e.g. an EC2 instance).. – NotTheDr01ds Dec 10 '20 at 16:11