首先使用ssh -i /path/to/keyfile user@host1和host2 -vvvv,分别查看你能否登录这两台机器以及成功使用的keyfile
....
debug1: Trying private key: /Users/xxx/.ssh/host1.pem
debug1: read PEM private key done: type RSA
debug3: sign_and_send_pubkey: RSA 30:a9:37:b5:c4:ad:8a:b4:c8:64:f7:fb:c1:7d:48:62
debug2: we sent a publickey packet, wait for reply
debug1: Authentication succeeded (publickey).
Authenticated to host1 ....
fabric
from fabric.api import *
env.hosts = ['host1', 'host2']
env.user = 'user'
env.key_filename = '/path/to/keyfile.pem'
run('uname -a')
放在「~/.ssh/authorized_keys 下」?应该是放它里边。它应该是个文件,一行一个公钥。
授权失败的话,请检查系统日志。另外注意 ~/.ssh 的权限应该是 700,当然所有者也不能错。