
公開鍵認証を使い、パスワードなしでアクセスする方法である。
この方法は、単にパスワードを入力する手間が省ける以外に、例えば、SSHを経由してのrsyncやsshfsなどを運用する場合に必要となる。
まず、クライアント側(アクセスする側)でパスフレーズなしの公開鍵を下記の要領で作成する。Enter passphraseと聞かれたら何も入力せずにリターンキーを押す。
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/v/home/foo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /v/home/foo/.ssh/id_rsa.
Your public key has been saved in /v/home/foo/.ssh/id_rsa.pub.
The key fingerprint is:
45:eb:52:5f:ad:8c:f3:c5:b5:45:f7:5r:1d:98:c5:b4 foo@example
The key's randomart image is:
+--[ RSA 2048]----+
| . *+ |
| . .+o o+|
| + .E*|
| + .++ +*|
| S . + +.=|
| . o++ |
| o |
| |
| |
+-----------------+
すると、$HOME/.ssh/id_rsaと$HOME/.ssh/id_rsa.pubという二つのファイルが作成されるはずである。
このうち、id_rsa.pubをサーバ側(アクセスされる側)に持って行く。実際にはファイルの内容を、サーバ側の$HOME/.ssh/authorized_keysに追加する。そして、アクセス権を0600すなわち、ownerのみ読み書きができるように設定する。また.sshディレクトのアクセス権も0700になっているか確認する。
以上で、パスワードなしでアクセスできるようになる。
たいていの場合、.ssh内のファイルのアクセス権が自分以外でも読み書きできたり、ホームディレクトリのアクセス権が他人でも書き換えられるようになっていると、sshは安全のため、認証を行わなくなる。
.ssh/authorized_keysは600が原則。
ホームディレクトリは最低でも755にしておく。
商用SSH(学術機関などではフリーで使えるバージョンが公開されている)とOpenSSHでは鍵を変換しないと使えない。まず、クライアント側Windowsのコマンドプロンプトで、公開鍵を以下の要領で作成する。
C:\>ssh-keygen2 -t rsa
Generating 2048-bit rsa key pair
5 oOo.oOo.oOo.
Key generated.
2048-bit rsa, foo@BAB4380083, Fri Apr 07 2006 05:28:47
Passphrase :
Again :
Key is stored with NULL passphrase.
(You can ignore the following warning if you are generating hostkeys.)
This is not recommended.
Don't do this unless you know what you're doing.
If file system protections fail (someone can access the keyfile),
or if the super-user is malicious, your key can be used without
the deciphering effort.
Private key saved to C:/Documents and Settings/foo/Application Data/SSH/UserKeys/id_rsa_2048_a
Public key saved to C:/Documents and Settings/foo/Application Data/SSH/UserKeys/id_rsa_2048_a.pub
できたid_rsa_2048_a.pubをサーバ側のLinuxに持って行く。これを次のコマンドで変換する。
$ ssh-keygen -i -f id_rsa_2048_a.pub > id_rsa_2048_a_openssh.pub
これで、id_rsa_2048_a_openssh.pubはOpenSSHで使える公開鍵に変換された。この内容を、$HOME/.ssh/authorized_keysに追加してやると、商用SSHから、パスワードなしでアクセスできるようになる。
なお、同様に、商用SSHの秘密鍵をOpenSSHで使える形式に変換するには、
$ ssh-keygen -i -f id_rsa_2048_a > id_rsa
とする。そしてid_rsaを自分のホームディレクトリ上にある.sshに移動して、アクセス権を600にする。
ファイル名は必ずir_rsaとすること。(id_rsa_2048_a_openssh等にすると、うまくいかない)