sshfs 报错 "远程主机已断开连接"

我正在尝试使用以下命令将一个 sftp 连接挂载到一个文件夹中,但是出现了错误。
~$ sshfs admin@example.com:/ testfo
remote host has disconnected
如果我通过 SSH 登录到系统并使用 localhost 重复执行该命令,就会出现相同的错误。这条命令在另一台机器上也可以运行,所以问题肯定出在服务器上。
~$ cat /var/log/auth.log

[...]

May 24 22:49:43 example sshd[20095]: Accepted publickey for admin from 24.111.222.33 port 47086 ssh2: RSA ad:xx:6e:xx:14:xx:bd:b5:xx:cb:66:xx:xx:xx:a3:ac
May 24 22:49:43 example sshd[20095]: pam_unix(sshd:session): session opened for user admin by (uid=0)
May 24 22:49:43 example systemd-logind[812]: Removed session 60.
May 24 22:49:43 example systemd-logind[812]: New session 61 of user admin.
May 24 22:49:44 example sshd[20143]: Received disconnect from 24.203.164.45: 11: disconnected by admin
May 24 22:49:44 example sshd[20095]: pam_unix(sshd:session): session closed for user admin

由于我在调试类似问题时发现,~/.ssh目录的所有者是管理员。

供将来参考的附加信息:

问题并不在于SSH本身,而是在于SFTP。这表现为SSH连接正常工作,但SFTP始终失败。尝试进行SFTP会导致Received unexpected end-of-file from SFTP server

问题与登录脚本输出的字符串(例如~/.bashrc)无关。

该问题存在于所有用户(包括root)。

以下是我的sshd配置文件(/etc/ssh/sshd_config):

# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
已安装软件包()

дҪ иғҪзӣҙжҺҘдҪҝз”Ёsftpеҗ—пјҹдҪ зҡ„жңҚеҠЎеҷЁдё»зӣ®еҪ•дёӢзҡ„~/.bashrcжҲ–е…¶д»–еҗҜеҠЁж–Үд»¶зҡ„еҶ…е®№жҳҜд»Җд№Ҳпјҹ - Jakuje
@Jakuje 嗯...问题似乎确实出在sftp上。使用FileZilla尝试时出现了“从SFTP服务器接收到意外的文件末尾”的错误。 - Slava Knyazev
  1. 你能够使用sftp登录不同的用户吗?
  2. 服务器上的sshd_config配置文件是什么样子的?
  3. 你的~/.bashrc文件是什么样子的?
- Jakuje
@Jakuje 相同 2) 没有.. 所有用户都出现相同的错误。3) http://pastebin.com/raw/ujNk3fVR 4) bashrc 没有相关内容,但在这里可以找到:http://pastebin.com/raw/mUD3DWmG - Slava Knyazev
你从which sftp-server得到什么?子系统应该是Subsystem sftp /usr/lib/openssh/sftp-server。你是否已经安装了sftp-server?使用apt-get install openssh-sftp-server进行安装。 - Jakuje
@Jakuje openssh-sftp-server已安装,但是which命令没有返回任何内容。 - Slava Knyazev
@DavidFoerster 完成了! - Slava Knyazev
如果服务器的主机密钥发生了变化,就会出现这种情况。尝试使用常规的ssh连接,并查找错误。 - Edd Barrett
6个回答

老问题,但这是我遇到的第一个与此问题相关的。 我的问题是服务器需要密钥验证,但我在使用sudo命令并指定-o IdentityFile=~/.ssh/id_rsa时,意味着~被扩展为root的主目录,而不是我的主目录。 指定完整路径有效,并且我想使用$HOME也可以(因为它会在之前扩展)。

2谢谢,我能够通过这个提示安装上了。 - Bemmu

你的 `sshd_config` 中的 `Subsystem` 值是错误的。 应该是 `Subsystem sftp /usr/lib/openssh/sftp-server` 或者 `internal-sftp`。尝试修改 `/etc/ssh/sshd_config` 为这个值,重新启动 ssh 服务并再次尝试。


我不确定它是否有帮助,但我曾经有过类似的问题。
remote host has disconnected
经过一番搜索和浏览后,我意识到实际上我是通过不同的端口连接了ssh。 所以,举个例子,我必须通过ssh连接(假设端口号为1234):
ssh username@example.com -p 1234
当端口号为22时,使用标准的ssh替代。因此,在sshfs连接中也必须使用相同的端口号。
sshfs username@example.com:/ ~/testfolder -p 1234
解决了我的问题。

非常感谢。它有效果 :) - Jamaluddin Rumi

还有一个发生在我身上的原因是,dropbearmulti二进制文件本身缺少了字符串/usr/libexec/sftp-server,这个字符串在DD-WRT的构建33600左右丢失了。检查一下该二进制文件是否提到了这个文件,如果没有,即使存在也无法工作。我不得不使用33525版本的二进制文件,并创建一个启动器来关闭正常的有问题的dropbear,然后运行这个版本。你可以创建一个名为dropbear的符号链接指向dropbearmulti。你可以使用stopservice sshd停止当前的dropbear,然后运行可用的版本。在ps中查看正确的参数是什么样子的。 最好将它放在jffs上(或解压到/tmp),这样你仍然可以umount任何驱动器。

如果您能够通过ssh直接连接到主机:
ssh xxx.xxx.xxx.xxx
你可能会被提示保存密钥,你需要输入YES,而不仅仅是Y。然后,你应该被要求输入远程机器上用户的用户名和密码。 使用你尝试进行sshfs连接的那个用户,将结果回复给我。 如果你收到连接被拒绝的消息,我猜测你没有在远程计算机上安装SSH。可以通过在远程计算机上运行以下命令来安装OpenSSH:
sudo apt install openssh-server

1是的,我能够通过SSH连接到它。"如果我通过SSH登录系统,会出现相同的错误..." 这里暗指了它。据我所知,服务器已经正确设置好了。 - Slava Knyazev
好的,也许我误读了你的评论,你能成功使用SSH登录吗?还是像我在这里阅读到的那样:"如果我通过SSH登录系统,出现相同错误。" - Christopher Angulo-Bertram
我可以正常地通过SSH连接到它。 - Slava Knyazev
好的,看了你的命令行,也许你多了一个空格?我在网上找到了这个链接,我会发给你。 sshfs <用户名>@<IP地址>:/远程路径 ~/remoteserv。我在这里找到了它:http://www.howtogeek.com/howto/ubuntu/how-to-mount-a-remote-folder-using-ssh-on-ubuntu/。还有一堆其他的指令,但是根据你原来的问题,你的命令似乎多了一个空格。 - Christopher Angulo-Bertram
我也看不到你是如何将本地系统连接为目录的。以下是你的代码和我的代码: sshfs admin@example.com:/ testfo - Christopher Angulo-Bertram
我的代码: sshfs <username>@<ipaddress>:/remotepath ~/remoteserv - Christopher Angulo-Bertram
你在命令中看到testfo之前的额外空格了吗? - Christopher Angulo-Bertram
Stack Exchange 网络站对解决方案和评论之间的区别非常严格。评论往往成为垃圾信息和与 Stack Exchange 模式不符的激烈讨论的源头,因此发布评论需要一定的声望,以显示您对该网站理念的了解。这并不是为了刁难新用户,而是为了保持网站的高质量。(请参阅为什么会删除某些答案? - David Foerster