SCP传输至远程主机失败:client_loop: send disconnect: Broken pipe
SCP远程传输失败:client_loop: send disconnect: Broken pipe
问题背景
本地Fedora 36虚拟机向同主机的CentOS 7.9虚拟机执行scp -vvv foobar user@host:命令可成功,但向配置几乎一致的远程CentOS 7.9虚拟机执行该命令时失败,报错:
client_loop: send disconnect: Broken pipe scp: Connection closed
成功场景Verbose日志
... debug1: Sending subsystem: sftp debug2: channel 0: request subsystem confirm 1 debug3: send packet: type 98 debug2: channel_input_open_confirmation: channel 0: callback done debug2: channel 0: open confirm rwindow 0 rmax 32768 debug2: channel 0: rcvd adjust 2097152 debug3: receive packet: type 99 debug2: channel_input_status_confirm: type 99 id 0 debug2: subsystem request accepted on channel 0 scp: debug2: Remote version: 3 scp: debug2: Server supports extension "posix-rename@openssh.com" revision 1 scp: debug2: Server supports extension "statvfs@openssh.com" revision 2 scp: debug2: Server supports extension "fstatvfs@openssh.com" revision 2 scp: debug2: Server supports extension "hardlink@openssh.com" revision 1 scp: debug2: Server supports extension "fsync@openssh.com" revision 1 scp: debug3: Sent message fd 6 T:17 I:1 scp: debug3: Recevied stat reply T:105 I:1 F:0x000f M:40700 scp: debug3: source_sftp: copying local foobar to remote ./foobar scp: debug3: Sent dest message SSH2_FXP_OPEN I:2 P:./foobar M:0x001a scp: debug3: Sent message SSH2_FXP_WRITE I:4 O:0 S:2 scp: debug3: SSH2_FXP_STATUS 0 scp: debug3: In write loop, ack for 4 2 bytes at 0 scp: debug3: Sent message SSH2_FXP_CLOSE I:3 scp: debug3: SSH2_FXP_STATUS 0 debug2: channel 0: read<=0 rfd 4 len 0 debug2: channel 0: read failed debug2: chan_shutdown_read: channel 0: (i0 o0 sock -1 wfd 4 efd 6 [write]) debug2: channel 0: input open -> drain debug2: channel 0: ibuf empty debug2: channel 0: send eof debug3: send packet: type 96 debug2: channel 0: input drain -> closed debug3: receive packet: type 98 debug1: client_input_channel_req: channel 0 rtype exit-status reply 0 debug3: receive packet: type 96 debug2: channel 0: rcvd eof debug2: channel 0: output open -> drain debug2: channel 0: obuf empty debug2: chan_shutdown_write: channel 0: (i3 o1 sock -1 wfd 5 efd 6 [write]) debug2: channel 0: output drain -> closed debug3: receive packet: type 97 debug2: channel 0: rcvd close debug3: channel 0: will not send data after close debug2: channel 0: almost dead debug2: channel 0: gc: notify user debug2: channel 0: gc: user detached debug2: channel 0: send close debug3: send packet: type 97 debug2: channel 0: is dead debug2: channel 0: garbage collecting debug1: channel 0: free: client-session, nchannels 1 debug3: channel 0: status: The following connections are open: #0 client-session (t4 r0 i3/0 o3/0 e[write]/0 fd -1/-1/6 sock -1 cc -1) debug3: send packet: type 1 Transferred: sent 3428, received 3156 bytes, in 0.3 seconds Bytes per second: sent 12806.6, received 11790.4 debug1: Exit status 0
失败场景Verbose日志
... debug1: Sending subsystem: sftp debug2: channel 0: request subsystem confirm 1 debug3: send packet: type 98 debug2: channel_input_open_confirmation: channel 0: callback done debug2: channel 0: open confirm rwindow 0 rmax 32768 debug3: send packet: type 1 client_loop: send disconnect: Broken pipe scp: Connection closed
已尝试的排查措施
- 使用
-O选项切换至原始scp协议而非sftp - 检查目标主机sshd配置中sftp-server是否可用且配置正确
- 将客户端MTU从1500降至1000
- 添加
-o ServerAliveInterval=600选项 - 移除目标主机
~/.ssh/authorized_keys中的客户端密钥
进一步排查与解决建议
1. 检查远程网络的防火墙/安全组规则
远程环境中,防火墙或云安全组可能在SSH会话建立后,阻断了sftp子系统的流量。对比本地虚拟机和远程主机的网络规则,确保22端口的双向流量完全放行(sftp流量在SSH隧道内,无需额外端口)。
2. 查看远程主机的sshd日志
登录远程CentOS 7.9主机,查看/var/log/secure日志,定位执行失败的时间段,查找是否有sshd相关的报错(比如sftp子系统启动失败、连接被强制断开的具体原因)。
3. 验证远程主机sftp-server的权限与SELinux限制
执行以下命令确认sftp-server的权限:
ls -l /usr/libexec/openssh/sftp-server
同时检查SELinux是否限制了sftp的运行:
ausearch -m avc -ts recent | grep sftp
如果有SELinux报错,可临时关闭SELinux测试(setenforce 0),或添加相应的SELinux规则。
4. 调整SSH窗口大小参数
失败日志中显示rwindow 0,可能是窗口大小协商异常导致管道中断。尝试在scp命令中添加窗口大小参数:
scp -vvv -o WindowSize=1024 foobar user@remote-host:
或在客户端~/.ssh/config中添加全局配置:
Host remote-host WindowSize 1024
5. 测试纯SSH会话稳定性
先建立SSH会话并保持:
ssh user@remote-host
然后在另一个终端执行scp命令,观察是否还会出现断开问题。如果纯SSH会话也不稳定,说明是底层网络或SSH连接的问题,而非scp/sftp本身。
6. 用rsync替代测试
执行rsync命令测试文件传输:
rsync -avvv foobar user@remote-host:
如果rsync能成功,说明问题出在scp/sftp的协议交互上;如果同样失败,可定位到更底层的网络或主机配置问题。
内容的提问来源于stack exchange,提问作者Francois
相关产品推荐
相关产品推荐

