10

我正在尝试在两台服务器之间同步文件

rsync -avlzp /source user@server:/destination

但相反,我收到错误说明

bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(635) [sender=3.0.2]

但 rsync安装在两台服务器上。我究竟做错了什么?我也试过

rsync -av -e "ssh -l ssh-user" /source server:/destination

结果相同。

我主要是尝试使用 rsync 以便它只复制存在差异的差异......

谢谢

4

2 回答 2

17

当运行交互式 shell 时,rsync 可能在远程机器上的路径中,但在通过 ssh(使用非交互式 shell)运行 rsync 时不会。例如,.bashrc 通常只为交互式 shell 运行,因此如果在此处定义了路径,则rsync --server在远程端运行时将不会对其进行解析。试试 /etc/profile。有关详细信息,请参阅bash(1) 。

您可以使用--rsync-path指定远程计算机上 rsync 二进制文件的路径。

于 2009-04-08T22:00:51.777 回答
7

关于这个问题有很多很好的信息,包括上面的答案。但就我而言,我是源机器和目标机器的管理员,我希望了解发生了什么以及哪些“正确”的配置选项可以解决这个问题。我还没有完全成功,但其他人似乎可能会发现这些信息很有用。

正如上面的答案所述,当运行交互式 shell 时,rsync 可能位于远程机器上的路径中,但在通过 ssh 运行 rsync 时则不会。正如 Siddesh 在http://siddesh-bg.blogspot.com/2009/02/rsync-command-not-found-error-even.html中指出的那样,这可以通过从本地 shell 显式指定远程 rsync 的路径来解决:

rsync -av --rsync-path=/usr/local/bin/rsync -e "ssh -l ssh-user" /source server:/destination

但作为管理员,我想解决问题,而不仅仅是解决问题。OldSchool 在https://groups.google.com/group/comp.unix.solaris/browse_thread/thread/0a06a4d382d752d8?pli=1上发布了有用的故障排除方法

他建议你找出远程机器上正在为你运行的 shell,然后在那里建立了什么 PATH:

ssh user@host 'echo $SHELL' 
ssh user@host 'echo $PATH' 

Tom Feiner 在为什么 SSH 远程命令获得的环境变量比手动运行时少?讨论SSH命令执行shell的环境搭建。

OldSchool 还指出,“man sshd_config”提供了一些关于 ssh 会话如何获取环境的信息。sshd_config 设置“PermitUserEnvironment”可以设置为允许用户在服务器端的 ~/.ssh/environment 和 AuthorizedKeysFile 文件中的环境选项由 sshd 处理。默认为否。

因此,使用默认 sshd_config 设置,路径将由您的默认 shell 构建。如果您的 ssh 命令请求交互式会话 (ssh user@host),那么在远程服务器上,默认 shell 将执行交互式登录,这可能会导致您期望的 PATH。但是,如果您正在为 rsync (rsync -av --rsync-path=/usr/local/bin/rsync -e "ssh -l ssh-user" /source server:/destination) 启动 ssh 会话,那么远程服务器会给出你是一个 SSH 命令执行 shell,它是一个非交互式 shell。在我的例子中,远程非交互式 shell (bash) 没有执行 /etc/profile,因为它只对交互式 shell 或带有 --login 选项的非交互式 shell 执行此操作。我可以这样强迫它:

ssh user@host 'echo $PATH;source /etc/profile;echo $PATH' 

我还研究了 Daniel Barrett 和 Richard Silverman 的“SSH The Secure Shell”(O'Reilly 2001)并在 /etc/ssh/sshrc 中设置了一个路径,我希望它可以使 rsync 可用于 SSH 命令执行 shell,但是 /etc/ssh /sshrc 在调用用户 shell 或命令之前执行,显然它的环境没有传递给该 shell 或命令。

在这一点上,我将对 rsync 命令行选项感到满意,即:

rsync -av --rsync-path=/usr/local/bin/rsync -e "ssh -l ssh-user" /source server:/destination

因为我担心如果我更改 sshd_config 设置“PermitUserEnvironment”,那么我的服务器上的安全审核可能会失败。在我对我们组织的安全审计有了更多经验之后,我可能会重新审视这个问题。

于 2011-12-30T22:13:25.827 回答