目录
一、火墙介绍
在Linux中火墙策略是基于netfilter实现的。
1、netfilter:内核上有个安全插件netfilter(访问控制列表),这个列表里有许多详细的规则,当对这个规则允许或拒绝时,可以控制其他主机是否能访问,极大的提高了安全性。
2、iptables:管理netfilter的工具,通过iptables往netfilter表格里面写网络安全策略。
3、iptables | firewalld:对iptables进行管理,用iptables或firewalld手段来写网络安全策略。
二、火墙管理工具切换
在rhel8中默认使用的是firewalld
1、firewalld —> iptables
dnf install iptables-services -y
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl mask firewalld.service
systemctl enable --now iptables.service
2、iptables —> firewalld
systemctl stop iptables.service
systemctl disable --now iptables.service
systemctl mask iptables.service
systemctl unmask firewalld.service
systemctl enable --now firewalld.service
三、iptables的使用及火墙默认策略
1、火墙策略的永久保存
iptables策略记录文件:/etc/sysconfig/iptables
- 永久保存策略:
iptables-save > /etc/sysconfig/iptables
service iptables save
2、火墙默认策略
默认策略中的5条链
input |
输入(目的地址为本机) |
output |
输出(原地址为本地,向外发送) |
forward |
实现转发 |
postrouting |
路由之后(发送到网卡之前) |
prerouting |
数据包进入路由之前 |
默认策略的3张表
filter ## 经过本机内核的数据 ( input output forward )nat ## 不经过内核的数据 ( postrouting , prerouting,input,output )mangle ## 当 filter 和 nat 表不够用时使用 ( input output forward postrouting , prerouting, )
3、iptables的命令
-F 清空iptables(如不保存,服务重启后,策略会恢复)
-t 指定表名称(默认查看filter表)
-n 不做解析
-L 查看
-A 添加策略
-p 协议
--dport 目的地端口
--sport 来源端口
-s 来源
-j 动作
ACCEPT:允许
DROP:丢弃
REJECT:拒绝
SNAT:源地址转换
DNAT:目的地地址转换
-N 新建链
-E 更改链名称
-X 删除链
-D 删除规则
-I 插入规则
-R 更改规则
-P 更改默认规则
(1)清空iptables表
iptables -F
iptables -nL
(2)查看指定表
iptables -t nat -nL
(3)允许指定ip访问
iptables -t filter -A INPUT -s 1.1.1.111 -j ACCEPT
iptables -nL
4)-I INPUT 2
:插入指定次序规则(INPUT 默认第一行)
iptables -t filter -A INPUT -s 1.1.1.112 -j ACCEPT
iptables -t filter -I INPUT 2 -s 198.168.1.25