linux 简单记录8 --iptables 与 firewalld 防火墙

Posted 春困秋乏夏打盹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux 简单记录8 --iptables 与 firewalld 防火墙相关的知识,希望对你有一定的参考价值。

iptables 与 firewalld 防火墙

iptables 与 firewalld 都不是真正的防火墙,它们都只是用来定义防火墙策略的防火墙管理工具而已, 或者说, 它们只是一种服务。
iptables服务会把配置好的防火墙策略交由内核层面的 netfilter 网络过滤器来处理,
firewalld 服务则是把配置好的防火墙策略交由内核层面的 nftables 包过滤框架来处理。
防火墙会从上至下的顺序来读取配置的策略规则,在找到匹配项后就立即结束匹配工作并去执行匹配项中定义的行为(即放行或阻止)。
iptables 服务把用于处理或过滤流量的策略条目称之为规则

--在进行路由选择前处理数据包(PREROUTING);
--处理流入的数据包(INPUT);
--处理流出的数据包(OUTPUT);
--处理转发的数据包(FORWARD);
--在进行路由选择后处理数据包(POSTROUTING)
ACCEPT(允许流量通过)
REJECT(拒绝流量通过),在拒绝流量后再回复一条“您的信息已经收到,但是被扔掉了”信息
LOG(记录日志信息)
DROP(拒绝流量通过),直接将流量丢弃而且不响应

Linux 系统中的防火墙策略设置为 REJECT 拒绝动作后,流量发送方会看到端口不可达的响应
防火墙策略修改成 DROP 拒绝动作后,流量发送方会看到响应超时的提醒,但是流量发送方无法判断流量是被拒绝,还是接收方主机当前不在线

# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
From 192.168.10.10 icmp_seq=1 Destination Port Unreachable
# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3000ms

iptables 中基本的命令参数
iptables 命令可以根据流量的源地址、目的地址、传输协议、服务类型等信息进行匹配
iptables 中常用的参数以及作用

-P 设置默认策略
-F 清空规则链
-L 查看规则链
-A 在规则链的末尾加入新规则
-I num 在规则链的头部加入新规则
-D num 删除某一条规则
-s 匹配来源地址 IP/MASK,加叹号“!”表示除这个 IP 外
-d 匹配目标地址
-i 网卡名称 匹配从这块网卡流入的数据
-o 网卡名称 匹配从这块网卡流出的数据
-p 匹配协议,如 TCP、 UDP、 ICMP
--dport num 匹配目标端口号
--sport num 匹配来源端口号
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere             udp dpt:domain
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:domain
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootps
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:bootps

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             192.168.122.0/24     ctstate RELATED,ESTABLISHED
ACCEPT     all  --  192.168.122.0/24     anywhere            
ACCEPT     all  --  anywhere             anywhere            
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable
REJECT     all  --  anywhere             anywhere             reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     udp  --  anywhere             anywhere             udp dpt:bootpc
# iptables -F #清空规则
# iptables -L
# iptables -I INPUT -p icmp -j ACCEPT ##添加允许ICMP流量进入规则(ping命令)
# iptables -D INPUT 1 #删除规则num为1的规则
# iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT #允许192.168.10.0网段的主机访问本机的22号端口
# iptables -A INPUT -p tcp --dport 22 -j REJECT #拒绝来自其他主机的22端口流量
防火墙策略规则是按照从上到下的顺序匹配的,因此一定要把允许动作放到拒绝动作前面,否则所有的流量就将被拒绝掉,
# iptables -I INPUT -p tcp --dport 12345 -j REJECT #在input规则中拒绝所有访问本机的12345端口
# iptables -I INPUT -p udp --dport 12345 -j REJECT #在input规则中拒绝所有访问本机的12345端口
# iptables -I INPUT -p tcp -s 192.168.10.5 --dport 80 -j REJECT
# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
# service iptables save

firewalld

