Killerz0ne
Killerz0ne

Reputation: 344

SSH'ing from windows 10 into wsl2 ubuntu

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:

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.

Upvotes: 9

Views: 21409

Answers (2)

NotTheDr01ds
NotTheDr01ds

Reputation: 20630

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 [email protected] 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 [email protected] 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.

Upvotes: 5

Killerz0ne
Killerz0ne

Reputation: 344

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

  • ChallengeResponseAuthentication yes
  • PasswordAuthentication yes

Upvotes: 7

Related Questions