sftp/scp 连接失败但 ssh 正常

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sftp/scp 连接失败但 ssh 正常相关的知识,希望对你有一定的参考价值。

参考技术A

最近遇到了通过 ssh 能正常访问服务器,但使用 sftp、scp 失败的情况,最终发现:
如果进行 Shell 初始化时,在为 非交互式会话 进行了输出( .profile , .bashrc , .cshrc 等),则 sftp / scp 在连接时可能会失败。这是因为这些输出使 sftp / scp 客户端发生了混淆。可以通过执行以下命令来验证 Shell 是否存在这个问题:

如果以上命令产生任何输出,那么需要修改 Shell 初始化。

参考资料:

(完)

SSH,SCP,SFTP命令汇总

ssh远程连接总结:


1:直接连接到远程主机上面,-p22代表ssh端口 root代表远程主机的用户名@172.16.1.31代表远程主机的IP地址。(直接切到远程主机上)

[[email protected] ~]# ssh -p22 [email protected]
[email protected]‘s password:
Last login: Mon Aug 14 23:04:42 2017 from m01
[[email protected] ~]#


2:直接调用远程主机的命令(不会切到机器上)

[[email protected] ~]# ssh -p22 [email protected] /sbin/ifconfig
[email protected]‘s password:
eth0      Link encap:Ethernet  HWaddr 00:0C:29:EB:DA:9F 
          inet addr:10.0.0.31  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feeb:da9f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:344 errors:0 dropped:0 overruns:0 frame:0
          TX packets:256 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:30665 (29.9 KiB)  TX bytes:29816 (29.1 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:EB:DA:A9 
          inet addr:172.16.1.31  Bcast:172.16.1.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feeb:daa9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:399 errors:0 dropped:0 overruns:0 frame:0
          TX packets:292 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:44077 (43.0 KiB)  TX bytes:44353 (43.3 KiB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:282 errors:0 dropped:0 overruns:0 frame:0
          TX packets:282 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:31196 (30.4 KiB)  TX bytes:31196 (30.4 KiB)

[[email protected] ~]#


3:当第一次ssh连接的时候,本地会产生一个密钥文件~/.ssh/known_hosts (多个密钥)


4.windows客户端通过ssh连接

windows下实现远程连接的软件一般有secureCRT,Putty,xshell


5. ssh客户端附带的远程拷贝scp命令。

scp的基本命令使用:scp -secure copy(remote file copy program)

每次都是全量拷贝,增量拷贝rsync

推:push (本地在前)

scp -P22 -r -p /tmp/oldboy [email protected]:/tmp

举例1:把本地/etc/hosts文件拷贝到远端主机172.16.1.31的/data目录中

[[email protected] ~]# scp -P22 /etc/hosts [email protected]:/data
[email protected]‘s password:
hosts                                                    100%  158     0.2KB/s   00:00   

举例2:把本地/data目录拷贝到远端主机172.16.1.31的/data目录中,-r代表拷贝目录,p代表保持目录属性。

[[email protected] data]# scp -P22 -rp /data [email protected]:/data   
[email protected]‘s password:
a.txt                                                    100%    0     0.0KB/s   00:00

#scp为远程拷贝文件或目录的命令

#P(大写,注意和ssh命令的不通)接端口,默认22端口时可以省略-P22

#-l 限制拷贝速度(企业案例,生产环境中一定要限速)

拉:远端在前

[[email protected] data]# scp -P22 -rp [email protected]:/data /data


ssh服务附带的sftp功能

ssh服务中有安全FTP功能,即通过ssh加密数据进行传输

windows客户端和linux服务器之间传输数据工具

1:  rz,sz(lrzsz)

2:  winscp  基于ssh,sftp

3:sfx(xshell)

4:sftp 基于ssh加密传输

5:samba,http,ftp,nfs

ftp工具:vsftp, proftpd, sftp


linux sftp客户端连接sftp服务器方法:

登录frp的方法就是sftp [email protected]如果ssh端口为52113则登录命令如下:

sftp -oPort=52113 [email protected] #特殊端口sftp连接命令

sftp不支持目录,如果想上传需要打包。

put:把本地传递到远端哪里(上传)

get:把远程传递到本地哪里(下载)

技术分享

技术分享


windows中sftp连接:

技术分享

1)put上传,后面要用“要上传的本地文件”

sftp> pwd          
/root
sftp> ls
anaconda-ks.cfg       install.log           install.log.syslog
sftp> put "D:\a.txt"
Uploading a.txt to /root/a.txt
  100% 0 bytes      0 bytes/s 00:00:00    
sftp> ls
a.txt                 anaconda-ks.cfg       install.log
install.log.syslog
sftp>

这样就把windows中D:\a.txt文件传到linux的root目录中了

2)get下载

技术分享







本文出自 “sandshell” 博客,请务必保留此出处http://sandshell.blog.51cto.com/9055959/1956271

以上是关于sftp/scp 连接失败但 ssh 正常的主要内容,如果未能解决你的问题,请参考以下文章

ssh2_scp_send 文件没有被复制,没有错误

SSH,SCP,SFTP命令汇总

Linux ssh,sftp,scp使用方法,以及怎样设置黑白名单

在带有 ssh、scp 或 sftp 的 jenkins 管道中使用 ssh 凭据

ssh scp sftp简易用法

【求助】SSH可以连接 SFTP连接失败