菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
96
0

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

原创
05/13 14:22
阅读数 64233

问题描述

在平常的工作习惯中,如果使用VS Code做脚本的开发,是一个非常好用的工具,现在也可以通过VS Code的不同方式来连接到Linux VM中(ssh), 第一种是VS Code的Terminal中通过powershell窗口使用ssh命令登录。另外一种就是通过插件remote-ssh登录并管理Linux VM机器(非常方便的小工具,也是本文采用的方式)。 当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 

  • 公钥文件在当前命令窗口的文件夹下,需要通过cat来查看公钥的内容,并把内容复制到创建VM的门户上。
  • cat ~/testssh01.pub

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

ssh -i ~/testssh01 azureuser@10.111.12.123

(登录成功)

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

 (详细的如何使用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中输入命令:

 

参考资料

使用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 azureuser@10.111.12.123 来登录VM。https://docs.microsoft.com/zh-cn/azure/virtual-machines/linux/ssh-from-windows#connect-to-your-vm

 

发表评论

0/200
96 点赞
0 评论
收藏