LoginSignup
3
2

More than 5 years have passed since last update.

公開鍵暗号方式によるSSH接続

Last updated at Posted at 2016-07-17

環境

Mac OSX El Capitan
Raspberry Pi2 raspbian

RSA暗号鍵の生成

サーバ側(ラズパイ)、クライアント側(Mac)両方で以下のコマンドを入力し、RSA暗号鍵を生成する
$ ssh-keygen -t rsa
いろいろ入力を促されるが、全て何も入力せずにEnterをだけで先に進めればOK

クライアント側の公開鍵をサーバに転送

クライアント側でscpコマンドを使ってサーバに公開鍵を転送する
$ scp -P 22 ~/.ssh/id_rsa.pub user@ipアドレス:~/.ssh/authorized_keys

他のクライアントPCからも公開鍵暗号方式でSSH接続したい場合は、authorized_keysの最後に公開鍵を追加していけば良い。

パーミッションの変更

クライアント側
$ chmod 700 ~/.ssh

サーバ側
$ chmod 600 ~/.ssh/authorized_keys

パスワードによるログインを禁止にする

サーバ側のsshの設定を変更する
$ sudo vi /etc/ssh/sshd_config

#PasswordAuthentication yes

PasswordAuthentication no

その後、sshをrestartすれば設定が反映される
$ sudo /etc/init.d/ssh restart

config設定

SSH接続のたびに $ ssh user@ipアドレス を入力するのは手間がかかる
クライアント側の ~/.ssh/config を編集(無ければ作成)する

Host raspi2
  HostName     192.168.76.73
  User         pi
  Port         22
  IdentityFile ~/.ssh/id_rsa

例えば上のように設定しておくと、次回から
$ ssh raspi2
と入力するだけでsshによる接続ができるようになる。

3
2
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
3
2