LoginSignup
5
7

More than 5 years have passed since last update.

MacOSXからRaspberry Piにパスワード無しで接続する

Last updated at Posted at 2015-08-13

MacOSXからRaspberry Piにパスワード無しで接続する

Raspbianの初回起動時にやったことの続き

MacOSX側(Terminal)での作業

鍵を生成する

ssh-keygen -t rsa

result
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/admin/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/admin/.ssh/id_rsa.
Your public key has been saved in /Users/admin/.ssh/id_rsa.pub.
The key fingerprint is:
--:--:--:--:--:--:--:--:--:--:--:--:--:--:--:-- admin@MACBOOKPRO.local
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
|                 |
+-----------------+

生成されたか確認する

ls ~/.ssh

result
id_rsa      id_rsa.pub  known_hosts
  • id_rsa:生成された秘密鍵
  • id_rsa.pub:生成された公開鍵

Raspberry Piに公開鍵を転送する

scp .ssh/id_rsa.pub pi@raspberrypi.local:

result
pi@raspberrypi.local's password: 
id_rsa.pub                                                                                                                                 100%  400     0.4KB/s   00:00    

Raspberry Piでの作業

.sshディレクトリの有無を確認する

ls ~/.ssh

もし.sshディレクトリが無ければ作成する
mkdir ~/.ssh
chmod 700 ~/.ssh

MacOSXから転送した公開鍵を認証鍵として移動する

mv ~/id_rsa.pub ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys

※複数の認証鍵を登録するときは
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
として、authorized_keysに追記していく

公開鍵認証方式が使えるように設定を変更する

sudo nano /etc/ssh/sshd_config

/etc/ssh/sshd_config
[Before]
PermitRootLogin yes
#AuthorizedKeysFile     %h/.ssh/authorized_keys

[After]
PermitRootLogin no
AuthorizedKeysFile     %h/.ssh/authorized_keys
  • PermitRootLogin no:rootのログインを禁止(直接関係ないがついでに設定しておく)
  • AuthorizedKeysFile %h/.ssh/authorized_keys:認証鍵の置き場所

今回は

  • RSAAuthentication yes
  • PubkeyAuthentication yes

がデフォルトで有効になっていた(コメントアウトされていなかった)が、もしコメントアウトされていたらあわせて有効にする

sshサーバを再起動して、設定を反映する

sudo /etc/init.d/ssh restart

新規に接続してみる

ssh pi@raspberrypi.local
パスワードが聞かれずにログインできれば完了

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