このブログは、旧・はてなダイアリー「檜山正幸のキマイラ飼育記 メモ編」(http://d.hatena.ne.jp/m-hiyama-memo/)のデータを移行・保存したものであり、今後(2019年1月以降)更新の予定はありません。

今後の更新は、新しいブログ http://m-hiyama-memo.hatenablog.com/ で行います。

MercurialのためのSSHの設定と実験

サーバー側で、ssh-keygenを使ってキーペア(秘密、公開)を作る。


$ ssh-keygen -t rsa -C 'hiyama@chimaira.org'
Generating public/private rsa key pair.
Enter file in which to save the key (/home/caty/.ssh/id_rsa):
Created directory '/home/caty/.ssh'.
Enter passphrase (empty for no passphrase):パスフレーズ入力
Enter same passphrase again:パスフレーズ入力
Your identification has been saved in /home/caty/.ssh/id_rsa.
Your public key has been saved in /home/caty/.ssh/id_rsa.pub.
The key fingerprint is:
4b:67:4c:f2:1e:c3:8b:80:1e:4e:20:97:ad:5c:e6:b0 hiyama@chimaira.org

$ ls ~/.ssh/
id_rsa id_rsa.pub

$ chmod 0700 ~/.ssh

$

その後、公開キーをオーソライズされたキーとして保管する。


$ cd ~/.ssh/

$ cat id_rsa.pub >> authorized_keys

$ chmod 0600 authorized_keys

$

秘密キーをなんらかの方法でWindowsクライアントに持ってくる。「-----BEGIN RSA PRIVATE KEY-----」行と「-----END RSA PRIVATE KEY-----」行で挟まれたアスキーテキストなので、移動は簡単。そのアスキーテキストファイルを、Windowsの ~/.ssh に置く(どこでもいいが、サーバー側と同じ習慣に従うのが吉)。拡張子は .key にした。

puttygen.exe を起動して、[Load an existing private key file] を選ぶ。~/.ssh/*.key のファイルを指定する。このとき、パスフレーズを聞かれる。その後で、[Save private key] をして、PuTTY形式で保存する。このときの拡張子は .ppk (PuTTY Private Key File) が吉。

PuTTY Private Key File は次の形式。


PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: imported-openssh-key
Public-Lines: 4
1
2
3
4
Private-Lines: 8
1
2
3
4
5
6
7
8
Private-MAC: xxx

plink.exeをパスが通った場所に置いて、次のバッチファイルを作った。


@echo off
rem This is ssh.bat
setlocal

if NOT "%SSH_USER%"=="" goto SSH_USER
set _KEY=%1
shift
goto DO

:SSH_USER
set _KEY=%SSH_USER%
goto DO

:DO
echo plink.exe -ssh -i "%HOME%\.ssh\%_KEY%.ppk" %1 %2 %3 %4 %5 %6 %7 %8 %9
plink.exe -ssh -i "%HOME%\.ssh\%_KEY%.ppk" %1 %2 %3 %4 %5 %6 %7 %8 %9

endlocal

最初の実行は次のようになる。


C:\Documents and Settings\Hiyama>ssh caty@caty-sites.net date
plink.exe -ssh -i "C:\Documents and Settings\Hiyama\Work\.ssh\caty.ppk" caty@cay-sites.net date
The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's rsa2 key fingerprint is:
ssh-rsa 1024 71:3b:12:16:99:12:f4:32:09:b5:35:d9:2e:ce:38:59
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
Store key in cache? (y/n) y
Passphrase for key "imported-openssh-key":
Sat Feb 13 15:11:52 JST 2010
C:\Documents and Settings\Hiyama>

二度目の実行は:


C:\Documents and Settings\Hiyama>ssh caty@caty-sites.net date
plink.exe -ssh -i "C:\Documents and Settings\Hiyama\Work\.ssh\caty.ppk" caty@caty-sites.net date
Passphrase for key "imported-openssh-key":
Sat Feb 13 15:16:49 JST 2010
C:\Documents and Settings\Hiyama>