将用户添加到ec2-ssh可以正常工作,但是scp没有

时间:2017-10-30 17:48:53

标签: amazon-ec2 ssh username scp ssh-agent

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

与其他任何用户一样,我们可以SSH正常,但不能SCP。

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

-

我们使用ssh-agent,每个用户ssh-添加自己的.pem密钥。 他们的公钥被添加到远程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,我们只使用了系统默认值。

我真的不确定为什么本地和/或远程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

1 个答案:

答案 0 :(得分:1)

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

显然,在您的EC2实例登录期间会调用ssh-agent(例如,在您的Bash个人资料中)。

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

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