协议版本不匹配--你的shell干净吗?

协议版本不匹配--你的shell干净吗?

按照此处给出的说明进行 rsync 备份时:http://troy.jdmz.net/rsync/index.html

我收到错误“协议版本不匹配 - 你的 shell 干净吗?”

我在某处读到,我需要静音提示 (PS1="") 和 motd (.hushlogin) 显示来解决这个问题。我已经这样做了,提示和登录横幅 (MOTD) 不再出现,但运行时仍然会出现错误:

rsync -avvvz -e "ssh -i /home/thisuser/cron/thishost-rsync-key" remoteuser@remotehost:/remote/dir /this/dir/

ssh 客户端和 sshd 服务器都使用该协议的版本 2。

可能是什么问题呢?

[编辑] 我发现http://www.eng.cam.ac.uk/help/jpmg/ssh/authorized_keys_howto.html 它指出有时需要“使用 -2 标志强制使用 v2 来 ssh 或 slogin

 ssh -2 -i ~/.ssh/my_private_key remotemachine"

目前还不清楚这是否解决了问题,因为我认为我是在错误发生改变后才做出这一改变的,但事实是错误已经演变成其他错误。当我了解更多信息时,我会更新此内容。我一定会尝试在 emacs shell 中运行它的建议 -

答案1

您的某个登录脚本 (.bashrc/.cshrc/etc.) 可能正在向终端输出数据(但不应该输出)。这会导致 ssh 在连接并准备复制时出错,因为它开始接收它不期望的额外数据。删除启动脚本中生成的输出。

您可以在 bashrc 中使用以下代码来检查您的终端是否是交互式的并且仅输出文本。其他 shell 也存在等效的东西:

if shopt -q login_shell; then
    [any code that outputs text here]
fi

或者像这样,因为当 shell 是交互式的时候,特殊参数-包含:i

if echo "$-" | grep i > /dev/null; then
    [any code that outputs text here]
fi

更多信息请参阅:通过 ssh 从 Linux 到 Windows sbs 2003 进行 rsync 协议不匹配

为了诊断这个问题,请确保您通过 ssh 登录到主机时获得以下输出:

USER@HOSTNAME's password: 
Last login: Mon Nov  7 22:54:30 2011 from YOURIP
[USER@HOSTNAME ~]$ 

如果您收到任何换行符或其他数据,则表明正在发送额外的输出。您可以将 .bashrc/.cshrc/.profile/etc. 文件重命名为其他名称,这样它们就不会输出额外的输出。当然,仍然有系统文件可能导致这种情况。在这种情况下,请与您的系统管理员核实系统文件是否不输出数据。

答案2

解决此问题:

对于 ssh 连接,有一个简单的方法可以测试 shell 是否干净:从 ssh 连接运行命令,而不是启动交互式 shell。该false命令将立即终止而不产生任何输出,因此这是一个很好的测试:

bash$ ssh remotehost false
Enter passphrase for key '/home/user/.ssh/my_private_key': 
bash$

如果该命令行产生任何输出,则您的某个启动脚本就是罪魁祸首:

bash$ ssh remotehost false
Enter passphrase for key '/home/user/.ssh/my_private_key': 
Welcome to RemoteHost!

This system is company property and is provided for authorized use only, 
as set forth in applicable written policies. Unauthorized use is prohibited 
and may be subject to discipline, civil suit and criminal prosecution.
 
Welcome back - You last logged in 16 days ago...
bash$

如果出现此错误,需要检查的另一件事是 rsync 是否已安装并且可通过 ssh 定位:

bash$ ssh remotehost "rsync --version"
Enter passphrase for key '/home/user/.ssh/my_private_key': 
rsync  version 3.0.9  protocol version 30
Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.
bash$

如果 rsync 不在路径中,您将看到类似以下内容:

bash$ ssh remotehost "rsync --version"
Enter passphrase for key '/home/user/.ssh/my_private_key': 
bash: rsync: command not found
bash$

您可以通过安装 rsync 来解决此问题,或者如果它已安装在异常位置,则将位置传递给 rsync 命令行:

rsync -avvvz -e "ssh -i /home/thisuser/cron/thishost-rsync-key" \
    --rsync-path="/usr/local/bin/rsync" \
    remoteuser@remotehost:/remote/dir /this/dir/

答案3

这通常是由于 shell 的登录程序在非交互式 shell 上输出内容而导致的。您可以通过以下方式测试是否是这种情况:

ssh username@host "/bin/true" > testfile
ls -l testfile

如果 testfile 不为 0 字节,则问题在于您的 shell 正在输出某些内容。检查/etc/profile.profile.bashrc.cshrc等。如果是,您可以将其更改为检查您的终端是否是交互式的并且仅在 bashrc 中使用以下代码输出文本。其他 shell 也存在等效的东西:

if shopt -q login_shell; then
    [any code that outputs text here]
fi

或者像这样,因为当 shell 是交互式的时候,特殊参数-包含:i

if echo "$-" | grep i > /dev/null; then
    [any code that outputs text here]
fi

但是,如果测试文件实际上是 0 字节,则说明您的 shell 运行正常,但可能您使用的 rsync 版本非常旧。您可以告诉客户端(假设它是较新的一端)不要宣传版本太高,以免旧版 rysnc 服务器无法识别。您可以使用选项执行此操作--protocol=。就我而言,使用--protocol=30成功了。

如果仍有问题,请尝试以 rsysnc 连接的用户身份登录 ssh,然后尝试运行rsync --version以查看 shell 是否可以找到 rsync。如果出现“未找到命令”提示,则 rsync 可能未安装在您连接的计算机上,或者不在路径中。Rsync 确实有用于指定远程端路径的选项,请阅读手册页。

答案4

我得到的原因protocol version mismatch -- is your shell clean?只是因为我没有在其他结束了。sudo yum install rsync问题解决了。

相关内容