原 scp命令拷贝文件报错scp: Connection closed
现象
1 2 3 4 5 | [root@localhost soft]# scp unrar root@smdw:/tmp/ Authorized users only. All activities may be monitored and reported. root@smdw's password: scp: Connection closed |
分析
1 2 3 4 5 | ping smdw ssh -v root@smdw scp -v unrar root@smdw:/tmp/ tail -f /var/log/secure |
-v 参数会显示详细的连接过程,有助于诊断问题。
日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | scp报错 [root@localhost soft]# scp unrar root@smdw:/tmp/ Authorized users only. All activities may be monitored and reported. root@smdw's password: scp: Connection closed [root@localhost soft]# 对方机器显示 Jun 13 18:01:22 localhost sshd-session[44737]: Connection from 192.168.16.91 port 52772 on 192.168.16.128 port 32122 rdomain "" Jun 13 18:01:23 localhost sshd-session[44738]: Connection from 192.168.16.127 port 40522 on 192.168.16.128 port 32122 rdomain "" Jun 13 18:01:23 localhost sshd-session[44738]: reprocess config line 148: Deprecated option RSAAuthentication Jun 13 18:01:23 localhost sshd-session[44738]: reprocess config line 150: Deprecated option RhostsRSAAuthentication Jun 13 18:01:23 localhost sshd-session[44738]: Failed publickey for root from 192.168.16.127 port 40522 ssh2: ED25519 SHA256:muGMb0to401dJjdfkspUcXIIXbvmtZBwPCgCz/1Z4h8 Jun 13 18:01:38 localhost sshd-session[44738]: Accepted password for root from 192.168.16.127 port 40522 ssh2 Jun 13 18:01:38 localhost sshd-session[44738]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0) Jun 13 18:01:38 localhost sshd-session[44738]: User child is on pid 44740 Jun 13 18:01:39 localhost sshd-session[44740]: Starting session: subsystem 'sftp' for root from 192.168.16.127 port 40522 id 0 Jun 13 18:01:39 localhost sshd-session[44740]: Received disconnect from 192.168.16.127 port 40522:11: disconnected by user Jun 13 18:01:39 localhost sshd-session[44740]: Disconnected from user root 192.168.16.127 port 40522 Jun 13 18:01:39 localhost sshd-session[44738]: pam_unix(sshd:session): session closed for user root debug的错误如下 [root@localhost soft]# scp -v unrar root@smdw:/tmp/ Executing: program /usr/local/openssh/bin/ssh host smdw, user root, command sftp OpenSSH_9.9p2, OpenSSL 3.5.0 8 Apr 2025 debug1: Reading configuration data /root/.ssh/config debug1: /root/.ssh/config line 1: Applying options for * debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling debug1: Connecting to smdw [192.168.16.128] port 32122. debug1: Connection established. debug1: identity file /root/.ssh/id_rsa type 3 debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa_sk type -1 debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1 debug1: identity file /root/.ssh/id_ed25519 type -1 debug1: identity file /root/.ssh/id_ed25519-cert type -1 debug1: identity file /root/.ssh/id_ed25519_sk type -1 debug1: identity file /root/.ssh/id_ed25519_sk-cert type -1 debug1: identity file /root/.ssh/id_xmss type -1 debug1: identity file /root/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_9.9 debug1: Remote protocol version 2.0, remote software version OpenSSH_9.9 debug1: compat_banner: match: OpenSSH_9.9 pat OpenSSH* compat 0x04000000 debug1: Authenticating to smdw:32122 as 'root' debug1: load_hostkeys: fopen /root/.ssh/known_hosts2: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ssh-ed25519 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: SSH2_MSG_KEX_ECDH_REPLY received debug1: Server host key: ssh-ed25519 SHA256:AVb6SH/W6cYizomWHGkMro4k+JQ8SxasSL+X6evHX5k debug1: load_hostkeys: fopen /root/.ssh/known_hosts2: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory debug1: Host '[smdw]:32122' is known and matches the ED25519 host key. debug1: Found key in /root/.ssh/known_hosts:2 debug1: ssh_packet_send2_wrapped: resetting send seqnr 3 debug1: rekey out after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: Sending SSH2_MSG_EXT_INFO debug1: expecting SSH2_MSG_NEWKEYS debug1: ssh_packet_read_poll2: resetting read seqnr 3 debug1: SSH2_MSG_NEWKEYS received debug1: rekey in after 134217728 blocks debug1: SSH2_MSG_EXT_INFO received debug1: kex_ext_info_client_parse: server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,rsa-sha2-512,rsa-sha2-256> debug1: kex_ext_info_check_ver: publickey-hostbound@openssh.com=<0> debug1: kex_ext_info_check_ver: ping@openssh.com=<0> debug1: SSH2_MSG_SERVICE_ACCEPT received Authorized users only. All activities may be monitored and reported. debug1: SSH2_MSG_EXT_INFO received debug1: kex_ext_info_client_parse: server-sig-algs=<ssh-ed25519,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,rsa-sha2-512,rsa-sha2-256> debug1: Authentications that can continue: publickey,password debug1: Next authentication method: publickey debug1: Will attempt key: /root/.ssh/id_rsa ED25519 SHA256:muGMb0to401dJjdfkspUcXIIXbvmtZBwPCgCz/1Z4h8 debug1: Will attempt key: /root/.ssh/id_ecdsa debug1: Will attempt key: /root/.ssh/id_ecdsa_sk debug1: Will attempt key: /root/.ssh/id_ed25519 debug1: Will attempt key: /root/.ssh/id_ed25519_sk debug1: Will attempt key: /root/.ssh/id_xmss debug1: Offering public key: /root/.ssh/id_rsa ED25519 SHA256:muGMb0to401dJjdfkspUcXIIXbvmtZBwPCgCz/1Z4h8 debug1: Authentications that can continue: publickey,password debug1: Trying private key: /root/.ssh/id_ecdsa debug1: Trying private key: /root/.ssh/id_ecdsa_sk debug1: Trying private key: /root/.ssh/id_ed25519 debug1: Trying private key: /root/.ssh/id_ed25519_sk debug1: Trying private key: /root/.ssh/id_xmss debug1: Next authentication method: password root@smdw's password: Authenticated to smdw ([192.168.16.128]:32122) using "password". debug1: channel 0: new session [client-session] (inactive timeout: 0) debug1: Requesting no-more-sessions@openssh.com debug1: Entering interactive session. debug1: pledge: filesystem debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0 debug1: client_input_hostkeys: searching /root/.ssh/known_hosts for [smdw]:32122 / (none) debug1: client_input_hostkeys: searching /root/.ssh/known_hosts2 for [smdw]:32122 / (none) debug1: client_input_hostkeys: hostkeys file /root/.ssh/known_hosts2 does not exist debug1: client_input_hostkeys: host key found matching a different name/address, skipping UserKnownHostsFile update debug1: Sending subsystem: sftp debug1: pledge: fork debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 debug1: client_input_channel_req: channel 0 rtype eow@openssh.com reply 0 debug1: channel 0: free: client-session, nchannels 1 Transferred: sent 2220, received 2492 bytes, in 0.2 seconds Bytes per second: sent 14704.8, received 16506.4 debug1: Exit status 127 scp: Connection closed |
主要问题分析:
SFTP子系统问题:
- 日志显示连接成功建立,但立即断开("disconnected by user")
- 会话尝试启动SFTP子系统但失败("Starting session: subsystem 'sftp'")
可能的根本原因:
- 目标机器的SFTP子系统配置有问题
/etc/ssh/sshd_config中可能有配置问题- 权限问题导致SFTP无法正常工作
解决
从调试信息来看,SCP连接在认证成功后尝试启动SFTP子系统时失败了(Exit status 127)。以下是详细分析和解决方案:
问题根源
关键错误:
123debug1: Sending subsystem: sftpdebug1: Exit status 127scp: Connection closedExit status 127表示目标服务器无法找到SFTP子系统(通常是路径错误或权限问题)。- 虽然认证成功,但SFTP服务未能启动。
其他线索:
- 目标服务器使用自定义SSH端口(
32122)和自定义OpenSSH路径(/usr/local/openssh/bin/ssh),可能存在配置不一致。
- 目标服务器使用自定义SSH端口(
解决方案
1. 检查目标机的SFTP配置
在目标服务器(smdw)上执行:
1 | vi /etc/ssh/sshd_config |
确保以下配置正确:
1 | Subsystem sftp /usr/libexec/openssh/sftp-server |
如果路径不同(例如自定义安装的OpenSSH),需修正为实际路径:
1 | find / -name "sftp-server" 2>/dev/null |
然后更新配置,例如:


