#####1.火墙介绍#######################
1.netfilter
2.iptables
3.iptables | firewalld
#####2.火墙管理工具切换#################
在rhel8中默认使用的是firewalld
[root@rhel8_node1 ~]# dnf install iptables-services.x86_64 -y
firewalld --- iptables
[root@rhel8_node1 ~]# systemctl stop firewalld
[root@rhel8_node1 ~]# systemctl disable firewalld
[root@rhel8_node1 ~]# systemctl mask firewalld
[root@rhel8_node1 ~]# systemctl enable iptables.service
注意:
iptables --- firewalld
systemctl stop iptables
systemctl disable iptables
systemctl mask iptables
systemctl enable firewalld
#####3.iptables的使用###############
[root@rhel8_node1 ~]# iptables -L ##查看
[root@rhel8_node1 ~]# vim /etc/sysconfig/iptables ##iptables策略记录文件
永久保存策略:
[root@rhel8_node1 ~]# iptables -F ##刷新
[root@rhel8_node1 ~]# iptables -nL
[root@rhel8_node1 ~]# systemctl restart iptables.service ##重启之后 策略恢复
[root@rhel8_node1 iptables -L
方法:
[root@rhel8_node1 ~]# iptables -F
[root@rhel8_node1 ~]# iptables-save > /etc/sysconfig/iptables ##将火墙信息导入
或者:
[root@rhel8_node1 ~]# service iptables save
测试:
[root@rhel8_node1 ~]# systemctl restart iptables.service
[root@rhel8_node1 ~]# iptables -nL
#####4.火墙默认策略#####################
默认策略中的五条链:
input | 输入 |
output | 输出 |
forward | 转发 |
postrouting | 路由之后 |
prerouting | 路由之前 |
默认三张表:
filter | 经过本机内核的数据(input output forward) |
nat | 不经过内核的数据(input output postrouting prerouting) |
mangle | 当filter和nat表不够用时使用(input output postrouting prerouting forward) |
iptables命令:
iptables
-t | 指定表名称 |
-n | 不做解析 |
-L | 查看 |
-A | 添加策略 |
-p | 协议 |
--dport | 目的地端口 |
-s | 来源 |
-j(后可跟以下五条) | 动作 |
ACCEPT | 允许 |
DROP | 丢弃 |
REJECT | 拒绝 |
SNAT | 源地址转换 |
DNAT | 目的地地址转换 |
-N | 新建链 |
-E | 更改链名称 |
-X | 删除链 |
-D | 删除规则 |
-I | 插入规则 |
-R | 更改规则 |
-P | 更改默认规则 |
例子:
[root@rhel8_node1 ~]# iptables -t filter -L
[root@rhel8_node1 Desktop]# watch -n 1 iptables -t filter -nL ##监控面板
[root@rhel8_node1 Desktop]# iptables -A INPUT -j REJECT
[root@rhel8_node1 Desktop]# iptables -D INPUT 1
[root@rhel8_node1 Desktop]# iptables -A INPUT -j DROP
[root@rhel8_node1 Desktop]# iptables -A INPUT -s 192.168.3.109 -j ACCEPT ##按照顺序执行
[root@rhel8_node1 Desktop]# iptables -I INPUT -s 192.168.3.109 -j ACCEPT ##插入默认第一条
[root@rhel8_node1 Desktop]# iptables -I INPUT -s 192.168.3.109 -p tcp --dport 22 -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -R INPUT 1 -s 192.168.3.109 -p tcp --dport 80 -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -P INPUT DROP
[root@rhel8_node1 Desktop]# iptables -N redhat
[root@rhel8_node1 Desktop]# iptables -E redhat REDHAT
[root@rhel8_node1 Desktop]# iptables -X REDHAT
数据包状态:
RELATED | 建立过连接的 |
ESTABLISHED | 正在连接的 |
NEW | 新的 |
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -i lo -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW ! -s 192.168.3.109 -p tcp --dport 22 -j ACCEPT ##除了109之外的
[root@rhel8_node1 Desktop]# iptables -A INPUT -m state --state NEW -j REJECT
测试:
[root@rhel7 ~]# ping 192.168.3.110 ##无法ping通
[root@rhel7 ~]# ssh root@192.168.3.110 ##无法连接
[root@test Desktop]# ssh root@192.168.3.110 ##可连接
[root@rhel8_node1 Desktop]# service iptables save ##保存火墙策略
数据转发:
[root@rhel8_node1 ~]# nmcli connection add con-name westos1 ifname ens224 type ethernet ip4 172.25.254.110/24 ##添加网卡
[root@test Desktop]# nmcli connection modify uuid ea74cf24-c2a2-ecee-3747-a2d76d46f93b ip4 172.25.254.120/24
[root@test Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
[root@test Desktop]# systemctl restart NetworkManager
[root@test Desktop]# nmcli connection down ens160
[root@test Desktop]# nmcli connection up ens160
[root@test Desktop]# route -n
nat表中的dnat snat
[root@rhel8_node1 Desktop]# watch -n 1 iptables -t nat -nL
snat:
[root@rhel8_node1 Desktop]# iptables -t nat -A POSTROUTING -o ens160 -j SNAT --to-source 192.168.3.110
测试:
[root@test Desktop]# ping 192.168.3.109 ##可ping通
[root@rhel7 ~]# w -i
dnat:
[root@rhel8_node1 Desktop]# iptables -t nat -A PREROUTING -i ens160 -p tcp --dport 22 -j DNAT --to-dest 172.25.254.120
测试:
[root@rhel7 ~]# ssh root@192.168.3.110
[root@test ~]# ifconfig