玄箱でOpenSSHを公開鍵方式で運用する
OpenSSH(ssh)には、2つのバージョンが存在しそれによって使える鍵が違い、次のような鍵を使います。
- ロトコルバージョン1のRSA鍵
- ロトコルバージョン2のRSA鍵
- ロトコルバージョン2のDSA鍵
RSA公開鍵による認証
RSA公開鍵と秘密鍵を作成するには、ssh-keygenコマンドを使用します。どのタイプの鍵を生成するかは 環境によって選択します。
プロトコルバージョン1(SSH1)のRSA鍵を作成
$ ssh-keygen -t rsa1
Generating public/private rsa1 key pair.
Enter file in which to save the key (/home/tmp-kun/.ssh/identity):【Enter】
Created directory '/home/tmp-kun/.ssh'.
Enter passphrase (empty for no passphrase):【パスフレーズ】
Enter same passphrase again:【パスフレーズ】
Your identification has been saved in /home/tmp-kun/.ssh/identity.
Your public key has been saved in /home/tmp-kun/.ssh/identity.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx tmp-kun@KURO-BOX
ホームディレクトリに.sshディレクトリが作成され、秘密鍵(identity)と公開鍵(identity.pub) が作成されます。
プロトコルバージョン2(SSH2)のRSA鍵を作成
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tmp-kun/.ssh/id_rsa):
Created directory '/home/tmp-kun/.ssh'.
Enter passphrase (empty for no passphrase):【パスフレーズ】
Enter same passphrase again:【パスフレーズ】
Your identification has been saved in /home/tmp-kun/.ssh/id_rsa.
Your public key has been saved in /home/tmp-kun/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx tmp-kun@KURO-BOX
ホームディレクトリに.sshディレクトリが作成され、秘密鍵(id_rsa)と公開鍵(id_rsa.pub) が作成されます。
プロトコルバージョン2(SSH2)のDSA鍵を作成
$ ssh-keygen -t dsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tmp-kun/.ssh/id_dsa):
Created directory '/home/tmp-kun/.ssh'.
Enter passphrase (empty for no passphrase):【パスフレーズ】
Enter same passphrase again:【パスフレーズ】
Your identification has been saved in /home/tmp-kun/.ssh/id_dsa.
Your public key has been saved in /home/tmp-kun/.ssh/id_dsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx tmp-kun@KURO-BOX
ホームディレクトリに.sshディレクトリが作成され、秘密鍵(id_dsa)と公開鍵(id_dsa.pub) が作成されます。
鍵束を作成する
公開鍵の鍵束は、authorized_keysというファイルに作成します。(この作業は、接続先となるホストに 必要になります。)
$ cd ~/.ssh/
$ cat identity.pub >> authorized_keys
$ chmod 600 authorized_keys
クライアントから接続
UTF-8 Tera Term ProなどSSHが使えるクライアントから接続してみます。
接続には、接続先ホストに登録した公開鍵とペアとなる秘密鍵を使って接続します。
# ssh -i /root/.ssh/rsync root@192.168.xxx.010
The authenticity of host '192.168.xxx.010 (192.168.xxx.010)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)?
sshで初めて接続するホストの場合、接続先のホストのフィンガープリントの表示と「本当にこのホストに接続したいのか?」 という問合せが行なわれます。ユーザはここでログイン先が本当に目的のホストであることを確認します。
ここで「yes」と答えると、known_hostsというファイルに接続先のホストの公開鍵を保存し、 今後の接続時に変っていないかチェックして、なりすましに対処します。