おかゆ++

IT業界の片隅で生き残るブログ

Raspberry pi Model B+ で sshの設定変更とSSHキー設定

設定ファイルでの基本的な設定

sshの設定ファイルの場所は```/etc/ssh/sshd_config

ポート変更

デフォルトの22から任意のポートに変更する。

$ sudo vim /etc/ssh/sshd_config
Port 22
↓
Port 12345

rootでのログイン禁止

#PermitRootLogin without-password
PermitRootLogin no

ログイン可能ユーザを限定する

AllowUsers [UserName]

設定変更後は、サービスを再起動する。

sudo /etc/init.d/ssh restart

SSHキーの設定

公開鍵でのみsshできるようにする。

キー設定

使用するキーを新規生成する。

例では、鍵長4096のRSA方式のキーを生成。 対話形式で、パスワードなども適宜入力していく。 特になにも変更しなかった場合は、/home/hoge/.ssh/に生成される。

$ ssh-keygen -t rsa -b 4096 -C "email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hoge/.ssh/id_rsa): 
Created directory '/home/hoge/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/hoge/.ssh/id_rsa.
Your public key has been saved in /home/hoge/.ssh/id_rsa.pub.
...

この後sshの設定を変更するが、その前に秘密鍵を別PCなりなんなりにコピーしておくこと。 ssh接続できなくなってラズパイにディスプレイとマウスをつなぐ羽目になる。

authorized_keysに追加

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

パスワードでの認証を非許可にする

/etc/ssh/sshd_configで変更する。

#PasswordAuthentication yes
PasswordAuthentication no

確認

  • 秘密鍵を使って接続できることを確認する
  • パスワードのみでは接続できないことを確認する