0基础linux运维教程 常见问题

Posted 山河玩转linux运维

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了0基础linux运维教程 常见问题相关的知识,希望对你有一定的参考价值。

很多的同学在学习rsync的时候都会遇到这样那样的问题,下面山河老师整理了一些问题,看以下的常见问题可以让你避免不必要的麻烦。 问题

protocol version mismatch -- is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(178) [sender=3.1.2]

分析与解决方案

# 1、原因是
rsync 在传输数据之前,会先与远端进行一次 ssh 登录认证,而当 .bashrc文件有输出的时候,rsync 客户端解析返回的数据包会出现混乱,于是乎就会出现文中开头提到的报错:客户端和远端的协议版本不兼容/不匹配了。
# 2、需要说明的是:
远端 sshd 进程是通过“bash –c”的方式来执行命令(即"非交互式的非登录shell" )
但在执行命令之前,ssh的那一次登录本身是“非交互式的登录shell”, 非交互式的登录shell (bash –l xxx.sh) 载入的信息列表及顺序如下:
/etc/profile
<sub>/.bash_profile ( -> </sub>/.bashrc -> /etc/bashrc)
<sub>/.bash_login
</sub>/.profile
$BASH_ENV
所以 ssh 的时候会载入“ <sub>/.bash_profile”,
让我们再来看一下 .bash_profile 的内容:
cat </sub>/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f <sub>/.bashrc ]; then
. </sub>/.bashrc # .bashrc 默认又会加载 /etc/bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
看到这儿,我想你大概明白了为什么 .bashrc 里有输出流会导致 rsync 传输失败了。
# 3、解决方案
既然都找到原因了,那怎么解决呢?
最简单的方案当然是直接删掉.bashrc文件输出的内容
但有同学会问,我本来就是要让用户登录的时候有登录提示的呀?现在为了 rsync 岂不是得禁掉?
那这里就又引入了另一个知识点:交互式shell和飞交互式shell、登录shell和非登录shell 的区别
什么是交互式的呢?就像我们平时通过命令行做事一样,你敲一个命令,终端解析执行完之后给你一个结果,这样一种交互式的形式。那么,平时我们接触的Shell基本上都是交互式的,如gnome-terminal打开一个Shell以及通过Ctrl+alt+1等切换过去的文本终端。交互式Shell下, "echo $-"返回的字符串中包含i,否则不包含。也可以通过在bash后面加-i参数打开一个交互式的Shell,具体可以看man bash。bash后面加-c参数执行命令打开的是非交互式Shell,可以用以下命令验证:
bash -c \'echo $-\' # 返回hBc
解释完交互式之后,继续解析本小节后半部分中的登录二字。登录Shell其实很好理解,就是我们平时通过用户名/密码才能登录的Shell,最典型的就是用Ctrl+alt+1切换过去的文本终端。如何区分登录Shell和非登录Shell呢,可以通过查看$0的值,登录Shell返回-bash,而非登录Shell返回的是bash。平时gnome-terminal打开的Shell就是非登录Shell。也可以通过在bash后面加--login参数打开一个登录Shell。
回到本小节开头的问题,咱们可以用如下方式去区分一个命令是处于登录shell还是非登录shell:
[[ $- == *i* ]] && echo \'This is interactive shell.\'
其中,$-中包含i意思是指当前的Shell是一个交互式(interactive)的Shell。
针对文中开头的问题,咱们加上如上的判断,就可以做到登录的时候有提示,同时 rsync 也不会报错了,可谓一举两得。
好了,今天的内容就到这儿了,其实 shell 作为一门古老的编程语言以及随着 linux 版本的多样化发展、不断的演变,”坑“很多,却也值得让人细细探索~

其他常见错误

1.报错:
[root@web01 <sub>]# rsync -avz 1.txt rsync_backup@172.16.1.41::backu
@ERROR: Unknown module \'backu\'
rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]
#原因:模块名字写错了
2.报错:
[root@web01 </sub>]# rsync -avz 1.txt rsync_backu@172.16.1.41::backup
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]
#原因:
1)虚拟用户写错了
2)虚拟用户密码写错了
3)密码文件权限不对,一定是600
3.报错:
[root@web01 <sub>]# rsync -avz 1.txt rsync_backup@172.16.1.41::backup
rsync: failed to connect to 172.16.1.41 (172.16.1.41): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]
#原因:开启了防火墙
4.报错:
[root@web01 </sub>]# rsync -avz 1.txt rsync_backup@172.16.1.41::/backup
ERROR: The remote path must start with a module name not a /
rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]
#原因:::后面跟的时模块名
5.报错:
[root@web01 <sub>]# rsync -avz 1.txt rsync_backup@172.16.1.41::backup/
sending incremental file list
rsync: delete of stat xattr failed for "1.txt" (in backup): Permission denied (13)
sent 47 bytes received 107 bytes 308.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
#原因:
1)服务端真实目录权限不足
2)selinux没有关闭
6.报错:
[root@web01 </sub>]# rsync -avz 1.txt rsync_backup@172.16.1.41::backup/
rsync: failed to connect to 172.16.1.41 (172.16.1.41): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]
#原因:服务端服务没有启动
7.报错:
[root@web01 ~]# rsync -avz 1.txt rsync_backup@172.16.1.41::backup/
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]
#原因:服务端没有模块指定的目录

以上是关于0基础linux运维教程 常见问题的主要内容,如果未能解决你的问题,请参考以下文章

0基础linux运维教程 安装以及rsync的传输方式与工作模式

0基础linux运维教程 rsync的优缺点

0基础linux运维教程 共享存储之nfs实践及要点

0基础linux运维教程 共享存储之nfs介绍

0基础linux运维教程 Rsync结合inotify实现数据实时同步

挑战0基础变身高薪 Linux 运维工程师!