RHEL 7 系统中集成了多款防火墙管理工具, 其中 firewalld服务是默认的防火墙配置管理工具
firewalld 支持动态更新技术并加入了区域(zone)的概念。
简单来说,区域就是 firewalld 预先准备了几套防火墙策略集合(策略模板),用户可以根据生产场景的不同而选择合适的策略集合,
从而实现防火墙策略之间的快速切换。

firewalld 中常见的区域名称(默认为 public)
trusted 允许所有的数据包
home    拒绝流入的流量,除非与流出的流量相关;而如果流量与 ssh、 mdns、 ipp-client、amba-client 与 dhcpv6-client 服务相关,则允许流量
internal 等同于 home 区域
work    拒绝流入的流量,除非与流出的流量数相关;而如果流量与 ssh、 ipp-client 与dhcpv6-client 服务相关,则允许流量
public   拒绝流入的流量,除非与流出的流量相关;而如果流量与 ssh、 dhcpv6-client 服务相关,则允许流量
external 拒绝流入的流量,除非与流出的流量相关;而如果流量与 ssh 服务相关,则允许流量
dmz 拒绝流入的流量,除非与流出的流量相关;而如果流量与 ssh 服务相关,则允许流量
block 拒绝流入的流量,除非与流出的流量相关
drop 拒绝流入的流量,除非与流出的流量相关

firewalld-cmd 是 firewalld 防火墙配置管理工具的 CLI版本
使用 firewalld 配置的防火墙策略默认为运行时(Runtime)模式,当前模式,重启失效。
永久(Permanent)模式,策略一直存在,方法就是在用 firewall-cmd 命令正常设置防火墙策略时添加--permanent 参数
firewall-cmd --reload 配置立即生效

[root@localhost ~]# firewall-cmd --get-default-zone
FirewallD is not running
[root@localhost ~]# systemctl start firewalld.service 
[root@localhost ~]# firewall-cmd --get-default-zone #查看当前所使用的区域
public
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33 #查询ens33网卡在firewalld服务中的区域
public
# firewall-cmd --permanent --zone=external --change-interface=ens33 #修改ens33网卡的默认区域为external
# firewall-cmd --get-zone-of-interface=ens33
# firewall-cmd --permanent --get-zone-of-interface=ens33
# firewall-cmd --set-default-zone=public
# firewall-cmd --panic-on #启动/关闭 firewalld 防火墙服务的应急状况模式,阻断一切网络连接(当远程控制服务器时请慎用)
# firewall-cmd --panic-off 
[root@localhost ~]# firewall-cmd --zone=public --query-service=ssh #查询zone区域是否允许ssh和https服务
yes
[root@localhost ~]# firewall-cmd --zone=public --query-service=https
no 
[root@localhost ~]# firewall-cmd --zone=public --add-service=https #把https协议的流量设置为永久允许
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@localhost ~]# firewall-cmd --reload #并立即生效
success 
# firewall-cmd --permanent --zone=public --remove-service=http #拒绝http流量
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080-8081/tcp #允许8080,8081端口
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports
8080-8081/tcp 

流量转发

流量转发命令格式为 firewall-cmd --permanent --zone=<区域> --add-forward-port=port=<源端口号>:proto=<协议>:toport=<目标端口号>:toaddr=<目标 IP 地址>
# firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.10.10
把原本访问本机 888 端口的流量转发到 22 端口,要且求当前和长期均有效:
firewalld 中的富规则表示更细致、更详细的防火墙策略配置,它可以针对系统服务、端口号、源地址和目标地址等诸多信息进行更有正对性的策略配置。

 

以上是关于linux 简单记录8 --iptables 与 firewalld 防火墙的主要内容,如果未能解决你的问题,请参考以下文章

Linux iptables防火墙原理与常用配置

linux 如何调出上网历史记录(用IP分类,因为有多个ip)用程序实现

linux下的防火墙Netfilter配置:iptables的了解与使用(详细)

CentOS 7/8 安装启动iptables.services信息包过滤系统报错

CentOS 7/8 安装启动iptables.services信息包过滤系统报错

linux/unix -shell值iptables基础知识