1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VSCodeでRaspberry pi内ファイルを編集

Last updated at Posted at 2024-08-02

 今までRaspberry piでpythonファイルをデバッグするときは、nanoエディタで直接編集するか、Windowsに送ってVSCodeで編集してRaspberry Piに送っていました。
 VSCodeでRaspberry Pi内のファイルを直接編集することができたので、ここに記しておきます。

SSHの接続設定

SSHキーの作成(Windows側)

WindowsでPowerShellを起動し、

PowerShel
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

と実行してください。
 すると、

PowerShell
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/your_username/.ssh/id_rsa):

という風にデフォルトのパスで良いか聞いてきます。
 今回はデフォルトのパスで良いので、そのままEnterを押下します。
 次に、パスフレーズの入力を2回求められるので2回打ち込んでください。

Raspberry Pi側に公開鍵を渡す

Windows側で公開鍵をコピーします。
公開鍵は、

PowerShell
type ~/.ssh/id_rsa.pub

で見ることができます。

Raspberry PiにSSHで接続します(このときはパスワード認証を使用します)。

PowerShell
ssh PiUserName@<Raspberry PiIPアドレス>

Raspberry Piのホームディレクトリに~/.sshディレクトリがない場合は作成します。

terminal
mkdir -p ~/.ssh

公開鍵をauthorized_keysファイルに追加します。

terminal
echo "<コピーした公開鍵>" >> ~/.ssh/authorized_keys

ファイルのパーミッションを設定します。

terminal
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Raspberry Piでパスワード認証を無効にする

Raspberry PiでSSH設定ファイルを編集します。

terminal
sudo nano /etc/ssh/sshd_config

次の行を追加します(コメントアウトされている箇所を編集で直しても良い)。

PasswordAuthentication no
PubkeyAuthentication yes

設定ファイルを保存して閉じます。

SSHサービスを再起動します。

terminal
sudo systemctl restart ssh

SSH接続の確認

一度SSH接続を切ります。

terminal(PowerShell内)
exit

クライアントから再度SSH接続を試みます。

PowerShell
ssh PiUserName@<Raspberry PiIPアドレス>

VSCodeでの接続

 VSCodeを立ち上げ、左上のExtensionsをクリック。
 検索ボックスにRemote SSHと入力し、Remote -SSHをinstallします。
 左下の><のようなボタンをクリック。
 上の検索バーの検索候補に出てくる
Connect to Host... Remote-SSH
をクリック。
 検索バーに

Raspberry Piのユーザ名@<Raspberry PiのIPアドレス>

を打ち込むと、新しくウインドウが立ち上がります。
 そのまま先ほど設定したSSH用のパスフレーズを入力すると接続できます。
 あとはWindowsで使っているのと変わらずに作業できます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?