YU2TA7KA's BLOG ~take one step at a time~

派生開発、組み込み開発周りのこと。

Win10からRaspberryPiへVisual Studio Code(Insiders)のRemote Developmentを使ってリモートアクセスする方法

はじめに

ラズパイにRust環境を構築しました。この環境へリモートアクセスしつつ、lldbデバッガを利用できる状態にすることを目指します*1。その1stステップとして、Visual Studio CodeのRemote Developmentを使ってリモートアクセスする方法を記載します。

動作環境

コード エディター Visual Studio Code - Insiders Insiders版である必要があります
ホスト側OS Windows 10 Home
リモートアクセス先OS Raspbian GNU/Linux 9.9 (stretch)
拡張機能1 Remote Development 0.15.0
拡張機能2 Remote - SSH(Nightly) 2019.6.39966 Nightly版である必要があります

1.SSHの公開鍵認証設定

  • Windows PowerShell(管理者)にて、SSHの公開鍵と秘密鍵を生成します。

f:id:yuji-tanaak:20190825085556j:plain
管理者としてWindows PowerShellを実行する

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\WINDOWS\system32> cd ~
PS C:\Users\yuji> mkdir .ssh
PS C:\Users\yuji> cd .\.ssh\
PS C:\Users\yuji\.ssh> ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\yuji/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\yuji/.ssh/id_rsa.
Your public key has been saved in C:\Users\yuji/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:kO6CRmwA1eWEgYd7A9u+C8gerumWGTDPO1S1+8c9mkM yuji@DESKTOP-K01AID6
The key's randomart image is:
+---[RSA 4096]----+
|...+.+o          |
|. + +o..         |
|.  * .+.         |
|ooo =...         |
|.+++ ...S        |
|.=+....   E      |
|.+B..... o .     |
|o*+...  . +.o    |
|*+ ...   .oo .   |
+----[SHA256]-----+

  • ラズパイへSSH公開鍵(id_rsa.pub)をアップロードします。

PS C:\Users\yuji\.ssh> $UserAtRemoteHost = "pi@192.168.3.3"
PS C:\Users\yuji\.ssh> $LocalSourcePublicKey = $env:UserProfile + "\.ssh\id_rsa.pub"
PS C:\Users\yuji\.ssh> $RemoteTargetPublicKey = $UserAtRemoteHost + ":~/tmp.pub"
PS C:\Users\yuji\.ssh> scp $LocalSourcePublicKey $RemoteTargetPublicKey
The authenticity of host '192.168.3.3 (192.168.3.3)' can't be established.
ECDSA key fingerprint is SHA256:YTzGrGOdxCeC9G+XAcoIqf8wWgHIDqT+h+V9zOHTWao.
Are you sure you want to continue connecting (yes/no)?
Warning: Permanently added '192.168.3.3' (ECDSA) to the list of known hosts.
pi@192.168.3.3's password:
id_rsa.pub                                                                            100%  747   163.8KB/s   00:00
PS C:\Users\yuji\.ssh> ssh $UserAtRemoteHost "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat ~/tmp.pub >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys && rm -f ~/tmp.pub"
pi@192.168.3.3's password:
PS C:\Users\yuji\.ssh> ssh -i .\id_rsa $UserAtRemoteHost
Linux raspberrypi 4.19.57-v7+ #1244 SMP Thu Jul 4 18:45:25 BST 2019 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Aug 25 06:26:59 2019 from 192.168.3.9
pi@raspberrypi:~ $ 

2.Visual Studio Code(Insiders)のインストール

通常のVisual Studio Codeではラズパイへのリモートアクセス機能が未実装のためInsiders版が必要になります*2

3.拡張機能のインストール

f:id:yuji-tanaak:20190825090516j:plain

  • Remote - SSHをアンインストールします*3

4.ssh設定ファイルの作成

f:id:yuji-tanaak:20190825092330j:plain
「F1キー」でコマンドパレットを出し、remote-sshと入力し、Connect to Hostを選択する。
f:id:yuji-tanaak:20190825092518j:plain
Configure SSH Hostsを選択する。
f:id:yuji-tanaak:20190825092549j:plain
.ssh\configを選択する。

Host pi@192.168.3.3
    HostName 192.168.3.3
    User pi
    IdentityFile ~/.ssh/id_rsa

5.リモートアクセスの確認

f:id:yuji-tanaak:20190825093307j:plain
設定したアクセス先をクリックする。
f:id:yuji-tanaak:20190825093456j:plain
新しいウィンドウが開き、Open folderでラズパイのフォルダを展開できたら接続成功。

おわりに

Visual Studio Code(Insiders)のRemote Developmentを使ってリモートアクセスできました。
この環境へlldbを入れていきたいのですが、aptでそのままインストールするとバージョンが3.8.1になります。このバージョンではrustcが生成したデバッグ情報の読み取りに問題があるため、最新版を入れる必要があります。それでlldb-8のインストールを試しているのですが、いまのところ全くうまくできません。そもそもDebian,Ubuntuまでの対応でラズパイにlldb-8は入れられない?

通常のVisual Studio Codeからラズパイへリモートアクセスを試みたときのエラー

Install and start server if needed
> 
> Running remote connection script
> armv7l is only supported when running VS Code Insiders
> fd2145bc-a2b3-407f-8410-7490b1670142##29##
> 
"install" terminal command done
Received install output: fd2145bc-a2b3-407f-8410-7490b1670142##29##
Architecture only available on VS Code Insiders
The remote server architecture is only supported on VS Code Insiders
------

f:id:yuji-tanaak:20190825094044j:plain

*1:lldb環境は実践Rust入門[言語仕様から開発手法まで]の推奨環境です。書籍ではリモートアクセスはしていないです。

*2:最初私はそれを知らなくてエラーになりました。

*3:Remote Developmentインストール時に一緒に入ってきます