天天看点

【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件

问题描述

在平常的工作习惯中,如果使用VS Code做脚本的开发,是一个非常好用的工具,现在也可以通过VS Code的不同方式来连接到Linux VM中(ssh), 第一种是VS Code的Terminal中通过powershell窗口使用ssh命令登录。另外一种就是通过插件remote-ssh登录并管理Linux VM机器(非常方便的小工具,也是本文采用的方式)。 当SSH连接成功后,可以直接在VS Code中直接打开VM中的文件夹,运行脚本及编辑文件。非常方便。

【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件

实现步骤

1)通过使用SSH方式创建VM,以Azure VM为例,在创建过程中注意两点

  • 使用ssh-keygen -m PEM -t rsa -b 4096 生成公钥,私钥。
  • ssh-keygen -m PEM -t rsa -b 4096       
    【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
  • 公钥文件在当前命令窗口的文件夹下,需要通过cat来查看公钥的内容,并把内容复制到创建VM的门户上。
  • cat ~/testssh01.pub      
    【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件

2)创建VM成功后,可以通过PowerShell验证是否能已经能正常连接,使用ssh命令 (-i 后面的参数为私钥存储文件)

ssh -i ~/testssh01 [email protected]      
【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件

(登录成功)

3) 在VS Code中先安装Remote-SSH插件。成功后可以通过Remote-SSH图表,添加新的Host config文件,在文件中输入Host (自定义名称),Linux Host name IP 地址,username ,IdentityFile为您SSH的私钥文件路径。

【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件

 (详细的如何使用Remote-SSH,可以参考官方文档:https://code.visualstudio.com/docs/remote/ssh-tutorial#_install-the-extension)

Choose the Remote-SSH: Connect to Host command and connect to the host by entering connection information for your VM in the following format: 

user@hostname

.

The 

user

 is the username you set when adding the SSH public key to your VM. For the 

hostname

, go back to the Azure portal and in the Overview pane of the VM you created, copy the Public IP address.

4) 当完成以上配置后,就可以方便的连接到Linux VM中并直接在VS Code的Terminal中输入命令:

【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件

参考资料

使用ssh-keygen -m PEM -t rsa -b 4096 来插件SSH公钥和私钥。  https://docs.microsoft.com/zh-cn/azure/virtual-machines/linux/ssh-from-windows#create-an-ssh-key-pair

使用cat ~/.ssh/id_rsa.pub 来查看公钥信息并复制到创建VM时的门户上。https://docs.microsoft.com/zh-cn/azure/virtual-machines/linux/create-ssh-keys-detailed#provide-ssh-public-key-when-deploying-a-vm

使用ssh -i ~/.ssh/id_rsa.pub [email protected] 来登录VM。https://docs.microsoft.com/zh-cn/azure/virtual-machines/linux/ssh-from-windows#connect-to-your-vm

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

继续阅读