详述Linux中Firewalld高级配置的使用

发布时间:2025-12-25 点击:2
服务器
ip伪装与端口转发
firewalld支持两种类型的网络地址转换
ip地址伪装(masquerade)
可以实现局域网多个地址共享单一公网地址上网
ip地址伪装仅支持ipv4,不支持ipv6
默认external区域启用地址伪装
端口转发(forward-port)
也称为目的地址转换或端口映射
通过端口转发,指定ip地址及端口的流量将被转发到相同计算机上的不同端口,或者转发到不同计算机上的端口
地址伪装配置
为指定区域增加地址伪装功能
firewall-cmd [--permanent] [--zone= zone] --add-masquerade [--timeout seconds] //--timeout=seconds:在一段时间后自动删除该功能为指定区域删除地址伪装功能
firewall-cmd [--permanent] [--zone= zone] --remove-masquerade查询指定区域是否开启地址伪装功能
firewall-cmd [--permanent] [--zone=zone] --query-masquerade端口转发配置
列出端口转发配置
firewall-cmd [--permanent] [--zone=zone] --list-forward-ports添加端口转发规则
firewall-cmd [--permanent] [--zone=zone] --add-forward-port=port=portid[-portid]:proto=protocol[:toport-portid[-portid]][:toaddr-address[/mask]][--timeout=seconds]删除端口转发规则
firewall-cmd [--permanent] [--zone=zone] --remove-forward-port=port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]]查询端口转发规则
firewall-cmd [--permanent] [--zone=zone] --query-forward-port-port-portid[-portid]:proto=protocol[:toport-portid[-portid]][:toaddr=address[/mask]]firewalld直接规则
直接规则(direct interface)
允许管理员手动编写的iptables、ip6tables和ebtables 规则插入到firewalld管理的区域中
通过firewall-cmd命令中的–direct选项实现
除显示插入方式之外,优先匹配直接规则
自定义规则链
firewalld自动为配置”了 规则的区域创建自定义规则链
in 区域名 deny: 存放拒绝语句,优先于in 区域名 _allow 的规则
in 区域名 allow: 存放允许语句
允许tcp/9000端口的入站流量
irewall-cmd --direct --add-rule ipv4 filter in work_ allow 0 -p tcp --dport 9000 j accept in work_ allow: 匹配work区域的规则链
0:代表规则优先级最高,放置在规则最前面
可以增加 –permanent选项表示永久配置
查询所有的直接规则
firewall-cmd --direct --get-all-rulesipv4 filter in_ work _allow 0 -p tcp --dport 9000 -j accept可以增加 –permanent选项表示查看永久配置
firewalld富语言规则
富语言(rich language)
表达性配置语言,无需了解iptables语法
用于表达基本的允许/拒绝规则、配置记录(面向syslog和auditd)、端口转发、伪装和速率限制
rule [family=<rule family>] [ source address=<address> [invert true] ] [ destination address=<address> [invert=true] ] [ <element> ] [ log [prefix=<prefix text>] [level=<log level>] [limit value=rate/duration] ] [ audit ] [ acceptlrejectldrop ]理解富语言规则命令
firewall-cmd处理富语言规则的常用选项
选项说明 -add-rich-rule= \\\’rule\\\’
向指定区域中添加rule,如果没有指定区域,则为默认区域
–remove-rich-rule= \\\’rule\\\’
从指定区域中删除rule,如果没有指定区域,则为默认区域
–query-rich-rule= \\\’rule\\\’
查询rule是否已添加到指定区域,如果未指定区域,则为默认区域。<br/>规则存在,则返回0,否则返回1
–list-rich-rules
输出指定区域的所有富规则,如果未指定区域,则为默认区域
已配置富语言规则显示方式
firewall-cmd --list-allfirewall-cmd --list-all-zones--list-rich-rules富语言规则具体语法
source、destination、 element、 service、 port、 protocol、icmp-block、masquerade、 forward-port、 log、 audit、acceptlreject|drop
拒绝从192.168.8.101的所有流量
firewall-cmd --permanent --zone=work --add-rich-rule=\\\'rule family=ipv4 source address=192.168.8.101/32 reject\\\'ddress选项使用source或destination时,必须用family= ipv4 | ipv6
接受192.168.1.0/24子网端口范置8000-9000的tcp流量
firewall-cmd --permanent --one=work --add-rich-rule=\\\'rule family=ipv4 source address=192.168.1.0/24 port port=8000-9000 protocol=tcp accept\\\'丢弃所有icmp包
firewall-cmd --permanent --add-rich-rule=\\\'rule protocol value=icmp drop\\\'接受来自192.168.8.1的http流量,并记录日志
firewall-cmd --add-rich-rule=\\\'rule family=ipv4 source address=192.168.8.1/32 service name=http log level=notice prefix= new http”limit value 3/s accept\\\'以192.168.8.1访问http,并观察/var/log/messages
apr 16 17:09:55 server kernel: new http in=ens33 out=mac=00:0c:29:69:01:c4:00:50:56:c0:00:08:08:00 src=192.168.8.1 dst=192.168.8.131len=52 tos=0xoo prec=0x00 ttl =64 id=20582 df proto=tcp spt=65289 dpt=80window=8192 res=0x00 syn urgp=0apr 16 17:09:55 server kernel: new http in=ens33 out=mac=00:0c:29:69:01:c4:00:50:56:c0:00:08:08:00 src=192.168.8.1 dst=192.168.8.131len=52 tos=0x0o prec=0x0o ttl =64 id=20590 df proto=tcp spt=65291 dpt=80window=8192 res=0x00 syn urgp=0apr 16 17:09:55 server ke

网站被封了域名可以用吗?怎么做?
5月末周全球域名注册商(国际域名)新增注册量TOP12
discuz 用什么虚拟主机
腾讯租云服务器
跑代码的云服务器多少钱
虚机系统自动检测被挂木马
云服务器配置数据库
哪些企业网站模板才合适企业用?