scp 到 Amazon EC2 失败,但 ssh 有效

Posted

技术标签:

【中文标题】scp 到 Amazon EC2 失败,但 ssh 有效【英文标题】:scp to Amazon EC2 fails but ssh works 【发布时间】:2013-05-27 08:01:32 【问题描述】:

我有一个 EC2 实例正在运行。我可以毫无问题地 ssh:

ssh -i mykey.pem ec2-user@someIPaddress 

。但是,scp 失败。运行这个:

scp -vvv -i mykey.pem test.txt ec2-user@someIPaddress:/tmp/ 

产生以下结果:

Executing: program /usr/bin/ssh host someIPadress, user ec2-user, command scp -v -t /tmp/
OpenSSH_6.0p1 Debian-3ubuntu1, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to someIPaddress [someIPaddress] port 22.
debug1: connect to address someIPadress port 22: Network is unreachable
ssh: connect to host someIPaddress port 22: Network is unreachable
lost connection

其他信息:

我的 .pem 密钥只有我自己的读写权限 (-rw-------)。

有人告诉我,如果在 EC2 上运行 /bin/true 会产生非空行,则 scp 可能会出现问题,但这里不是这种情况:

[ec2-user@someIPaddress ~]$ /bin/true
[ec2-user@someIPaddress ~]$ 

我验证了 test.txt 存在 :)

我验证我可以通过 ssh 登录来写入 /tmp。

我很惊讶 ssh 有效,但 scp 无效。有什么想法吗?

编辑:运行

ssh -vvv -i mykey.pem ec2-user@someIP 

产生:

