向ec2-ssh添加用户是可行的,但scp不行



作为"ec2用户",我们可以对ec2实例进行SSH和SCP。

和其他用户一样,我们可以SSH,但不能SCP。

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

--

我们使用ssh代理,每个用户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代理与ssh的工作方式不同于与SCP的工作方式。

任何帮助都将不胜感激!!

调试信息

这起作用--ssh作为"ec2用户":

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

这也起作用——ssh作为"jdoe":

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

这也起作用--scp作为"ec2用户":

$> 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为"jdoe":

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

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

$> 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

最新更新