Lion Serverとのssh接続設定

Server.app 設定

Server.appのハードウェア>設定で「SSHによるリモートログインを許可」にチェックを付ける。


他のMacからログイン

IPアドレスまたはサーバ名で接続。@マークの前にユーザを表記してログインユーザを指定。

air:~ user$ ssh admin@192.168.xxx.yy
The authenticity of host '192.168.xxx.yy (192.168.xxx.yy)' can't be established.
RSA key fingerprint is 5e:bd:45:60:50:4a:42:ea:36:f2:7f:7c:5a:8d:xx:yy.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.xxx.yy' (RSA) to the list of known hosts.
Password:
mini:~ admin$ pwd
/Users/admin
mini:~ admin$ logout
Connection to 192.168.xxx.yy closed.


RSA公開鍵をサーバへ送る

クライアントの公開鍵は~/.ssh/id_rsa.pubにあるという前提。
(鍵を作ってなければ"ssh-keygen -t rsa"で作成してくださいませ。参考:http://homepage1.nifty.com/y-osumi/works/code/ssh/
sshクライアント側のRSA公開鍵をサーバへ送る。scpコマンド末尾のコロンが重要。

air:~ user$ scp .ssh/id_rsa.pub admin@192.168.xxx.yy:
Password:
id_rsa.pub                                    100%  399     0.4KB/s   00:00    

ログインして公開鍵を設定。

air:~ user$ ssh admin@192.168.xxx.yy
Password:
Last login: Thu May 24 16:23:40 2012 from 192.168.xxx.zz
mini:~ admin$ ls -alF
total 56
drwxr-xr-x+ 17 admin  staff    578 May 24 16:32 ./
drwxr-xr-x+  7 root   admin    238 May 20 23:11 ../
-rw-------   1 admin  staff      4 May 17 18:29 .CFUserTextEncoding
-rw-r--r--@  1 admin  staff  15364 May 23 20:25 .DS_Store
drwx------   2 admin  staff     68 May 24 10:28 .Trash/
-rw-------   1 admin  staff   1845 May 24 16:23 .bash_history
drwx------   3 admin  staff    102 May 23 16:26 .cups/
-rw-------   1 admin  staff     35 May 22 14:13 .lesshst
drwx------+  3 admin  staff    102 May 17 18:29 Desktop/
drwx------+  5 admin  staff    170 May 18 10:18 Documents/
drwx------+  5 admin  staff    170 May 23 02:36 Downloads/
drwx------@ 37 admin  staff   1258 May 23 15:28 Library/
drwx------+  3 admin  staff    102 May 17 18:29 Movies/
drwx------+  3 admin  staff    102 May 17 18:29 Music/
drwx------+  4 admin  staff    136 May 17 18:29 Pictures/
drwxr-xr-x+  4 admin  staff    136 May 19 18:01 Public/
-rw-r--r--   1 admin  staff    399 May 24 16:32 id_rsa.pub

まず.sshディレクトリ作成し、その中にauthorized_keysを作成し、そこに公開鍵を追記する。

mini:~ admin$ mkdir .ssh
mini:~ admin$ cat id_rsa.pub >> .ssh/authorized_keys

確認。そしてパーミッションを適切に変更。.sshは700、authorized_keysは600。

mini:~ admin$ cd .ssh/
mini:.ssh admin$ ls -l
total 8
-rw-r--r--  1 admin  staff  399 May 24 16:34 authorized_keys
mini:.ssh admin$ chmod 600 authorized_keys 
mini:.ssh admin$ cd
mini:~ admin$ ls -al | grep .ssh
-rw-------   1 admin  staff     35 May 22 14:13 .lesshst
drwxr-xr-x   3 admin  staff    102 May 24 16:34 .ssh
mini:~ admin$ chmod 700 .ssh/
mini:~ admin$ ls -al | grep .ssh
-rw-------   1 admin  staff     35 May 22 14:13 .lesshst
drwx------   3 admin  staff    102 May 24 16:34 .ssh
mini:~ admin$ logout
Connection to 192.168.xxx.yy closed.

再度ログインしてみると、今度はパスワードを訊いてこない。これで公開鍵によるログインは成功。

air:~ user$ ssh admin@192.168.xxx.yy
Last login: Thu May 24 16:33:09 2012 from 192.168.xxx.zz
mini:~ admin$ 


パスワード認証を拒否し公開鍵認証のみに限定する

/etc/sshd_configを修正する。
私はemacsもviも苦手なのでnanoを使う。もちろんsudoじゃないと保存できないので"sudo nano /etc/sshd_config"とする。
なお、修正作業前に"sudo cp /etc/sshd_config /etc/sshd_config.bak"とバックアップを作っておくと安心。

修正箇所は以下の2箇所。

  • 44行目 公開鍵認証を有効化、公開鍵の場所を明示
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile	.ssh/authorized_keys

の2行目と3行目の#を消してコメントアウトを外す。

#RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile	.ssh/authorized_keys
  • 58行目 パスワード認証の拒否
# To disable tunneled clear text passwords both PasswordAuthentication and
# ChallengeResponseAuthentication must be set to "no".
#PasswordAuthentication no
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes

の3行目4行目のコメントアウトを外す。最後の7行目はコメントアウトを外すと共にyesをnoに変更

# To disable tunneled clear text passwords both PasswordAuthentication and
# ChallengeResponseAuthentication must be set to "no".
PasswordAuthentication no
PermitEmptyPasswords no

# Change to no to disable s/key passwords
ChallengeResponseAuthentication no

設定後はsshdの再起動で/etc/sshd_configを読み込ませる…のだけど、コマンドが不明。なのでシステム環境設定>共有の「リモートログイン」のチェックを一旦外し、再度チェックを付ける。
(参考: Mac:OSフリーズ時に再起動できるようにする [ssh接続] | Macとかの雑記帳

どうやら以下のコマンドで停止・起動を行えば良いようだ。

$sudo launchctl unload  /System/Library/LaunchDaemons/ssh.plist
$sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist

(参考:http://www.srcw.net/wiki/index.php?Lion#jc26e499及びmac osx - How to start/stop/restart launchd services from the command line? - Server Fault