免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 11309 | 回复: 11

[系统安全] [已解决]求助:centos7下firewalld规则编写问题。 [复制链接]

论坛徽章:
0
发表于 2017-10-26 15:51 |显示全部楼层
本帖最后由 zhxd 于 2017-10-31 16:05 编辑

现在三台主机A(192.168.0.1)、B(192.168.0.2)、C(192.168.0.3),均为centos7系统,且主机B、C的防火墙均已禁用,在同一局域网内。在A主机上开启了firewalld防火墙,现在想编写firewalld规则,禁止B以外的其他主机ping通主机A。
firewall-cmd编写了如下规则:
# firewall-cmd --zone=public --add-rich-rule "rule family=ipv4 source address=192.168.0.2/24 invert=true protocol value=icmp reject" --permanent
# firewall-cmd --complete-reload
# # firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: ssh dhcpv6-client
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
        rule family="ipv4" source NOT address="192.168.0.2/24" protocol value="icmp" drop
# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-10-26 23:34:05 CST; 19min ago
     Docs: man:firewalld(1)
Main PID: 1203 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─1203 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Oct 26 23:37:55 localhost.localdomain firewalld[1203]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
Oct 26 23:37:55 localhost.localdomain firewalld[1203]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
Oct 26 23:37:55 localhost.localdomain firewalld[1203]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
Oct 26 23:37:55 localhost.localdomain firewalld[1203]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
Oct 26 23:38:43 localhost.localdomain firewalld[1203]: WARNING: ICMP type 'beyond-scope' is not supported by the kernel for ipv6.
Oct 26 23:38:43 localhost.localdomain firewalld[1203]: WARNING: beyond-scope: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
Oct 26 23:38:43 localhost.localdomain firewalld[1203]: WARNING: ICMP type 'failed-policy' is not supported by the kernel for ipv6.
Oct 26 23:38:43 localhost.localdomain firewalld[1203]: WARNING: failed-policy: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.
Oct 26 23:38:43 localhost.localdomain firewalld[1203]: WARNING: ICMP type 'reject-route' is not supported by the kernel for ipv6.
Oct 26 23:38:43 localhost.localdomain firewalld[1203]: WARNING: reject-route: INVALID_ICMPTYPE: No supported ICMP type., ignoring for run-time.

为什么主机B、C依然能够ping通主机A呢?

请问我的规则哪里有问题,应该怎么写呢?

论坛徽章:
0
发表于 2017-10-26 16:37 |显示全部楼层

不会firewall-cmd
能不能列一下iptables的规则?iptables-save

论坛徽章:
0
发表于 2017-10-26 16:41 |显示全部楼层
回复 2# marsaber

谢谢!我这是firewalld,不是iptables。

论坛徽章:
0
发表于 2017-10-26 16:44 |显示全部楼层
回复 3# zhxd

在RHEL7里有几种防火墙共存:firewalld、iptables、ebtables,默认是使用firewalld来管理netfilter子系统,不过底层调用的命令仍然是iptables等。
firewalld跟iptables比起来至少有两大好处:
1、firewalld可以动态修改单条规则,而不需要像iptables那样,在修改了规则后必须得全部刷新才可以生效;
2、firewalld在使用上要比iptables人性化很多,即使不明白“五张表五条链”而且对TCP/IP协议也不理解也可以实现大部分功能。
firewalld跟iptables比起来,不好的地方是每个服务都需要去设置才能放行,因为默认是拒绝。而iptables里默认是每个服务是允许,需要拒绝的才去限制。

firewalld自身并不具备防火墙的功能,而是和iptables一样需要通过内核的netfilter来实现,也就是说firewalld和 iptables一样,他们的作用都是用于维护规则,而真正使用规则干活的是内核的netfilter,只不过firewalld和iptables的结构以及使用方法不一样罢了。

论坛徽章:
0
发表于 2017-10-26 16:48 |显示全部楼层
回复 4# marsaber

这篇文章我看过。我也尝试过像写iptables那样先写允许,再写禁止,但是执行后,所有的都ping不通主机A了。

论坛徽章:
0
发表于 2017-10-26 17:05 |显示全部楼层
测试环境,主机A:192.168.1.10,主机B:192.168.1.11,主机C:192.168.1.101
需求:B可以ping通A,但是ping不通A。
在主机A上:
#!/bin/sh

/usr/sbin/iptables -F
/usr/sbin/iptables -X
/usr/sbin/iptables -P INPUT DROP
/usr/sbin/iptables -P FORWARD ACCEPT
/usr/sbin/iptables -P OUTPUT ACCEPT
/usr/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
/usr/sbin/iptables -A INPUT -p icmp -s 192.168.1.11 -j ACCEPT
我这边试验了一下,是OK的。
说明:
我习惯性的把iptables的所有规则清除掉,然后重写规则,另外我安装了iptables-services软件包以实现iptables规则的保存(service iptables save)

论坛徽章:
0
发表于 2017-10-26 17:16 |显示全部楼层
回复 6# marsaber

哥!谢谢你热心的回复。
我说了,我用的是firewalld,不是用的iptables。
iptables的规则在firewalld上不实用(至少在我这个情况下是行不通的)。

论坛徽章:
0
发表于 2017-10-26 17:21 |显示全部楼层
本帖最后由 marsaber 于 2017-10-26 17:23 编辑

https://linux.cn/article-6315-1.html
在后台, firewalld 和 iptables 服务都通过相同的接口来与内核中的 netfilter 框架相交流,这不足为奇,即它们都通过 iptables 命令来与 netfilter 交互。然而,与 iptables 服务相反, firewalld 可以在不丢失现有连接的情况下,在正常的系统操作期间更改设定。


主要是没有研究过firewalld,但是二者是相通的呀,只是操作的壳不一样罢了。

论坛徽章:
0
发表于 2017-10-26 17:23 |显示全部楼层
如果就是想用firewalld,你就检查一下你的规则,肯定是规则哪里出问题了。

论坛徽章:
0
发表于 2017-10-26 17:31 |显示全部楼层
回复 8# marsaber

好的,我再看看。谢谢了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP