|
在一次使用 rsync 做同步服务时,明明已经正确添加了 accept rsync 服务端口(873)的 iptables 规则,却发现怎么都连不上。由于该规则简单且不会有误,也没有怀疑是 iptables 的问题,直到绕了个大圈子去排除了 rsync 的问题后,才将注意力重新集中在 iptables 上来。
先使用 iptables -L 命令查看现有规则:[root@localhost rsyncd]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp any
ACCEPT esp -- anywhere anywhere
ACCEPT ah -- anywhere anywhere
ACCEPT udp -- anywhere 224.0.0.251 udp dpt:mdns
ACCEPT udp -- anywhere anywhere udp dpt:ipp
ACCEPT tcp -- anywhere anywhere tcp dpt:ipp
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ftp
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:https
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 上面对于 INPUT 链来说,为它所写的所有规则都放在一个自定义的 RH-Firewall-1-INPUT 链中。
我们如果直接如下添加允许对 873 端口连接的规则:ptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT 或写成:ptables -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT 那么该条新增的规则会被添加在 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 之后。该条规则的意思是,任何进入的连接都会被拒绝 (REJECT ),并且它会响应(reject-with)一个 icmp-host-prohibited 的信息给客户端(这是主动式拒绝)。通过 wireshark 可以观察到这样的信息:
ICMP 消息的类型为 3 时表示目标不能到达,其不能到达的原因由 codes 部分给出,当其值为 10 时表示对方主机主动拒绝了这一请求。
之所以在添加相应的 accept 的规则后仍然被拒绝( reject-with icmp-host-prohibited),这跟规则的匹配方式(顺序)有关:规则在链接中遵循顺序匹配,一旦匹配,则往下不再匹配的规律。因此,当 rsync 的连接请求进来时,它在 RH-Firewall-1-INPUT 中从上到下寻找相应的匹配规则,且匹配在 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 这条规则之上,因此该匹配一旦发生,后面即使再添加“允许”的规则那也是没用的了。
清楚了上面所述后,我们可以将该条规则调整到 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited 这条规则之前,如可以修改 /etc/sysconfig/iptables 总的书写顺序:
这么做后就可以顺利的连接上 rsync 服务器了,别的服务连接也类似如此。
上面所提到的 RH-Firewall-1-INPUT 自定义链是 RedHat 或 CentOS Linux 里可能预装的规则链。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|