一、火墙级别
1.netfilter
2.iptables
3.iptables|firewalld
二、防火墙的管理工具切换
防火墙的工具有firewalld和iptables,其中在rhel8中默认的管理工具是firewalld
iptables和firewalld的下载:
dnf install iptables-services -y
dnf install firewalld -y
切换管理工具从firewalld到iptables
- systemctl stop firewalld
- systemctl disable firewalld
- systemctl mask firewalld
- systemctl unmask iptables
- systemctl enable --now iptables
切换管理工具从iptables到firewalld - systemctl stop iptables
- systemctl disable iptables
- systemctl mask iptables
- systemctl unmask firewalld
- systemctl enable --now firewalld
三、iptables的使用
3.1 火墙策略的永久保存
iptables-save > /etc/sysconfig/iptables
service iptables save
3.2 iptables中的常用命令
iptable:
参数 | 作用 |
---|---|
-t | 制定表名称(默认表格为filter) |
-n | 不做解析 |
-L | 查看 |
-A | 添加策略 |
-p | 协议 |
–dport | 目的地端口 |
-s | 来源 |
-j | 动作 |
ACCEPT | 允许 |
DROP | 丢弃 |
REJECT | 拒绝 |
SNAT | 源地址转换 |
DNAT | 目的地地址转换 |
-N | 新建链 |
-E | 更改链名称 |
-X | 删除链(将上一步的GGG删除) |
-D | 删除规则(将第一行的规则删除) |
-l | 插入规则 |
-R | 更改规则 |
-P | 更改默认规则 |
3.3 火墙默认策略
默认策略中的五条链
input:输入
output:输出
forward:转发
postrouting:路由之后
prerouting:路由之前
默认的三张表
filter=经过本机内核的数据(input output forward)
nat=不经过内核的数据(postrouting prerouting output input)
mangle=当filter和nat表不够用时使用(input output forward postrouting prerouting)
四、iptables策略优化
三种数据状态:
ESTABLISHED:正在建立连接的
RELATED:之前连接过的
NEW:从未连接的
4.1 火墙优化部署
命令 | 作用 |
---|---|
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT | 允许RELATED和ESTABLISHED的数据链接 |
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT | 允许回环接口中NEW状态的数据连接 |
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT | 允许状态为NEW的数据访问80端口 |
iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT | 允许状态为NEW的数据访问22端口 |
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT | 状态为NEW的数据访问53端口 |
iptables -A INPUT -m state --state NEW ! -s 192.168.43.10 -p tcp --dport 22 -j ACCEPT | 允许192.168.43.10中状态为NEW的数据访问22端口 |
iptables -A INPUT -m state --state NEW -j ACCEPT|其余访问全部拒绝
service iptables save
snat
iptable -t nat -A POSTROUTING -o enp1s0 -j SNAT --to-source 192.168.43.10
dnat
iptable -t nat -A PREROUTING -i enp7s0 -j DNAT --to-disk=192.168.43.10
五、firewalld
5.1 firewalld使用
trusted:接受所有网络连接
home:家庭网络,可以接受ssh mdns ipp-client samba-client dhcp-client
work:工作网络,可以接受ssh ipp-client dhcp-client
public:公共网络 可以接受ssh dhcp-client
dmz:军用级网络 可以接受ssh
block:拒绝所有
drop:所有数据丢弃无回复
internal:内部网络 可以接受ssh mdns ipp-client samba-client dhcp-client
5.2 firewalld设定与数据存储
火墙配置目录地址: /etc/firewalld
火墙允许服务文件:/etc/firewalld/zones/public.xml
命令 | 功能 |
---|---|
firewall-cmd --state | 查看火墙状态 |
firewall-cmd --get-active-zones | 查看当前火墙生效域 |
firewall-cmd --get-default-zones | 查看默认域 |
firewall-cmd --list-all | 查看默认域中火墙策略 |
firewall-cmd --list-all --zone=work | 查看指定域中的火墙策略 |
firewall-cmd --set-default-zone-trusted | 修改当前域为trusted |
firewall-cmd --get-servers | 查看可设服务 |
firewall-cmd --permanent --remove-service=cockpit | 永久移除服务 |
firewall-cmd --reload | 重新加载 |
firewall-cmd --permanent --add-source=192.168.43.0/24 --zone=block | 指定数据来源访问指定域 |
firewall-cmd --reload | 重新加载 |
firewall-cmd --permanent --remoce-source=192.168.43.0/24 --zone=block | 移除数据来源访问指定域 |
firewall-cmd --permanent --remove-interface=enp160 -zone=public | 删除指定域网络接口 |
firewall-cmd --permanent --add-interface=enp160 -zone=block | 添加指定域的网络接口 |
firewall-cmd --permanent --change-interface=enp160 -zone=public | 更改网络接口到指定域名 |
5.3 firewalld的高级规则
firewall-cmd --direct --get-all-rules
:查看高级规则
firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 192.168.43.10 -p tcp --dport 22 -j ACCEPT
:添加高级规则使得172.25.254.0/24网段无法访问本机
firewall-cmd --direct --remove-rule ipv4 filter INPUT 1 -s 192.168.43.10 -p tcp --dport 22 -j ACCEPT
:删除此高级规则
5.2 firewalld中的NAT
SNAT
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
DNAT
firewall-cmd --permanent --add-forward-port=port=22:proto=tcp:toaddr=192.168.43.10
firewall-cmd --reload