我正在尝试将 SSH 密钥与 Fabric 一起使用,而不是每次运行时都必须输入密码fab
。我们所有的主机都共享同一个/etc/ssh/ssh_known_hosts
文件,其中包含它们所有的公共 rsa 密钥,而且我能够在没有密码的情况下从一台主机到另一台主机进行 SSH。
我必须遵循我的环境变量设置fabfile.py
:
env.use_ssh_config = True
env.ssh_config_path = '/etc/ssh/ssh_config'
env.key_filename = '/etc/ssh/ssh_host_rsa_key'
我fab test
以 root 身份运行一个简单的命令:
def test:
run('uname -s')
我已经浏览了所有文档并进行了大量搜索,但我没有看到使用密钥和配置的示例/etc/ssh
;这些示例通常展示如何使用 configs 和 keys in ~/.ssh/
,所以我可能误解了如何使用这些设置。
这是一个调试:
root@beef:~> fab test
[chicken] Executing task 'test'
[chicken] run: uname -s
DEBUG:ssh.transport:starting thread (client mode): 0x141c710L
INFO:ssh.transport:Connected (version 1.99, client OpenSSH_5.1)
DEBUG:ssh.transport:kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'arcfour128', 'arcfour256', 'arcfour', 'aes192-cbc', 'aes256-cbc', '[email protected]', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] server encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'arcfour128', 'arcfour256', 'arcfour', 'aes192-cbc', 'aes256-cbc', '[email protected]', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] client mac:['hmac-md5', 'hmac-sha1', '[email protected]', 'hmac-ripemd160', '[email protected]', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', '[email protected]', 'hmac-ripemd160', '[email protected]', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', '[email protected]', 'zlib'] server compress:['none', '[email protected]', 'zlib'] client lang:[''] server lang:[''] kex follows?False
DEBUG:ssh.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEBUG:ssh.transport:using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
DEBUG:ssh.transport:Switch to new keys ...
DEBUG:ssh.transport:Adding ssh-rsa host key for chicken: 56f3f71a494013976c183844d342ed1b
[chicken] Login password for 'root':
系统日志chicken
说
Jun 22 13:48:47 chicken sshd[7328]:未收到来自 172.xxx 的标识字符串
所以我没有传递正确的密钥文件或其他东西......
更新
我从 Fabric 用户邮件列表收到了一些故障排除提示。
我可以看到,当我从 shell 连接到 ssh 客户端时,客户端请求连接方法为“无”,然后是“基于主机”。Fabric(或 Paramiko)似乎立即请求公钥。请注意每个示例中选择的连接方法:
登录成功
root@beef:~> ssh -t -i /etc/ssh/ssh_host_rsa_key chicken uname -s
root@chicken:~> /usr/sbin/sshd -d
...snip...
debug1: userauth-request for user root service ssh-connection method none
debug1: attempt 0 failures 0
debug1: PAM: initializing for "root"
debug1: userauth-request for user root service ssh-connection method hostbased
debug1: attempt 1 failures 0
debug1: userauth_hostbased: cuser root chost beef. pkalg ssh-dss slen 55
debug1: PAM: setting PAM_RHOST to "beef"
debug1: PAM: setting PAM_TTY to "ssh"
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: restore_uid: 0/0
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: fd 4 clearing O_NONBLOCK
debug1: restore_uid: 0/0
Failed hostbased for root from 172.x.x.x port 54623 ssh2
debug1: userauth-request for user root service ssh-connection method hostbased
debug1: attempt 2 failures 1
debug1: userauth_hostbased: cuser root chost beef. pkalg ssh-rsa slen 271
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: restore_uid: 0/0
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: fd 4 clearing O_NONBLOCK
debug1: restore_uid: 0/0
debug1: ssh_rsa_verify: signature correct
debug1: do_pam_account: called
Accepted hostbased for root from 172.x.x.x port 54623 ssh2
通过 Fabric 登录失败
root@beef:~ > fab test
root@chicken:~> /usr/sbin/sshd -d
...snip...
debug1: userauth-request for user root service ssh-connection method publickey
debug1: attempt 0 failures 0
debug1: PAM: initializing for "root"
debug1: PAM: setting PAM_RHOST to "beef"
debug1: PAM: setting PAM_TTY to "ssh"
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys
debug1: restore_uid: 0/0
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys2
debug1: restore_uid: 0/0
Failed publickey for root from 172.x.x.x port 54630 ssh2
所以...问题:有什么方法可以将 hostbased 指定为 Fabric/Paramiko 中的首选连接方法?
这里的部分问题是我对公钥与基于主机的身份验证的混淆。不过,我确实问过 Fabric 邮件列表这个问题:
其中一位作者回应道:
Paramiko 是 Fabric 使用的 python SSH 模块。所以,看起来没有办法在 Fabric/Paramiko 中设置首选的身份验证方案。我决定最好在我的 fabfile 中运行我的命令,而不是尝试以
fab
root 身份运行并且不提示输入密码,这样我就可以在. 无论如何,这要求我将我的 sudo 密码作为参数传递。sudo()
/var/log/sudo.log