ksaitoの日記

日々試したことの覚え書き

ssh-keygen

移転しました。

自動的にリダイレクトします。

sshサーバにパスワードなしでログインする方法です。

鍵を生成する

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa): [デフォルトのまま]
Enter passphrase (empty for no passphrase): [パスフレーズを入力]
Enter same passphrase again: [パスフレーズを再入力]
Your identification has been saved in ~/.ssh/id_rsa.
Your public key has been saved in ~/.ssh/id_rsa.pub.
The key fingerprint is:

$ ls -l .ssh
合計 8
-rw------- 1 who group 1000 2006-11-25 18:40 id_rsa
-rw-r--r-- 1 who group  500 2006-11-25 18:40 id_rsa.pub

パスワードなしでログインするには秘密鍵ファイル(id_ras)を生成する時に入力するパスフレーズで[Enter]を指定します。
重要なことは生成した秘密鍵ファイル(id_rsa)の読み込み権限を持っている人は、誰でもパスワードなしのアクセスができてしまうことです。
これには、ローカルマシンのrootにアクセスできる人を含みます。
したがって、自分が唯一rootにアクセスできるユーザでない場合には、常にリスクがあります。

sshサーバ単独で試す

ひとまず、同じマシンにパスワードなしでログインできるか試します。

authorized_keysファイルを作成する。
$ cd ~/.ssh
$ touch authorized_keys
$ chmod 600 authorized_keys
$ ls -l authorized_keys
-rw------- 1 who group 0 2006-11-25 19:12 authorized_keys
公開鍵を登録してパスワードアクセスなしでログインできることを確認する。
$ ssh localhost
who@localhost's password:
Linux who-desktop 2.6.17-10-generic #2 SMP Fri Oct 13 18:45:35 UTC 2006 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Last login: Sat Nov 25 21:27:51 2006 from localhost
$ logout
Connection to localhost closed.
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ ssh localhost
Linux who-desktop 2.6.17-10-generic #2 SMP Fri Oct 13 18:45:35 UTC 2006 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Last login: Sat Nov 25 21:28:07 2006 from localhost
$

この手順を他のマシンでやることで同じように機能します。

パスフレーズの変更

パスフレーズを変更する時には、ssh-keygen -pを使います。

$ ssh-keygen -p
Enter file in which the key is (~/.ssh/id_rsa):
Key has comment '~/.ssh/id_rsa'
Enter new passphrase (empty for no passphrase): [パスフレーズを入力]
Enter same passphrase again: [パスフレーズを再入力]
Your identification has been saved with the new passphrase.
$ ssh localhost
Enter passphrase for key '~/.ssh/id_rsa':
Linux who-desktop 2.6.17-10-generic #2 SMP Fri Oct 13 18:45:35 UTC 2006 i686

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Last login: Sat Nov 25 21:28:50 2006 from localhost
$