0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SSHでPCからRaspberryPiへデータ送受信

Posted at

PCからRaspberryPiにファイルを送りたい

2024/12/23 記

RaspberryPiとWindowsPCでデータの送受信ができると便利です

SSH接続の有効化

RaspberryPi側
applications menu > 設定 > RaspberryPiの設定 > インターフェイス > SSHにチェック

WindowsPC側
Windows10以降はデフォルトでOpenSSHクライアントがインストールのため設定不要

送信先のIPアドレスを確認

hostname -I  # raspberrypiは LXterminalにて
ipconfig  # windowspcは windows powershellにて

100.xxx.xxx.xxxというのがIPアドレス
PC側はIPv4アドレスを参照

ユーザー名の確認

whoami # raspberrypiとwindowsどちらも

windowsの場合(PC名)\(ユーザー名)とでて
raspberrypiの場合はユーザー名とでます

送信コマンド

# PCからラズパイへ送信する場合
scp (PCパスとファイル) (ラズパイユーザー名)@(ラズパイIPアドレス):(ラズパイ保存パス)
# 例えば
scp C:\Users\~\Desktop\test.py username@100.xxx.x.xx:/home/~/Desktop/
# 成功時のメッセージ
test.py                               100% 3184    50.2KB/s   00:00

パスワード入力無しで送受信するとき

公開鍵暗号方式を使います

  1. まず初めにPCで次のコマンドを実行してSSHキーを作成

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

    オプションの説明
    -t rsa: 鍵の種類をRSAに指定(RSAは暗号化や電子署名に広く使われている公開鍵暗号方式の一つで,名前は開発者の3人(Rivest、Shamir、Adleman)の頭文字から取られています)

    -b 4096: 鍵の長さを4096ビットに設定

    -C "your_email@example.com": コメントを追加(省略可)

    提示される選択肢
    保存場所の選択: デフォルト~/.ssh/id_rsaで問題なければEnterを押す

    パスフレーズ: 無記入でEnter(完全にパスワードを省略したい場合)
    これで、~/.ssh/id_rsa(秘密鍵)と~/.ssh/id_rsa.pub(公開鍵)が生成される

  2. 公開鍵をPaspberryPiに転送
    以下をPC上で実行します(IPアドレスはRaspberryPiのものに置き換えます)

    ssh-copy-id username@<RaspberryPiのIPアドレス>
    # 例えば
    ssh-copy-id username@192.xxx.x.xx
    

    動作
    このコマンドはPCの公開鍵~/.ssh/id_rsa.pub
    Raspberry Piの~/.ssh/authorized_keysに追加します
    パスワードを聞かれた場合はRaspberryPiのログインパスワードを入力します

  3. sshの接続テスト
    設定後は次のコマンドを実行してパスワードなしで接続できるか確認します

    ssh username@<RaspberryPiのIPアドレス>
    

    パスワードなしでログインできれば成功です!

トラブルシューティング的なもの

ssh-copy-idが使えない場合があります
その場合は手動で公開鍵を転送します

cat ~/.ssh/id_rsa.pub | ssh username@<RaspberryPiのIPアドレス> 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

私はこれでパスワードレスのSSH接続が使えるようになりました!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?