netfilter firewalld --> iptables
1. 开启netfilter防火墙
关闭 firewalld
systemctl disable firewalld
systemctl stop firewalld
开启 netfilter
yum install -y iptables-services
systemctl enable iptables
systemctl start iptables
2. netfilter 5表5链 介绍
filter INPUT 目标-->进入包 FORWARD 转发包 OUTPUT 本地包-->目标
nat PREROUTING 进入的包-->更改数据包 POSTROUTING 更改数据包-->出去的包
raw
mangle
3. iptables 语法
查看默认规则 iptables -nvL
iptables -F
清空规则/etc/sysconfig/iptables
默认规则位置service iptables save
保存规则iptables -t filter -nvL
查看表内的规则
-t 指定表 没有-t 默认为filter表iptables -Z
清零 清空统计(数据包、字节等信息)
格式:iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.2 --dport 80 -j DROP
-A 添加 在表后面添加规则
-I 插入 在表最前端插入规则 (优先执行最前面的规则,多规则满足条件,优先执行最前的一条)
-D 删除 删除规则,删除规则内容要与添加的规则内容一样iptables -nvL --line-number
打印规则编号,iptables -D INPUT 7
删除编号为7 的规则
-i 指定网卡
-P 默认规则 (添加规则为默认规则)※ 注意:不要轻易操作
DROP 丢掉
REJECT 接收数据,返回拒绝
ACCEPT 允许数据通过
4. iptables filter 表 小案例
vim /usr/local/sbin/iptables
#! /bin/bash
ipt="/usr/sbin/iptables"
$ipt -F
$ipt -P INPUT DROP
$ipt -P OUTPUT ACCEPT
$ipt -P FORWARD ACCEPT
$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #符合要求的状态,通过
$ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT
禁ping(icmp示例)iptables -I INPUT -p icmp --icmp-type 8 -j DROP
5. iptables nat 表应用
- nat表应用
A机器两块网卡ens33(192.168.133.130)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens37(192.168.100.100),和A机器ens37可以通信互联。
需求1:可以让B机器连接外网
A机器上打开路由转发echo "1">/proc/sys/net/ipv4/ip_forward
A上执行iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
B上设置网关为192.168.100.1
需求2:C机器只能和A通信,让C机器可以直接连通B机器的22端口
A上打开路由转发echo "1">/ proc/sys/net/ipv4/ip_forward
A上执行iptables -t nat -A PREROUTING -d 192.168.133.130 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
A上执行iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.133.130
B上设置网关为192.168.100.1
route -n
查看网关route add default gw 10.0.0.1
添加网关 10.0.0.1
6. iptables规则备份和恢复
备份:iptables-services > /tmp/1.txt
恢复:iptables-restore < /tmp/1.txt