当我尝试使用代理转发通过 SSH 进行连接时,出现权限被拒绝的情况。我有四台机器:我的本地机器(我)、一台私人服务器(私人)、一个可以路由到私人服务器的公共网关(网关)和一个公共服务器(目的地)。
如果我需要连接到“目的地”或“网关”,我只需执行以下操作,即可验证我的密钥对两个主机都有效:
me@me:~$ ssh -i my.key destination
me@me:~$ ssh -i my.key gateway
如果我需要连接到“私人”,我可以执行以下操作,以验证代理转发是否按预期工作以及我的密钥对主机有效:
me@me:~$ ssh -Ati my.key gateway
me@gateway:~$ ssh private
如果我需要从“网关”连接到“目的地”,我可以执行以下操作,以验证代理转发在连接到目的地时是否有效:
me@me:~$ ssh -Ati my.key gateway
me@gateway:~$ ssh destination
如果我需要从“私人”连接到“目的地”,我应该能够执行以下操作:
me@me:~$ ssh -Ati my.key gateway
me@gateway:~$ ssh -At private
me@private:~$ ssh destination
但是,当我尝试最后一个命令时,我收到Permission denied (publickey).
如果我在详细程度打开的情况下运行,我会得到以下信息:
OpenSSH_6.6.1, OpenSSL 1.0.1k-fips 8 Jan 2015
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to destination [52.35.125.100] port 22.
debug1: Connection established.
debug1: identity file /home/me/.ssh/id_rsa type -1
debug1: identity file /home/me/.ssh/id_rsa-cert type -1
debug1: identity file /home/me/.ssh/id_dsa type -1
debug1: identity file /home/me/.ssh/id_dsa-cert type -1
debug1: identity file /home/me/.ssh/id_ecdsa type -1
debug1: identity file /home/me/.ssh/id_ecdsa-cert type -1
debug1: identity file /home/me/.ssh/id_ed25519 type -1
debug1: identity file /home/me/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.6.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1
debug1: match: OpenSSH_6.6.1 pat OpenSSH_6.6.1* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug3: load_hostkeys: loading entries for host "destination" from file "/home/me/.ssh/known_hosts"
debug3: load_hostkeys: found key type ECDSA in file /home/me/.ssh/known_hosts:7
debug3: load_hostkeys: loaded 1 keys
debug3: order_hostkeyalgs: prefer hostkeyalgs: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],[email protected],[email protected],[email protected],ssh-ed25519,ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected],[email protected],aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected],[email protected],aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit: none,[email protected],zlib
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: kex_parse_kexinit: [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ecdsa-sha2-nistp256,ssh-ed25519
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected],[email protected],aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,[email protected],[email protected],[email protected],aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,[email protected]
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-md5,hmac-sha1,[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,[email protected],hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,[email protected]
debug2: kex_parse_kexinit: none,[email protected]
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit:
debug2: kex_parse_kexinit: first_kex_follows 0
debug2: kex_parse_kexinit: reserved 0
debug2: mac_setup: setup [email protected]
debug1: kex: server->client aes128-ctr [email protected] none
debug2: mac_setup: setup [email protected]
debug1: kex: client->server aes128-ctr [email protected] none
debug1: kex: [email protected] need=16 dh_need=16
debug1: kex: [email protected] need=16 dh_need=16
debug1: sending SSH2_MSG_KEX_ECDH_INIT
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ECDSA 01:2f:4e:3f:b3:a3:92:0f:29:e0:72:d7:d1:2c:af:4b
debug3: load_hostkeys: loading entries for host "destination" from file "/home/me/.ssh/known_hosts"
debug3: load_hostkeys: found key type ECDSA in file /home/me/.ssh/known_hosts:7
debug3: load_hostkeys: loaded 1 keys
debug3: load_hostkeys: loading entries for host "52.35.125.100" from file "/home/me/.ssh/known_hosts"
debug3: load_hostkeys: found key type ECDSA in file /home/me/.ssh/known_hosts:8
debug3: load_hostkeys: loaded 1 keys
debug1: Host 'destination' is known and matches the ECDSA host key.
debug1: Found key in /home/me/.ssh/known_hosts:7
debug1: ssh_ecdsa_verify: signature correct
debug2: kex_derive_keys
debug2: set_newkeys: mode 1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug2: set_newkeys: mode 0
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: me@me (0x5611e148efd0),
debug2: key: /home/me/.ssh/id_rsa ((nil)),
debug2: key: /home/me/.ssh/id_dsa ((nil)),
debug2: key: /home/me/.ssh/id_ecdsa ((nil)),
debug2: key: /home/me/.ssh/id_ed25519 ((nil)),
debug1: Authentications that can continue: publickey
debug3: start over, passed a different list publickey
debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password
debug3: authmethod_lookup publickey
debug3: remaining preferred: keyboard-interactive,password
debug3: authmethod_is_enabled publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: me@me
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/me/.ssh/id_rsa
debug3: no such identity: /home/me/.ssh/id_rsa: No such file or directory
debug1: Trying private key: /home/me/.ssh/id_dsa
debug3: no such identity: /home/me/.ssh/id_dsa: No such file or directory
debug1: Trying private key: /home/me/.ssh/id_ecdsa
debug3: no such identity: /home/me/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /home/me/.ssh/id_ed25519
debug3: no such identity: /home/me/.ssh/id_ed25519: No such file or directory
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
Permission denied (publickey).
注意:我尝试在发布之前清理主机、用户、地址等;我可能犯了一个转录错误。
我还验证了 SSH_AUTH_SOCK 在中间系统上是否正确导出:
me@gateway:~$ echo $SSH_AUTH_SOCK # /tmp/ssh-X5p4HjmGhP/agent.918
me@private:~$ echo $SSH_AUTH_SOCK # /tmp/ssh-aR5WfnzmgE/agent.14429
我应该寻找什么来确定问题出在哪里?
答案1
ssh-add -l
将列出您的中添加的键ssh-agent
。
ssh-agent
当您在命令行中输入密钥名称时,是什么让您认为您正在使用 进行连接?在连接到第一个主机之前,请运行
ssh-add my.key
然后您应该能够从第一跳连接到目标。
答案2
您应该寻找的是SSH_AUTH_SOCK=
连接后在中间系统上正确设置的环境变量。
连接后,您应该能够运行此命令并获得类似的结果。如果您没有看到变量集,则转发未发生。
# export | grep SSH_AUTH
declare -x SSH_AUTH_SOCK="/tmp/ssh-oDOTx6u2AR/agent.47026"
您的服务器必须允许转发,并且您的客户端必须配置为使用转发。服务器选项AllowAgentForwarding
默认为yes
,但如果服务器已强化,则可以在服务器上禁用。在您的客户端上,您可以在-A
连接期间使用 ,或将ForwardAgent
选项设置为yes
。您不应该转发到不受信任的主机。因此,您可以在配置中执行类似这样的操作。
# where *.example.org is pattern that matches all the systems you trust
# for forwarding purposes.
Host *.example.org
ForwardAgent yes
仔细查看您的日志,我发现您的客户端正在提供密钥,但目的地似乎没有接受它。
debug2: key: me@me (0x5611e148efd0), ## your key from the agent
debug2: key: /home/me/.ssh/id_rsa ((nil)),
debug2: key: /home/me/.ssh/id_dsa ((nil)),
debug2: key: /home/me/.ssh/id_ecdsa ((nil)),
...
debug1: Offering RSA public key: me@me
debug3: send_pubkey_test
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey