如何通过 SSH 连接到 Windows 子系统 Linux 上的 Microsoft Azure 虚拟机?

How to connect to Microsoft Azure virtual machine on Windows Subsystem for Linux through SSH?

我用 Windows 10 作为映像创建了一个 Microsoft Azure 虚拟机。我允许它有 SSH 连接。 Microsoft Azure 门户上的连接显示 "Network connectivity allowed"。但是,当我尝试使用命令 ssh -i <private key path> <vm-name>@<host ip address> 在 Windows 子系统上为 Linux 连接它时,它显示 ssh: connect to host <host ip address> port 22: Resource temporarily unavailable。为什么会这样?

我也想知道 Microsoft Azure 上有关创建私钥然后拥有私钥路径的说明。我应该如何创建私钥并为其设置路径?

您可以查看 microsoft/WSL/issue 2278,其中列出了几个问题:

  • 防火墙
    • the domain had been moved to cloud front and I needed to use the direct IP address instead of the domain name.
    • I received a "Resource temporarily unavailable" error because my subnet did not have a route to an Internet Gateway
    • I just had this problem because I installed Nginx in the server and it blocks SSH by default. You have to sudo ufw allow ssh.

注意:如“Connect to an Azure Linux vm with ssh and Windows Subsystem for Linux" by matthewdavis111 中所述,您从 WSL 连接到 Azure。

意味着您将在 Azure 远程服务器的 SSH 字段中输入 public 密钥:

在您的 PC 上,您可以生成 public/private 密钥:

ssh-keygen -P "" -t rsa -f azure

这将在 %USERPROFILE%\.ssh 中创建一个 azureazure.pub 文件。
将第二个(public 键)的内容复制到 Azure 设置。

根据您的 Windows 10 版本,您可能需要安装 Git for Widows,其中包括 ssh-keygen.exe.

C:\path\to\Git\usr\bin\ssh-keygen.exe

但在您的情况下,在 WSL bash 会话中这样做更好,特别是如果其余部分是在同一会话中完成的。
这些文件将位于 ~/.ssh ($HOME/.ssh)


对于 Windows 10 VM,请参阅“Creating A Win 10 Virtual Machine – Azure Portal" by Ayush Rathi
那就不需要SSH了。

Connecting to the Virtual Machine and Log On:

  1. On the Hub menu, click Virtual Machines.

  2. Select the virtual machine from the list.

  1. On the Virtual Machine Blade, Click on Connect. This creates and downloads a Remote Desktop Protocol file (.rdp file) to connect to your machine. Open this file to connect to your VM.

  2. You will get a warning that the .rdp is from an unknown publisher.This is a normal warning. In the Remote Desktop window, click Connect to continue.

  1. In the Windows Security window, type the username and password that you entered while creating the VM. The username is entered as vmname\username, then click OK.

  1. You will get a warning that the identity of the remote computer cannot be verified. This is a normal warning. Click Yes to verify the identity of the virtual machine and finish logging on.

Your machine is ready and you can use it just as you would use any other machine or server.