公開鍵暗号でのsshリモートログイン

今更ながら公開鍵暗号の設定を行う。
localはMac OS X 10.3.9。まずssh-keygenでssh2のrsa鍵を作る。

~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/local/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): (パスフレーズ入力)
Enter same passphrase again: (パスフレーズ再入力)
Your identification has been saved in /Users/local/.ssh/id_rsa.
Your public key has been saved in /Users/local/.ssh/id_rsa.pub.
The key fingerprint is:
**:**:**:**:**:**:**:**:**:**:**:**:**:**:**:** local@local.local

これで~/.sshに鍵が出来た。sshubuntuにログイン済なので、~/.ssh/known_hostsもある。

~$ ls -l .ssh/
total 24
-rw-------  1 local  local  1743  8 Jan 23:04 id_rsa
-rw-r--r--  1 local  local   399  8 Jan 23:04 id_rsa.pub
-rw-r--r--  1 local  local   396  4 Jan 23:05 known_hosts

このうち公開鍵をubuntuへscpでコピーする。末尾のコロンを忘れない。

~$ scp ~/.ssh/id_rsa.pub 192.168.***.*:
id_rsa.pub                                    100%  399     0.4KB/s   00:00    

ubuntuにログインして、~/.ssh/authorized_keysにid_rsa.pubを登録。

~$ cat id_rsa.pub >> ~/.ssh/authorized_keys
-bash: /home/ubuntu/.ssh/authorized_keys: No such file or directory

おっと、~/.ssh/が無かった。やりなおし。

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

この後にパーミッションを正しくする。

~$ chmod 700 ~/.ssh
~$ chmod 600 ~/.ssh/authorized_keys

確認。

ls -al
total **
drwx------ 2 ubuntu ubuntu 4096 2008-01-08 23:24 .ssh
-rw-r--r-- 1 ubuntu ubuntu  399 2008-01-08 23:20 id_rsa.pub
~$ cd .ssh/
~/.ssh$ ls -l
total 4
-rw------- 1 ubuntu ubuntu  399 2008-01-08 23:24 authorized_keys

ということでログアウトしてubuntuへ再度ログイン。

~$ ssh 192.168.***.*
Enter passphrase for key '/Users/local/.ssh/id_rsa': (パスフレーズ入力)

ということでパスフレーズを入れるとちゃんと入れた。
ちなみに/var/log/auth.logを見ると区別されている。

Jan  8 23:50:34 ubuntu sshd[9652]: Accepted password for ubuntu from 192.168.***.* port 49637 ssh2
Jan  8 23:50:34 ubuntu sshd[9656]: pam_unix(ssh:session): session opened for user ubuntu by (uid=0)

これはパスワード認証。

Jan  8 23:54:40 ubuntu sshd[9728]: Accepted publickey for ubuntu from 192.168.***.* port 49645 ssh2
Jan  8 23:54:40 ubuntu sshd[9732]: pam_unix(ssh:session): session opened for user ubuntu by (uid=0)

こっちだとパスフレーズ認証。