将用户添加到 ec2 - ssh 有效,但 scp 无效

Adding users to ec2 - ssh works, but scp does not

作为 "ec2-user",我们可以通过 SSH 和 SCP 连接到我们的 EC2 实例。

与任何其他用户一样,我们可以很好地使用 SSH,但不能使用 SCP。

相反,当尝试以 "ec2-user" 以外的任何用户身份使用 SCP 时,我们的 ec2 实例会生成一个新的(dup)ssh-agent,并且不会接收文件。

--

我们使用 ssh-agent,每个用户 ssh-add 自己的 .pem 密钥。 他们的 public 密钥被添加到远程 ec2 实例上,在每个用户的 ~/.ssh/authorized_keys 中。同样,我们的配置适用于 SSH。

(local)/home/jonathan/.ssh/config的内容是:

Host my-ec2.com
  User jdoe
  Hostname 123.123.123.123
  ForwardAgent yes
  IdentityFile /Users/jonathan/.ssh/key.pem

并且在 ec2 上没有远程 ~/.ssh/config,我们只是使用系统默认值。

我真的不确定为什么本地 and/or 远程 ssh-agent 在 SSH 上的工作方式与在 SCP 上的工作方式不同。

如有任何帮助,我们将不胜感激!

调试信息

这有效 -- ssh as "ec2-user":

$> ssh-add /Users/jonathan/.ssh/key.pem
$> ssh ec2-user@my-ec2.com

这也有效 -- ssh as "jdoe":

$> ssh-add /Users/jonathan/.ssh/key.pem
$> ssh jdoe@my-ec2.com

这也有效 -- scp as "ec2-user":

$> ssh-add /Users/jonathan/.ssh/key.pem
$> scp foo.txt ec2-user@my-ec2.com:~/.
foo.txt       100%    197   2.5KB/s   00:00 

这不起作用 -- scp as "jdoe":

$> ssh-add /Users/jonathan/.ssh/key.pem
$> scp foo.txt jdoe@my-ec2.com:~/.
Agent PID 12345

此外,每次失败的 scp 尝试都会在 ec2 上创建一个重复的 ssh-agent

$> ssh-add /Users/jonathan/.ssh/key.pem
$> ssh jdoe@my-ec2.com
$> ps -x | grep ssh-agent
11677 ?        Ss     0:00 ssh-agent
11708 ?        Ss     0:00 ssh-agent
11742 ?        Ss     0:00 ssh-agent
11919 ?        Ss     0:00 ssh-agent
12345 ?        Ss     0:00 ssh-agent
### a duplicate copy if ssh-agent is running with PID 12345
### there are as many ssh-agent running as failed SCP attempts...

这是完整 "scp -v" 输出的副本:

$> scp -v foo.txt jdoe@my-ec2.com:~/.
Executing: program /usr/bin/ssh host my-ec2.com, user jdoe, command scp -v -t ~/foo
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /Users/jonathan/.ssh/config
debug1: /Users/jonathan/.ssh/config line 2: Applying options for *
debug1: /Users/jonathan/.ssh/config line 7: Applying options for my-ec2.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 123.123.123.123 [123.123.123.123] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jonathan/.ssh/cps-keypair.pem type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/jonathan/.ssh/cps-keypair.pem-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 123.123.123.123:22 as 'jdoe'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:j4kSCIWNUz5k7LHHK+n9iR9kxktihrD4X/srX4uX/5U
debug1: Host '123.123.123.123' is known and matches the ECDSA host key.
debug1: Found key in /Users/jonathan/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/jonathan/.ssh/cps-keypair.pem
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).
Authenticated to 123.123.123.123 ([123.123.123.123]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending command: scp -v -t ~/foo
Agent pid 24732
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 2812, received 2696 bytes, in 0.2 seconds
Bytes per second: sent 15501.9, received 14862.4
debug1: Exit status 0

在您的 EC2 实例上启动 ssh-agent 仅用于交互式会话。 scp 不需要它,它的输出会导致 scp 失败。

显然 ssh-agent 在登录您的 EC2 实例期间被调用(例如,在您的 Bash 配置文件中)。

要阻止它启动,请调整您的 EC2 实例上的配置文件脚本 - 例如通过添加像这样的 if 语句(假设您在 EC2 实例上使用 Bash):

if [[ "$-" == *i* ]]; then
  # <start ssh agent here>
fi