iptables 与 Firewalld 防火墙的概述
iptables 和 Firewalld 是 Linux 系统中两种常见的防火墙管理工具。它们用于配置和管理网络流量规则,以确保系统的安全性。iptables 是一个基于内核的防火墙工具,直接操作网络包过滤规则,而 Firewalld 则是一个更高级的动态管理工具,提供了更灵活的配置方式。
iptables 的核心功能
iptables 通过一系列规则链来管理网络流量,主要包括 INPUT、OUTPUT 和 FORWARD 三个默认链。INPUT 链用于处理进入系统的数据包,OUTPUT 链用于处理从系统发出的数据包,FORWARD 链用于处理通过系统转发的数据包。
# 允许所有进入的 SSH 连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 拒绝所有其他进入的流量
iptables -A INPUT -j DROP
iptables 的规则可以基于协议、端口、IP 地址等条件进行配置,并且支持 NAT(网络地址转换)功能。
Firewalld 的核心功能
Firewalld 是一个动态防火墙管理工具,支持区域(zone)的概念。每个区域可以定义不同的规则集,适用于不同的网络环境。Firewalld 使用 D-Bus 接口与内核通信,允许在不重启防火墙的情况下动态更改规则。
# 允许 HTTP 服务
firewall-cmd --zone=public --add-service=http --permanent
# 重新加载防火墙配置
firewall-cmd --reload
Firewalld 提供了更直观的命令行工具 firewall-cmd
,并且支持丰富的服务定义,简化了常见服务的配置。
iptables 与 Firewalld 的对比
iptables 是一个底层的工具,直接操作内核的 netfilter 框架,适合需要精细控制的场景。然而,它的配置相对复杂,且规则更改需要重新加载整个规则集。
Firewalld 则提供了更高层次的抽象,通过区域和服务的概念简化了配置过程。它支持动态更新规则,适合需要频繁更改防火墙配置的环境。
iptables
iptables 是 Linux 系统中用于配置防火墙规则的工具,提供了丰富的参数来定义和管理网络流量。以下是一些常用的 iptables 参数配置:
基本命令参数
-A
:将规则添加到指定链的末尾。-I
:将规则插入到指定链的开头或指定位置。-D
:从指定链中删除规则。-L
:列出指定链中的所有规则。-F
:清空指定链中的所有规则。-P
:设置指定链的默认策略(如 ACCEPT、DROP)。-N
:创建新的用户自定义链。-X
:删除用户自定义链。
匹配条件参数
-p
:指定协议类型(如 tcp、udp、icmp)。-s
:指定源 IP 地址或地址范围。-d
:指定目标 IP 地址或地址范围。--sport
:指定源端口号或端口范围。--dport
:指定目标端口号或端口范围。-i
:指定数据包进入的网络接口。-o
:指定数据包离开的网络接口。-m
:使用扩展模块进行匹配(如 state、multiport)。
目标动作参数
-j
:指定规则匹配后的动作(如 ACCEPT、DROP、REJECT、LOG)。-g
:跳转到指定的用户自定义链。--reject-with
:指定 REJECT 动作的拒绝类型(如 icmp-port-unreachable)。
日志记录参数
--log-prefix
:为日志记录添加前缀。--log-level
:指定日志记录的级别。
示例配置
# 允许来自 192.168.1.0/24 的 SSH 连接
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
# 拒绝所有来自外部的 ICMP 请求
iptables -A INPUT -p icmp -j DROP
# 记录并拒绝所有来自 10.0.0.1 的连接
iptables -A INPUT -s 10.0.0.1 -j LOG --log-prefix "Blocked IP: "
iptables -A INPUT -s 10.0.0.1 -j DROP
# 设置默认策略为 DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
Firewall
首先安装firewall图形化工具,使用命令yum install firewall-config。当然在此之前你需要配置相应 的repo来安装对应的服务
Centos-8.repo
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/BaseOS/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[PowerTools]
name=CentOS-$releasever - PowerTools - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/PowerTools/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
[AppStream]
name=CentOS-$releasever - AppStream - mirrors.aliyun.com
#failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-Official
之后你需要安装更新一下repo地址
yum update
yum upgrade
最后进行安装就可以: