天天看點

【機器學習 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

當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!

繼續閱讀