linux防火墙规则配置教程,Linux防火墙常用规则配置

当使用firewalld-cmd添加防火墙规则时,它实际上是将配置转换成iptables规则后,再应用到系统中。

设有如下网络拓扑:

内网internal (172.12.0.100)br1--linux---br0---external(10.10.11.250)

1. 查询系统中的zone信息

firewall-cmd --get-zones

block dmz drop external home internal public trusted work

zone:它是安全域的范围,就类似于Window上的域网络,工作网络,家庭网络,Internet网络等,不同的安全作用域其安全级别不同,安全程度不同,家庭zone的安全规则就是最宽松的。

trusted(信任) ---可接受所有的网络连

home(家庭) ---- 用于家庭网路,默认仅接受ssh, mDNS, ipp-client,dhcpv6-client服务连

internal(内部) --- 用于内部网络,默认仅接受ssh, mdns, ipp-client,dhcpv6-client服务连

work(工作) -- 用于工作区域,默认仅接受ssh, dhcpv6-client服务连

public(公共) --- 在公共区域使用,默认仅接受ssh,dhcpv6-client服务连,为firewalld的默认区域

external(外部) -- 出去的IPv4网络连,通过此区域或伪装(source nat)或是转发,仅接受ssh服务连接

dmz(非军事区) --- 仅接受ssh服务连

block(限制) ---拒绝所有的网络连接

drop(丢弃) ---任何接收的网络数据包都被丢弃,没有任何回复

2. 查询防火墙状态和配置信息

Firewall运行状态

[root@localhost ~]# firewall-cmd --state

Running

Firewall接口分配信息

[root@localhost ~]# firewall-cmd --list-all

public (active)

target: default

icmp-block-inversion: no

interfaces: eth0 ens4

….

开放端口信息

[root@localhost ~]# firewall-cmd --list-port

500/udp 4500/udp

3. 更改接口所属区域

当前接口所属区域

firewall-cmd --get-active-zones

public

interfaces: eth0 ens4

更改接口区域

firewall-cmd --permanent --zone=external --change-interface=eth0

firewall-cmd --permanent --zone=internal --change-interface=ens4

更改后的区域归属

firewall-cmd --get-active-zones

internal

interfaces: ens4

external

interfaces: eth0

4. 区域中添加、删除接口

将接口加入到区域中

firewall-cmd --permanent --zone=internal --add-interface=eth0

将接口从区域中删除

firewall-cmd --permanent --zone=internal --remove-interface=eth0

参数permanent指的是配置将永久有效

5. 添加、删除服务或端口

在公共区域添加邮件服务

firewall-cmd --permanent --zone=public --add-service=smtp

在公共区域删除邮件服务

firewall-cmd --permanent --zone=public --remove-service=smtp

在所有区域添加、删除udp端口67

firewall-cmd –permanent –add-port=67/udp

firewall-cmd –permanent -remove-port=67/udp

6. 设置、删除网络地址到指定的区域

firewall-cmd --permanent --zone=internal --add-source=172.12.0.0/24

firewall-cmd --permanent --zone=internal --remove-source=172.12.0.0/24

7. 添加、删除DNAT转发

在本机所有端口上接收到TCP 4522的报文时,将报文的目的地址和端口转换为内部网络的172.12.0.11:22端口

firewall-cmd --add-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent

删除DNAT规则

firewall-cmd --remove-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 --permanent

添加DNAT规则到区域

firewall-cmd --zone=internal --add-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent

从区域中删除DNAT规则

firewall-cmd --zone=internal --remove-forward-port=port=4522:proto=tcp:toport=22:toaddr=172.12.0.11 –permanent

8. SNAT规则添加、删除

设置IP地址伪装(SNAT)

firewall-cmd --zone=external --add-masquerade –permanent

删除SNAT规则

firewall-cmd --zone=external --remove-masquerade –permanent

将source为192.168.2.0网段来的数据包伪装成external(即ens4)的地址

firewall-cmd --permanent --zone=external --add-rich-rule=‘rule family=ipv4 source address=192.168.2.0/24 masquerade’

将source为192.168.2.0网段来的数据包伪装成external(即ens4)地址的规则移除

firewall-cmd --permanent --zone=external --remove-rich-rule=‘rule family=ipv4 source address=192.168.2.0/24 masquerade’

9. 查询端口是否打开

firewall-cmd --query-port=80/tcp

10. 重载Firewall使配置生效

firewall-cmd --reload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值