OpenSSH_6.0p1 Debian-3ubuntu1, OpenSSL 1.0.1c 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to someIPaddress [someIPaddress] port 22. 
debug1: Connection established.
debug3: Incorrect RSA1 identifier
debug3: Could not load "mykey.pem" as a RSA1 public key 
debug1: identity file mykey.pem type -1
debug1: identity file mykey.pem-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.1
debug1: match: OpenSSH_6.1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0 
debug1: Local version string SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1
debug2: fd 3 setting O_NONBLOCK
debug3: load_hostkeys: loading entries for host "someIPaddress" from file "/home/burger/.ssh/known_hosts"
debug3: load_hostkeys: found key type RSA in file /home/burger/.ssh/known_hosts:3
debug3: load_hostkeys: loaded 1 keys
debug3: order_hostkeyalgs: prefer hostkeyalgs: ssh-rsa-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-rsa
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: kex_parse_kexinit: 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-cert-v01@openssh.com,ssh-rsa-cert-v00@openssh.com,ssh-rsa,ecdsa-sha2-nistp256-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ssh-dss-cert-v01@openssh.com,ssh-dss-cert-v00@openssh.com,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-256-96,hmac-sha2-512,hmac-sha2-512-96,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com,zlib
debug2: kex_parse_kexinit: none,zlib@openssh.com,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: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
debug2: kex_parse_kexinit: ssh-rsa,ssh-dss
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-sha2-256,hmac-sha2-512,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
debug2: kex_parse_kexinit: none,zlib@openssh.com
debug2: kex_parse_kexinit: none,zlib@openssh.com
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: found hmac-md5
debug1: kex: server->client aes128-ctr hmac-md5 none
debug2: mac_setup: found hmac-md5
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug2: dh_gen_key: priv key bits set: 139/256
debug2: bits set: 481/1024
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA somekey
debug3: load_hostkeys: loading entries for host "someIPaddress" from file "/home/burger/.ssh/known_hosts"
debug3: load_hostkeys: found key type RSA in file /home/burger/.ssh/known_hosts:3
debug3: load_hostkeys: loaded 1 keys
debug1: Host 'someIPaddress' is known and matches the RSA host key.
debug1: Found key in /home/burger/.ssh/known_hosts:3
debug2: bits set: 523/1024
debug1: ssh_rsa_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: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug2: key: mykey.pem ((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: Trying private key: mykey.pem
debug1: read PEM private key done: type RSA
debug3: sign_and_send_pubkey: RSA some-key
debug2: we sent a publickey packet, wait for reply
debug1: Authentication succeeded (publickey).
Authenticated to someIPaddress ([someIPaddress]:22).
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug2: fd 3 setting TCP_NODELAY
debug3: packet_set_tos: set IP_TOS 0x10
debug2: channel 0: request pty-req confirm 1
debug1: Sending environment.
debug3: Ignored env rvm_gemsets_path
debug3: Ignored env rvm_scripts_path
debug3: Ignored env SSH_AGENT_PID
debug3: Ignored env rvm_bin_path
debug3: Ignored env GEM_HOME
debug3: Ignored env rvm_man_path
debug3: Ignored env TERM
debug3: Ignored env SHELL
debug3: Ignored env XDG_SESSION_COOKIE
debug3: Ignored env IRBRC
debug3: Ignored env rvm_user_path
debug3: Ignored env rvm_wrappers_path
debug3: Ignored env WINDOWID
debug3: Ignored env rvm_patches_path
debug3: Ignored env OLDPWD
debug3: Ignored env GNOME_KEYRING_CONTROL
debug3: Ignored env MY_RUBY_HOME
debug3: Ignored env rvm_docs_path
debug3: Ignored env GTK_MODULES
debug3: Ignored env rvm_verbose_flag
debug3: Ignored env USER
debug3: Ignored env LS_COLORS
debug3: Ignored env rvm_gems_cache_path
debug3: Ignored env rvm_config_path
debug3: Ignored env XDG_SESSION_PATH
debug3: Ignored env rvm_path
debug3: Ignored env XDG_SEAT_PATH
debug3: Ignored env rvm_debug_flag
debug3: Ignored env SSH_AUTH_SOCK
debug3: Ignored env DEFAULTS_PATH
debug3: Ignored env XDG_CONFIG_DIRS
debug3: Ignored env rvm_prefix
debug3: Ignored env rvm_examples_path
debug3: Ignored env PATH
debug3: Ignored env DESKTOP_SESSION
debug3: Ignored env rvm_rubies_path
debug3: Ignored env rvm_loaded_flag
debug3: Ignored env PWD
debug3: Ignored env GNOME_KEYRING_PID
debug1: Sending env LANG = en_US.UTF-8
debug2: channel 0: request env confirm 0
debug3: Ignored env MANDATORY_PATH
debug3: Ignored env UBUNTU_MENUPROXY
debug3: Ignored env rvm_usr_path
debug3: Ignored env GDMSESSION
debug3: Ignored env rvm_version
debug3: Ignored env rvm_src_path
debug3: Ignored env HOME
debug3: Ignored env SHLVL
debug3: Ignored env rvm_gems_path
debug3: Ignored env _JAVA_AWT_WM_NONREPARENTING
debug3: Ignored env rvm_ruby_string
debug3: Ignored env rvm_tmp_path
debug3: Ignored env LOGNAME
debug3: Ignored env GEM_PATH
debug3: Ignored env rvm_lib_path
debug3: Ignored env XDG_DATA_DIRS
debug3: Ignored env DBUS_SESSION_BUS_ADDRESS
debug3: Ignored env rvm_repos_path
debug3: Ignored env LESSOPEN
debug3: Ignored env rvm_reload_flag
debug3: Ignored env rvm_log_path
debug3: Ignored env rvm_help_path
debug3: Ignored env XDG_RUNTIME_DIR
debug3: Ignored env DISPLAY
debug3: Ignored env rvm_environments_path
debug3: Ignored env RUBY_VERSION
debug3: Ignored env rvm_archives_path
debug3: Ignored env LESSCLOSE
debug3: Ignored env rvm_user_install_flag
debug3: Ignored env COLORTERM
debug3: Ignored env XAUTHORITY
debug3: Ignored env _
debug2: channel 0: request shell confirm 1
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel_input_status_confirm: type 99 id 0
debug2: PTY allocation request accepted on channel 0
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: shell request accepted on channel 0
Last login: Fri May 31 14:18:38 2013 from stgt-5f7197c5.pool.mediaways.net

__|  __|_  )
_|  (     /   Amazon Linux AMI
___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2013.03-release-notes/
[ec2-user@ip-someIPaddress ~]$

【问题讨论】:

ssh -vvv 的调试输出是什么? 输出很长。我将它添加到我的问题的主要测试中。 有趣的是,ssh 似乎一开始确实失败了,但回退到另一个最终成功的方法。恐怕我无法进一步诊断,但也许其他人可以提供一些帮助。 您确定要从每台主机连接到相同的 IP 地址吗? scp 命令的错误似乎表明它无法连接到 ip 地址。 感谢数据!这确实是问题所在。它现在可以正常工作了。我为自己感到羞耻。 【参考方案1】:

此问题的另一个原因(SCP 在 SSH 成功时失败)是在登录期间向控制台打印了任何消息(例如,来自您的 .bashrc 脚本)

另见

https://superuser.com/questions/395356/scp-doesnt-work-but-ssh-does SCP doesn't work when echo in .bashrc?

【讨论】:

这对我有用,我在我的 AWS Linux 机器上设置了 zsh。但是chsh -s /bin/zsh 命令不起作用,所以我将zsh; 连接到我的.bashrc 文件的末尾。删除该命令后,scp 工作。我现在使用 chsh -s /bin/zsh ec2-user 作为 root 将 zsh 作为默认 shell。谢谢。

以上是关于scp 到 Amazon EC2 失败,但 ssh 有效的主要内容,如果未能解决你的问题,请参考以下文章

在 Linux 中通过 SSH 连接到 Amazon EC2

使用 scp 将文件复制到 Amazon EC2 实例?

使用 scp 将文件传输到 amazon ec2 实例总是给我拒绝权限(公钥,gssapi-with-mic)

无法从 ubuntu 14.04 LTS SSH 到 Amazon EC2 实例

连接到 Amazon EC2 实例时 SSH 挂起

在 Windows 中使用 PuTTY SSH 到 Amazon EC2 实例